Skip to content

Commit

Permalink
Merge branch 'refs/heads/dev' into fix_82/클라우드-이전
Browse files Browse the repository at this point in the history
  • Loading branch information
MinchoGreenT committed Aug 18, 2024
2 parents 35d5cdd + ab6d574 commit 270219d
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public BaseResponseDto<MyVeggieDiaryCount> selectDiaryCount(@PathVariable("myVeg
}

@GetMapping(value = "/{myVeggieId}/all")
public BaseResponseDto<MyVeggieDiaryCount> selectDiaryAll(
public BaseResponseDto<?> selectDiaryAll(
@PathVariable("myVeggieId") Long myVeggieId,
@AuthenticationPrincipal CustomUser user
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AllDairy> processData(List<SortedMyLikeDiary> diaryAllList){
return diaryAllList.stream()
.map(diaryAll -> AllDairy.of(diaryAll, formatDate(diaryAll.getDiary().getCreatedDate())))
.toList();
}


}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
public interface DiaryLikeRepository extends JpaRepository<DiaryLike, Long> {
@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<DiaryLike> findDiaryLikeByDiaryIdAndUserId(@Param("diaryId") Long diaryId, @Param("userId") Long userId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,37 @@ public interface DiaryRepository extends JpaRepository<Diary, Long> {
@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<Diary> 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<DiaryComment> 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<SortedMyLikeDiary> 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<SortedMyLikeDiary> 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<Diary> findDiaryByToday(@Param("myVeggie") MyVeggie myVeggie);






}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -97,8 +98,6 @@ public void verifyMyVeggie(Optional<MyVeggie> myVeggie){
throw new MyVeggieNotFoundException("존재하지 않는 채소입니다.",MyVeggieGardenErrorCode.NOT_FOUND_VEGGIE);
}
}


@Transactional
public List<FarmClubDiary> findDiaryAccordingToFarmClub(Long farmClubId, Long userId) {
List<SortedMyLikeDiary> diaryList = diaryRepository.findDiaryByFarmClub(farmClubId, userId);
Expand Down Expand Up @@ -134,15 +133,23 @@ public MyVeggieDiaryCount selectDiaryCount(MyVeggie myVeggie) {

@Transactional
public void pressLike(Long userId, Long diaryId) {
Optional<DiaryLike> diaryLike = diaryLikeRepository.findDiaryLikeByDiaryIdAndUserId(diaryId, userId);
checkLikeData(diaryLike);
User userData = userService.selectUserById(userId);
Diary diaryData = selectDiaryById(diaryId);
insertLike(userData, diaryData);
}

public void checkLikeData(Optional<DiaryLike> 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);
}

Expand Down Expand Up @@ -189,56 +196,44 @@ 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> diaryComment){
if(diaryComment.isEmpty()){
throw new DiaryCommentNotFoundException("해당 유저는 댓글 삭제 권한이 없습니다.");
}
}

private void addMyyVeggieDiary(
String content,
boolean isOpen,
Expand All @@ -256,7 +251,6 @@ private void addMyyVeggieDiary(
);
myVeggie.addDiary(newDiary);
}

private String getImageUrl(MultipartFile multipartFile) throws IOException {
return s3Service.uploadImage(multipartFile, "dairyimage");
}
Expand Down

0 comments on commit 270219d

Please sign in to comment.