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 bc6cd8c..f9fdfd2 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 @@ -16,6 +16,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 @@ -66,6 +67,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 99495e8..3809497 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 @@ -11,6 +11,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; @@ -28,6 +29,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 @@ -153,4 +156,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); + } + } }