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 8968492d..5b4f441e 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 2fe1558f..aa1a8408 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/exception/DiaryLikeNotFoundException.java b/src/main/java/com/modernfarmer/farmusspring/domain/myveggiegarden/exception/DiaryLikeNotFoundException.java deleted file mode 100644 index 7c9b74a2..00000000 --- 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 00af7295..4a8fa7db 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 8170a089..7779ac85 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 00000000..387820c2 --- /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 00000000..9043dddb --- /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); + } +} 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 351c49d0..92211860 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); + + } 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 e62aca51..ba0a67bf 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); - - - - - - } 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 84e2fc94..df543656 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; @@ -97,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); @@ -134,15 +133,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); } @@ -189,48 +196,37 @@ 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 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){ @@ -238,7 +234,6 @@ public void validateDiaryComment(Optional diaryComment){ throw new DiaryCommentNotFoundException("해당 유저는 댓글 삭제 권한이 없습니다."); } } - private void addMyyVeggieDiary( String content, boolean isOpen, @@ -256,7 +251,6 @@ private void addMyyVeggieDiary( ); myVeggie.addDiary(newDiary); } - private String getImageUrl(MultipartFile multipartFile) throws IOException { return s3Service.uploadImage(multipartFile, "dairyimage"); }