From 0facacb5d4f6d35943b33eae208540412ba4cbbe Mon Sep 17 00:00:00 2001 From: seungryeol Date: Fri, 16 Aug 2024 00:04:41 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[fix]=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80,=20=EC=82=AD=EC=A0=9C=20=EA=B2=80=EC=A6=9D?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/MyVeggieDiaryService.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java index 84e2fc9..418434a 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java @@ -1,6 +1,5 @@ package com.modernfarmer.farmusspring.domain.myveggiegarden.service; -import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.DiaryDeleteDto; import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.*; @@ -10,6 +9,8 @@ import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.*; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.DiaryAccessDeniedException; +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.LikeNotFoundException; +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.LikeAlreadyExistExcpetion; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.MyVeggieNotFoundException; import com.modernfarmer.farmusspring.domain.myveggiegarden.repository.DiaryCommentRepository; import com.modernfarmer.farmusspring.domain.myveggiegarden.repository.DiaryLikeRepository; @@ -134,15 +135,23 @@ public MyVeggieDiaryCount selectDiaryCount(MyVeggie myVeggie) { @Transactional public void pressLike(Long userId, Long diaryId) { + Optional diaryLike = diaryLikeRepository.findDiaryLikeByDiaryIdAndUserId(diaryId, userId); + checkLikeData(diaryLike); User userData = userService.selectUserById(userId); Diary diaryData = selectDiaryById(diaryId); insertLike(userData, diaryData); } + public void checkLikeData(Optional diaryLike){ + if(diaryLike.isPresent()) { + throw new LikeAlreadyExistExcpetion("좋아요 권한 에러", MyVeggieGardenErrorCode.EXIST_ALREADY_LIKE); + } + } + @Transactional public void cancelLike(User user, Diary diary) { DiaryLike diaryLike = diaryRepository.findDiaryLikeByIdAndUser(user, diary); - checkDiaryLikeData(diaryLike); + checkLikeDeleteData(diaryLike); deleteLike(user, diary); } @@ -228,9 +237,9 @@ public void checkDiaryData(Diary diary){ } } - public void checkDiaryLikeData(DiaryLike diaryLike){ + public void checkLikeDeleteData(DiaryLike diaryLike){ if(diaryLike == null) { - throw new DiaryLikeNotFoundException("해당 좋아요 데이터는 존재하지 않습니다.", MyVeggieGardenErrorCode.NOT_FOUND_DIARY_Like); + throw new LikeNotFoundException("해당 좋아요 데이터는 존재하지 않습니다.", MyVeggieGardenErrorCode.NOT_FOUND_DIARY_Like); } } public void validateDiaryComment(Optional diaryComment){ From 43e1a2c0fd43db9e7dfda4cf082825d1be6f7ce4 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Fri, 16 Aug 2024 00:04:57 +0900 Subject: [PATCH 2/5] =?UTF-8?q?[feat]=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../myveggiegarden/repository/DiaryLikeRepository.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryLikeRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryLikeRepository.java index 351c49d..9221186 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryLikeRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryLikeRepository.java @@ -15,4 +15,9 @@ public interface DiaryLikeRepository extends JpaRepository { @Query("SELECT count(dl) FROM diary_like as dl WHERE dl.diary.id = :diaryId ") int findDiaryLikeCountById(@Param("diaryId") Long diaryId); + + @Query("SELECT dl FROM diary_like as dl WHERE dl.diary.id = :diaryId AND dl.user.id = :userId") + Optional findDiaryLikeByDiaryIdAndUserId(@Param("diaryId") Long diaryId, @Param("userId") Long userId); + + } From f429d34915b84d2accfd0faa1aedddb6415af6e4 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Fri, 16 Aug 2024 00:05:11 +0900 Subject: [PATCH 3/5] =?UTF-8?q?[feat]=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=97=90=EB=9F=AC=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/DiaryLikeNotFoundException.java | 9 --------- .../exception/MyVeggieGardenErrorCode.java | 3 ++- .../exception/MyVeggieGardenExceptionHandler.java | 10 +--------- .../exception/custom/LikeAlreadyExistExcpetion.java | 11 +++++++++++ .../exception/custom/LikeNotFoundException.java | 9 +++++++++ 5 files changed, 23 insertions(+), 19 deletions(-) delete mode 100644 src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/DiaryLikeNotFoundException.java create mode 100644 src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeAlreadyExistExcpetion.java create mode 100644 src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeNotFoundException.java diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/DiaryLikeNotFoundException.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/DiaryLikeNotFoundException.java deleted file mode 100644 index 7c9b74a..0000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/DiaryLikeNotFoundException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.modernfarmer.farmusspring.domain.myveggiegarden.exception; - -import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.MyVeggieGardenBaseException; - -public class DiaryLikeNotFoundException extends MyVeggieGardenBaseException { - public DiaryLikeNotFoundException(String message, MyVeggieGardenErrorCode myVeggieGardenErrorCode) { - super(message, myVeggieGardenErrorCode); - } -} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenErrorCode.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenErrorCode.java index 00af729..4a8fa7d 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenErrorCode.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenErrorCode.java @@ -14,7 +14,8 @@ public enum MyVeggieGardenErrorCode implements ResponseCode { NOT_FOUND_DIARY_Like(3003, "해당 유저 권한의 좋아요를 찾을 수 없습니다."), NOT_FOUND_VEGGIE(3004, "해당 채소를 찾을 수 없습니다."), NO_VEGGIE_FOR_REGISTER(3005, "해당 팜클럽에 가입할 수 있는 채소가 없습니다."), - NO_ACCESS_DIARY(3006,"일기에 접근 권한이 없습니다."); + NO_ACCESS_DIARY(3006,"일기에 접근 권한이 없습니다."), + EXIST_ALREADY_LIKE(3007,"이미 좋아요가 존재합니다."); private final int code; private final String message; diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java index 8170a08..7779ac8 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/MyVeggieGardenExceptionHandler.java @@ -1,9 +1,9 @@ package com.modernfarmer.farmusspring.domain.myveggiegarden.exception; +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.LikeNotFoundException; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.MyVeggieGardenBaseException; import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom.RoutineNotFoundException; -import com.modernfarmer.farmusspring.domain.test.exception.TestException; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; @@ -40,14 +40,6 @@ public BaseResponseDto handleDiaryCommentNotFoundException(DiaryCommentNotFou e.getMessage()); return BaseResponseDto.of(MyVeggieGardenErrorCode.NOT_FOUND_DIARY_COMMENT, e); } - @ExceptionHandler(DiaryLikeNotFoundException.class) - public BaseResponseDto handleDiaryLikeNotFoundException(DiaryLikeNotFoundException e, HttpServletRequest request) { - log.error("DiaryLikeException : {} {} errMessage={}\n", - request.getMethod(), - request.getRequestURI(), - e.getMessage()); - return BaseResponseDto.of(MyVeggieGardenErrorCode.NOT_FOUND_DIARY_Like, e); - } @ExceptionHandler(MyVeggieGardenBaseException.class) public BaseResponseDto handleMyVeggieException(MyVeggieGardenBaseException e, HttpServletRequest request) { diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeAlreadyExistExcpetion.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeAlreadyExistExcpetion.java new file mode 100644 index 0000000..387820c --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeAlreadyExistExcpetion.java @@ -0,0 +1,11 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.MyVeggieGardenErrorCode; +import lombok.Getter; + +@Getter +public class LikeAlreadyExistExcpetion extends MyVeggieGardenBaseException{ + public LikeAlreadyExistExcpetion(String message, MyVeggieGardenErrorCode errorCode) { + super(message, errorCode); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeNotFoundException.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeNotFoundException.java new file mode 100644 index 0000000..9043ddd --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/custom/LikeNotFoundException.java @@ -0,0 +1,9 @@ +package com.modernfarmer.farmusspring.domain.myveggiegarden.exception.custom; + +import com.modernfarmer.farmusspring.domain.myveggiegarden.exception.MyVeggieGardenErrorCode; + +public class LikeNotFoundException extends MyVeggieGardenBaseException { + public LikeNotFoundException(String message, MyVeggieGardenErrorCode myVeggieGardenErrorCode) { + super(message, myVeggieGardenErrorCode); + } +} From 703745ae6fc6958b8350cd05f774cd070663b6d4 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Fri, 16 Aug 2024 02:19:55 +0900 Subject: [PATCH 4/5] =?UTF-8?q?[fix]=20=EB=8B=A4=EC=9D=B4=EC=96=B4?= =?UTF-8?q?=EB=A6=AC=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=A2=8B=EC=95=84?= =?UTF-8?q?=EC=9A=94=20=EC=B2=B4=ED=82=B9=20=EC=A1=B0=EA=B1=B4=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/DiaryRepository.java | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryRepository.java index e62aca5..ba0a67b 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/repository/DiaryRepository.java @@ -22,56 +22,37 @@ public interface DiaryRepository extends JpaRepository { @Modifying @Query("DELETE FROM diary_like AS dl WHERE dl.diary = :diary AND dl.user = :user") void deleteDiaryLikeByIdAndUser(@Param("user") User user, @Param("diary") Diary diary); - @Modifying @Query("DELETE FROM diary AS d WHERE d.id = :diaryId") void deleteDiaryById(@Param("diaryId") Long diaryId); - - - - @Query("SELECT d FROM diary AS d WHERE d.id = :diaryId AND d.myVeggie.id = :myVeggieId") Optional findDiaryByIdAndMyVeggieId(@Param("diaryId") Long diaryId, @Param("myVeggieId") Long myVeggieId); @Query("SELECT dl FROM diary_like AS dl WHERE dl.diary = :diary AND dl.user = :user") DiaryLike findDiaryLikeByIdAndUser(@Param("user") User user, @Param("diary") Diary diary); - - @Query("SELECT dc FROM diary_comment AS dc " + "JOIN FETCH dc.diary AS d " + "JOIN FETCH d.myVeggie AS mv " + "JOIN FETCH mv.user " + "WHERE d.id = :diaryId") List findDiaryById(@Param("diaryId") Long diaryId); - - @Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary(d, " + "CASE WHEN dl.user.id = :userId THEN true ELSE false END) " + "FROM diary AS d " + "JOIN FETCH d.myVeggie AS mv " + - "JOIN FETCH mv.user " + - "LEFT JOIN diary_comment AS dc ON dc.id = d.id "+ - "LEFT JOIN diary_like AS dl ON dl.id = d.id " + + "LEFT JOIN diary_like AS dl ON dl.diary.id = d.id AND dl.user.id = :userId " + "WHERE d.farmClub.id = :farmClubId AND d.isOpen = true " ) List findDiaryByFarmClub(@Param("farmClubId") Long farmClubId, @Param("userId") Long userId); - @Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary(d, " + "CASE WHEN dl.user.id = :userId THEN true ELSE false END) " + "FROM diary AS d " + "JOIN FETCH d.myVeggie AS mv " + - "JOIN FETCH mv.user " + "LEFT JOIN diary_like AS dl ON dl.diary.id = d.id AND dl.user.id = :userId " + "WHERE d.myVeggie = :myVeggie " + - "ORDER BY d.createdDate DESC") + "ORDER BY d.createdDate DESC " + ) List findDiariesByMyVeggie(@Param("myVeggie") MyVeggie myVeggie, @Param("userId") Long userId); - @Query("SELECT d FROM diary AS d WHERE d.myVeggie = :myVeggie AND FUNCTION('DATE', d.createdDate) = CURRENT_DATE") List findDiaryByToday(@Param("myVeggie") MyVeggie myVeggie); - - - - - - } From 43817701d11cc5cad83a9849d20963ecbcd15629 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Fri, 16 Aug 2024 02:20:10 +0900 Subject: [PATCH 5/5] =?UTF-8?q?[style]=20=EC=A4=84=EB=B0=94=EA=BF=88=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MyVeggieDiaryController.java | 2 +- .../myveggiegarden/dto/response/AllDairy.java | 4 ---- .../service/MyVeggieDiaryService.java | 15 --------------- 3 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieDiaryController.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieDiaryController.java index 8968492..5b4f441 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieDiaryController.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/controller/MyVeggieDiaryController.java @@ -80,7 +80,7 @@ public BaseResponseDto selectDiaryCount(@PathVariable("myVeg } @GetMapping(value = "/{myVeggieId}/all") - public BaseResponseDto selectDiaryAll( + public BaseResponseDto selectDiaryAll( @PathVariable("myVeggieId") Long myVeggieId, @AuthenticationPrincipal CustomUser user ) { diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/AllDairy.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/AllDairy.java index 2fe1558..aa1a840 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/AllDairy.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/dto/response/AllDairy.java @@ -34,15 +34,11 @@ public static AllDairy of(SortedMyLikeDiary diaryAll, String date){ diaryAll.getDiary().getDiaryLikes().size(), diaryAll.getDiary().getDiaryComments().size(), diaryAll.isMyLike() - ); } - public static List processData(List diaryAllList){ return diaryAllList.stream() .map(diaryAll -> AllDairy.of(diaryAll, formatDate(diaryAll.getDiary().getCreatedDate()))) .toList(); } - - } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java index 418434a..df54365 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/service/MyVeggieDiaryService.java @@ -98,8 +98,6 @@ public void verifyMyVeggie(Optional myVeggie){ throw new MyVeggieNotFoundException("존재하지 않는 채소입니다.",MyVeggieGardenErrorCode.NOT_FOUND_VEGGIE); } } - - @Transactional public List findDiaryAccordingToFarmClub(Long farmClubId, Long userId) { List diaryList = diaryRepository.findDiaryByFarmClub(farmClubId, userId); @@ -198,45 +196,34 @@ public DiaryInteractionsDto selectComment(Long userId, Long diaryId) { int commentCount = diaryCommentRepository.findDiaryCommentCountById(diaryId); return DiaryInteractionsDto.of(diaryCommentContent,likeCount,commentCount); } - - - public void insertComment(String content, User user, Diary diary){ DiaryComment diaryComment = DiaryComment.createDiaryComment(content, diary, user); diary.addDiaryComment(diaryComment); } - public void insertLike(User user, Diary diary){ DiaryLike newDiary = DiaryLike.createDiaryLike(diary, user); diary.addDiaryLike(newDiary); } - public void deleteLike(User user, Diary diary){ diaryRepository.deleteDiaryLikeByIdAndUser(user, diary); } - public boolean verifyDiaryState(Diary diary){ if(diary == null){return true;} return false; } - public Diary selectTodayDiary(MyVeggie myVeggie){ return myVeggieRepository.findDiariesByMyVeggieAndToday(myVeggie); } - - public Diary selectDiaryById(Long diaryId){ Diary diaryData = myVeggieRepository.findDiaryById(diaryId); checkDiaryData(diaryData); return diaryData; } - public void checkDiaryData(Diary diary){ if(diary == null) { throw new DiaryNotFoundException("해당 일기는 존재하지 않습니다.", MyVeggieGardenErrorCode.NOT_FOUND_DIARY_Like); } } - public void checkLikeDeleteData(DiaryLike diaryLike){ if(diaryLike == null) { throw new LikeNotFoundException("해당 좋아요 데이터는 존재하지 않습니다.", MyVeggieGardenErrorCode.NOT_FOUND_DIARY_Like); @@ -247,7 +234,6 @@ public void validateDiaryComment(Optional diaryComment){ throw new DiaryCommentNotFoundException("해당 유저는 댓글 삭제 권한이 없습니다."); } } - private void addMyyVeggieDiary( String content, boolean isOpen, @@ -265,7 +251,6 @@ private void addMyyVeggieDiary( ); myVeggie.addDiary(newDiary); } - private String getImageUrl(MultipartFile multipartFile) throws IOException { return s3Service.uploadImage(multipartFile, "dairyimage"); }