Skip to content

Commit

Permalink
Merge pull request #26 from PLACE-4th-UMC/feature/7-storyLike
Browse files Browse the repository at this point in the history
[feature/7-storyLike] 스토리 좋아요 누르기
  • Loading branch information
5jisoo authored Aug 10, 2023
2 parents 0ae3d34 + b095fc3 commit 20b9ae4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +67,17 @@ public BaseResponse<CommentUploadResDto> uploadStoryComment(@PathVariable Long s
}
}

@PostMapping("/{storyIdx}/like")
public BaseResponse<Void> 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<StoryUploadResponseDto> getStoryUploadView(@RequestParam Long userId) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 양방향 연관관계 메서드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
@Repository
public interface StoryLikeRepository extends JpaRepository<StoryLike, Long> {
Boolean existsByUserAndStory(User user, Story story);

StoryLike findByUserAndStory(User user, Story story);

List<StoryLike> findByUser(User user);
}
30 changes: 30 additions & 0 deletions place/src/main/java/com/umc/place/story/service/StoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 20b9ae4

Please sign in to comment.