Skip to content

Commit

Permalink
feat: 작성자와 일치하는지 검증하는 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjy committed Jul 16, 2024
1 parent ec0a215 commit eb1beb3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
45 changes: 33 additions & 12 deletions src/main/java/com/pyro/yolog/domain/diary/service/DiaryService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pyro.yolog.domain.diary.service;

import com.pyro.yolog.domain.auth.service.AuthService;
import com.pyro.yolog.domain.diary.dto.request.DiaryContentRequest;
import com.pyro.yolog.domain.diary.dto.request.DiaryDateRequest;
import com.pyro.yolog.domain.diary.dto.request.MoodRequest;
Expand All @@ -8,13 +9,13 @@
import com.pyro.yolog.domain.diary.dto.response.DetailDiaryResponse;
import com.pyro.yolog.domain.diary.dto.response.PreviewDiaryResponse;
import com.pyro.yolog.domain.diary.entity.Diary;
import com.pyro.yolog.domain.diary.entity.Mood;
import com.pyro.yolog.domain.diary.entity.Weather;
import com.pyro.yolog.domain.diary.exception.DiaryNotFoundException;
import com.pyro.yolog.domain.diary.mapper.DiaryMapper;
import com.pyro.yolog.domain.diary.repository.DiaryRepository;
import com.pyro.yolog.domain.member.exception.OwnerNotEqualException;
import com.pyro.yolog.domain.trip.entity.Trip;
import com.pyro.yolog.domain.trip.exception.TripNotFoundException;
import com.pyro.yolog.domain.trip.repository.TripRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -29,60 +30,80 @@
@RequiredArgsConstructor
public class DiaryService {
private static final String DAY = "Day";

private final AuthService authService;
private final DiaryRepository diaryRepository;
private final TripRepository tripRepository;
private final DiaryMapper diaryMapper;

public DetailDiaryResponse getDiary(Long id) {
return new DetailDiaryResponse((diaryRepository.findById(id)
.orElseThrow(EntityNotFoundException::new)));
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
return new DetailDiaryResponse(diary);
}


public List<PreviewDiaryResponse> getDiaries(Long tripId, String dayName) {
checkTripOwner(tripRepository.findById(tripId)
.orElseThrow(TripNotFoundException::new));
List<Diary> diaries = diaryRepository.findAllByTripIdAndDayName(tripId, dayName);
return diaries.stream().map(PreviewDiaryResponse::new).collect(Collectors.toList());
return diaries.stream().map(PreviewDiaryResponse::new)
.collect(Collectors.toList());
}

@Transactional
public DefaultDiaryResponse createDefaultDiary(final Long tripId, final DiaryDateRequest request) {
Trip trip = tripRepository.findById(tripId).orElseThrow(EntityNotFoundException::new);
Trip trip = tripRepository.findById(tripId).orElseThrow(TripNotFoundException::new);
final String dayName = DAY + (ChronoUnit.DAYS.between(trip.getStartDate(), request.getDate()) + 1);
return diaryMapper.toDefaultFormatResponse(
diaryRepository.save(diaryMapper.toEntity(trip, dayName, request.getDate())));
}

@Transactional
public void updateDiaryTitleAndContent(Long id, DiaryContentRequest request) {
final Diary diary = diaryRepository.findById(id).orElseThrow(EntityNotFoundException::new);
final Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
diary.updateTitleAndContent(request);
}

@Transactional
public void deleteDiary(Long id) {
checkDiaryOwner(diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new));
diaryRepository.deleteById(id);
}

@Transactional
public void updateWeather(Long id, WeatherRequest request) {
diaryRepository.findById(id).orElseThrow(EntityNotFoundException::new)
.updateWeather(request.getWeather());
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
diary.updateWeather(request.getWeather());
}

@Transactional
public void updateMood(Long id, MoodRequest request) {
diaryRepository.findById(id).orElseThrow(EntityNotFoundException::new)
.updateMood(request.getMood());
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkTripOwner(diary.getTrip());
diary.updateMood(request.getMood());
}


@Transactional
public void deleteOutOfDuration(Trip trip) {
checkTripOwner(trip);
diaryRepository.findById(trip.getId()).ifPresent(diary -> {
if (diary.getTravelDate().isBefore(trip.getStartDate()) || diary.getTravelDate().isAfter(trip.getFinishDate())) {
diaryRepository.deleteById(diary.getId());
}
});
}

private void checkDiaryOwner(Diary diary) {
checkTripOwner(diary.getTrip());
}

private void checkTripOwner(Trip trip) {
if (!trip.getMember().equals(authService.getLoginUser())) {
throw new OwnerNotEqualException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pyro.yolog.domain.member.exception;

import com.pyro.yolog.global.error.ErrorCode;
import com.pyro.yolog.global.error.exception.BusinessException;

public class OwnerNotEqualException extends BusinessException {
public OwnerNotEqualException() {
super(ErrorCode.OWNER_NOT_EQUAL_ERROR);
}
}
19 changes: 15 additions & 4 deletions src/main/java/com/pyro/yolog/domain/trip/service/TripService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pyro.yolog.domain.diary.service.DiaryService;
import com.pyro.yolog.domain.member.entity.Member;
import com.pyro.yolog.domain.auth.service.AuthService;
import com.pyro.yolog.domain.member.exception.OwnerNotEqualException;
import com.pyro.yolog.domain.trip.dto.TripRequest;
import com.pyro.yolog.domain.trip.dto.TripResponse;
import com.pyro.yolog.domain.trip.entity.Trip;
Expand Down Expand Up @@ -33,19 +34,17 @@ public void saveTrip(final TripRequest request) {
@Transactional
public void updateTrip(final Long id, final TripRequest request) {
final Trip trip = tripRepository.findById(id).orElseThrow(TripNotFoundException::new);
checkTripOwner(trip);
trip.update(request);
diaryService.deleteOutOfDuration(trip);
}

@Transactional
public void deleteTrip(Long id) {
checkTripOwner(getTrip(id));
tripRepository.deleteById(id);
}

public Trip getTrip(final Long id) {
return tripRepository.findById(id).orElseThrow(TripNotFoundException::new);
}

public List<TripResponse> getTrips() {
Member login = authService.getLoginUser();
return tripRepository.findAllByMember(login).stream()
Expand All @@ -55,4 +54,16 @@ public List<TripResponse> getTrips() {
public TripResponse getTripDetail(Long id) {
return new TripResponse(getTrip(id));
}

public Trip getTrip(final Long id) {
Trip trip = tripRepository.findById(id).orElseThrow(TripNotFoundException::new);
checkTripOwner(trip);
return trip;
}

private void checkTripOwner(Trip trip) {
if (!trip.getMember().equals(authService.getLoginUser())) {
throw new OwnerNotEqualException();
}
}
}

0 comments on commit eb1beb3

Please sign in to comment.