From 3a7de7a7777303527e997bd1c87c4084610ee3a7 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 16 Jul 2024 19:03:41 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20MemberNotFoundException=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=ED=95=B8=EB=93=A4=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pyro/yolog/domain/auth/service/AuthService.java | 4 ++-- .../member/exception/MemberNotFoundException.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/pyro/yolog/domain/member/exception/MemberNotFoundException.java diff --git a/src/main/java/com/pyro/yolog/domain/auth/service/AuthService.java b/src/main/java/com/pyro/yolog/domain/auth/service/AuthService.java index 6d47b44..e1cb920 100644 --- a/src/main/java/com/pyro/yolog/domain/auth/service/AuthService.java +++ b/src/main/java/com/pyro/yolog/domain/auth/service/AuthService.java @@ -2,10 +2,10 @@ import com.pyro.yolog.domain.member.entity.Member; import com.pyro.yolog.domain.member.entity.SocialType; +import com.pyro.yolog.domain.member.exception.MemberNotFoundException; import com.pyro.yolog.domain.member.repository.MemberRepository; import com.pyro.yolog.global.jwt.service.JwtService; import com.pyro.yolog.global.query.QueryService; -import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -37,7 +37,7 @@ public Long getLoginUserId() { public Member getLoginUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); UserDetails userDetails = (UserDetails) authentication.getPrincipal(); - return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(EntityNotFoundException::new); + return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(MemberNotFoundException::new); } diff --git a/src/main/java/com/pyro/yolog/domain/member/exception/MemberNotFoundException.java b/src/main/java/com/pyro/yolog/domain/member/exception/MemberNotFoundException.java new file mode 100644 index 0000000..16a9812 --- /dev/null +++ b/src/main/java/com/pyro/yolog/domain/member/exception/MemberNotFoundException.java @@ -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 MemberNotFoundException extends BusinessException { + public MemberNotFoundException() { + super(ErrorCode.MEMBER_NOT_FOUND_ERROR); + } +} From ec0a2159f7c846af20f28d9f372b458d0b84b2ee Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 16 Jul 2024 19:04:20 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20DiaryNotFoundException=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/diary/exception/DiaryNotFoundException.java | 10 ++++++++++ .../java/com/pyro/yolog/global/error/ErrorCode.java | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 src/main/java/com/pyro/yolog/domain/diary/exception/DiaryNotFoundException.java diff --git a/src/main/java/com/pyro/yolog/domain/diary/exception/DiaryNotFoundException.java b/src/main/java/com/pyro/yolog/domain/diary/exception/DiaryNotFoundException.java new file mode 100644 index 0000000..d94365d --- /dev/null +++ b/src/main/java/com/pyro/yolog/domain/diary/exception/DiaryNotFoundException.java @@ -0,0 +1,10 @@ +package com.pyro.yolog.domain.diary.exception; + +import com.pyro.yolog.global.error.ErrorCode; +import com.pyro.yolog.global.error.exception.BusinessException; + +public class DiaryNotFoundException extends BusinessException { + public DiaryNotFoundException() { + super(ErrorCode.DIARY_NOT_FOUND_ERROR); + } +} diff --git a/src/main/java/com/pyro/yolog/global/error/ErrorCode.java b/src/main/java/com/pyro/yolog/global/error/ErrorCode.java index 9fc83b6..d49970a 100644 --- a/src/main/java/com/pyro/yolog/global/error/ErrorCode.java +++ b/src/main/java/com/pyro/yolog/global/error/ErrorCode.java @@ -11,6 +11,7 @@ public enum ErrorCode { // MEMBER MEMBER_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "회원 정보를 찾지 못했습니다." ), + OWNER_NOT_EQUAL_ERROR(HttpStatus.BAD_REQUEST, "작성자만 접근 가능합니다."), // S3 FILE_UPLOAD_FAILURE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "S3 파일 업로드에 실패했습니다."), @@ -26,6 +27,7 @@ public enum ErrorCode { REQUEST_COLOR_COVER_INVALID_ERROR(HttpStatus.BAD_REQUEST, "올바른 색상 코드를 입력해야 합니다."), //DIARY + DIARY_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "해당 일기를 찾지 못했습니다."), REQUEST_WEATHER_NAME_INVALID_ERROR(HttpStatus.BAD_REQUEST, "올바른 날씨를 입력해야 합니다."), REQUEST_MOOD_NAME_INVALID_ERROR(HttpStatus.BAD_REQUEST, "올바른 기분을 입력해야 합니다."), From eb1beb36db8c7a0f3705eab02c3cd436952da68d Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 16 Jul 2024 19:04:47 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EC=9E=91=EC=84=B1=EC=9E=90?= =?UTF-8?q?=EC=99=80=20=EC=9D=BC=EC=B9=98=ED=95=98=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/diary/service/DiaryService.java | 45 ++++++++++++++----- .../exception/OwnerNotEqualException.java | 10 +++++ .../domain/trip/service/TripService.java | 19 ++++++-- 3 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/pyro/yolog/domain/member/exception/OwnerNotEqualException.java diff --git a/src/main/java/com/pyro/yolog/domain/diary/service/DiaryService.java b/src/main/java/com/pyro/yolog/domain/diary/service/DiaryService.java index a3a9f34..dd66bbd 100644 --- a/src/main/java/com/pyro/yolog/domain/diary/service/DiaryService.java +++ b/src/main/java/com/pyro/yolog/domain/diary/service/DiaryService.java @@ -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; @@ -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; @@ -29,24 +30,30 @@ @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 getDiaries(Long tripId, String dayName) { + checkTripOwner(tripRepository.findById(tripId) + .orElseThrow(TripNotFoundException::new)); List 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()))); @@ -54,30 +61,35 @@ public DefaultDiaryResponse createDefaultDiary(final Long tripId, final DiaryDat @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()); @@ -85,4 +97,13 @@ public void deleteOutOfDuration(Trip trip) { }); } + private void checkDiaryOwner(Diary diary) { + checkTripOwner(diary.getTrip()); + } + + private void checkTripOwner(Trip trip) { + if (!trip.getMember().equals(authService.getLoginUser())) { + throw new OwnerNotEqualException(); + } + } } diff --git a/src/main/java/com/pyro/yolog/domain/member/exception/OwnerNotEqualException.java b/src/main/java/com/pyro/yolog/domain/member/exception/OwnerNotEqualException.java new file mode 100644 index 0000000..640b45c --- /dev/null +++ b/src/main/java/com/pyro/yolog/domain/member/exception/OwnerNotEqualException.java @@ -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); + } +} diff --git a/src/main/java/com/pyro/yolog/domain/trip/service/TripService.java b/src/main/java/com/pyro/yolog/domain/trip/service/TripService.java index 43ae5ea..df1e305 100644 --- a/src/main/java/com/pyro/yolog/domain/trip/service/TripService.java +++ b/src/main/java/com/pyro/yolog/domain/trip/service/TripService.java @@ -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; @@ -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 getTrips() { Member login = authService.getLoginUser(); return tripRepository.findAllByMember(login).stream() @@ -55,4 +54,16 @@ public List 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(); + } + } }