From b79cc396b7512b7342fab64fbcaef4f244b80ca8 Mon Sep 17 00:00:00 2001 From: 5jisoo <56earls@gmail.com> Date: Thu, 10 Aug 2023 01:48:20 +0900 Subject: [PATCH] #7 feat: like story --- .../story/controller/StoryController.java | 12 ++++++++ .../com/umc/place/story/entity/StoryLike.java | 3 +- .../story/repository/StoryLikeRepository.java | 3 ++ .../umc/place/story/service/StoryService.java | 30 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/place/src/main/java/com/umc/place/story/controller/StoryController.java b/place/src/main/java/com/umc/place/story/controller/StoryController.java index c01a137..f338edd 100644 --- a/place/src/main/java/com/umc/place/story/controller/StoryController.java +++ b/place/src/main/java/com/umc/place/story/controller/StoryController.java @@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.*; import static com.umc.place.common.BaseResponseStatus.NULL_STORY; +import static com.umc.place.common.BaseResponseStatus.SUCCESS; @RestController @RequiredArgsConstructor @@ -52,6 +53,17 @@ public BaseResponse uploadStoryComment(@PathVariable Long s } } + @PostMapping("/{storyIdx}/like") + public BaseResponse likeStory(@PathVariable Long storyIdx, + @RequestParam Long userIdx) { + try { + storyService.likeStory(storyIdx, userIdx); + return new BaseResponse<>(SUCCESS); + } catch (BaseException e) { + return new BaseResponse<>(e.getStatus()); + } + } + @GetMapping("/uploadView") public BaseResponse getStoryUploadView(@RequestParam Long userId) { try { diff --git a/place/src/main/java/com/umc/place/story/entity/StoryLike.java b/place/src/main/java/com/umc/place/story/entity/StoryLike.java index 34722b8..e7ae2d1 100644 --- a/place/src/main/java/com/umc/place/story/entity/StoryLike.java +++ b/place/src/main/java/com/umc/place/story/entity/StoryLike.java @@ -30,8 +30,7 @@ public class StoryLike extends BaseEntity { private Story story; @Builder - public StoryLike(Long storyLikeIdx, User user, Story story) { - this.storyLikeIdx = storyLikeIdx; + public StoryLike(User user, Story story) { this.user = user; this.story = story; this.story.getLikes().add(this); // 양방향 연관관계 메서드 diff --git a/place/src/main/java/com/umc/place/story/repository/StoryLikeRepository.java b/place/src/main/java/com/umc/place/story/repository/StoryLikeRepository.java index 8c52fc3..07e5fb2 100644 --- a/place/src/main/java/com/umc/place/story/repository/StoryLikeRepository.java +++ b/place/src/main/java/com/umc/place/story/repository/StoryLikeRepository.java @@ -11,5 +11,8 @@ @Repository public interface StoryLikeRepository extends JpaRepository { Boolean existsByUserAndStory(User user, Story story); + + StoryLike findByUserAndStory(User user, Story story); + List findByUser(User user); } diff --git a/place/src/main/java/com/umc/place/story/service/StoryService.java b/place/src/main/java/com/umc/place/story/service/StoryService.java index 112d9eb..57f6972 100644 --- a/place/src/main/java/com/umc/place/story/service/StoryService.java +++ b/place/src/main/java/com/umc/place/story/service/StoryService.java @@ -10,6 +10,7 @@ import com.umc.place.story.dto.StoryUploadResponseDto; import com.umc.place.story.entity.Story; import com.umc.place.story.entity.StoryHistory; +import com.umc.place.story.entity.StoryLike; import com.umc.place.story.repository.StoryHistoryRepository; import com.umc.place.story.repository.StoryLikeRepository; import com.umc.place.story.repository.StoryRepository; @@ -25,6 +26,8 @@ import java.util.stream.Collectors; import static com.umc.place.common.BaseResponseStatus.*; +import static com.umc.place.common.Constant.ACTIVE; +import static com.umc.place.common.Constant.INACTIVE; @Service @RequiredArgsConstructor @@ -137,4 +140,31 @@ public StoryUploadResponseDto getStoryView(Long userId) throws BaseException { throw new BaseException(DATABASE_ERROR); } } + + @Transactional + public void likeStory(Long storyIdx, Long userIdx) throws BaseException { + try { + User user = userRepository.findById(userIdx).orElseThrow(() -> new BaseException(INVALID_USER_IDX)); + Story story = storyRepository.findById(storyIdx).orElseThrow(() -> new BaseException(INVALID_STORY_IDX)); + + StoryLike storyLike = storyLikeRepository.findByUserAndStory(user, story); + if (storyLike == null) { + StoryLike newLike = StoryLike.builder() + .user(user) + .story(story) + .build(); + storyLikeRepository.save(newLike); + } else { + if (storyLike.getStatus().equals(ACTIVE)) { + storyLike.setStatus(INACTIVE); + } else { + storyLike.getStatus().equals(ACTIVE); + } + } + } catch (BaseException e) { + throw e; + } catch (Exception e) { + throw new BaseException(DATABASE_ERROR); + } + } }