TIL 23.01.05

쓰옹·2023년 1월 5일
0

개발자를 향해~~TIL✍

목록 보기
58/87

팀프로젝트

좋아요 API 분리

하나로 되어있는걸 나눴다.
원래 post로만 되어있는 걸 post와 delete로 나눴다.

원래에서는 좋아요를 한 사용자인지 아닌지 확인을 하고 했으면 좋아요 취소/안했으면 좋아요 가 되게 로직을 짰는데 두개를 나누고 나니까
중복의 가능성도 있고 해서
좋아요를 한 사용자가 post를 다시 했을 때 중복이 되지 않도록 에러를 보내고
좋아요를 하지 않은 사용자가 delete를 했을 때 좋아요를 하지 않았다고 에러를 보내야했다.
레파지토리에서 optional<>로 해야했나...? 그게 더 나았으려나

	@PostMapping("/posts/{postId}/like")
    @Operation(summary = "Like post", description = "Like post Page")
    public ResponseEntity<String> likePost(@PathVariable Long postId, @AuthenticationPrincipal UserDetailsImpl userDetails) {
        String username = userDetails.getUsername();
        PostLike postLike = postLikeRepository.findByUsernameAndPostId(username, postId);
        if (postLike == null) {
            return postService.likePost(postId, username);
        } else {
            throw new IllegalArgumentException("You already did like on the post");
        }
    }

    @DeleteMapping("/posts/{postId}/like")
    @Operation(summary = "Cancel liked post", description = "Cancel liked post Page")
    public ResponseEntity<String> cancelLikedPost(@PathVariable Long postId, @AuthenticationPrincipal UserDetailsImpl userDetails) {
        String username = userDetails.getUsername();
        PostLike postLike = postLikeRepository.findByUsernameAndPostId(username, postId);
        if (postLike != null) {
            return postService.cancelLikedPost(postId, username);
        } else {
            throw new IllegalArgumentException("You didn't like on the post");
        }
    }

프로젝트 마무리

하고 발표자료 정리하고 정리하고

아 근데 git너무 어렵다..
브랜치 나눠서 협업하고 머지하고 오류나고 어떻게 써야할 지 모르겠고..어렵다
공부해야겠다.

profile
기록하자기록해!

0개의 댓글