Skip to content

Commit 74a55c0

Browse files
committed
Feat: 게시글 북마크 취소 API 구현
1 parent 1e74ed0 commit 74a55c0

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/main/java/com/back/domain/board/post/controller/PostBookmarkController.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
import lombok.RequiredArgsConstructor;
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.security.core.annotation.AuthenticationPrincipal;
10-
import org.springframework.web.bind.annotation.PathVariable;
11-
import org.springframework.web.bind.annotation.PostMapping;
12-
import org.springframework.web.bind.annotation.RequestMapping;
13-
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.web.bind.annotation.*;
1411

1512
@RestController
1613
@RequestMapping("/api/posts/{postId}/bookmark")
1714
@RequiredArgsConstructor
1815
public class PostBookmarkController {
19-
private final PostBookmarkService bookmarkService;
2016
private final PostBookmarkService postBookmarkService;
2117

2218
// 게시글 북마크
@@ -32,4 +28,18 @@ public ResponseEntity<RsData<PostBookmarkResponse>> bookmarkPost(
3228
response
3329
));
3430
}
31+
32+
// 게시글 북마크 취소
33+
@DeleteMapping
34+
public ResponseEntity<RsData<PostBookmarkResponse>> cancelBookmarkPost(
35+
@PathVariable Long postId,
36+
@AuthenticationPrincipal CustomUserDetails user
37+
) {
38+
PostBookmarkResponse response = postBookmarkService.cancelBookmarkPost(postId, user.getUserId());
39+
return ResponseEntity
40+
.ok(RsData.success(
41+
"게시글 북마크가 취소되었습니다.",
42+
response
43+
));
44+
}
3545
}

src/main/java/com/back/domain/board/post/repository/PostBookmarkRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.Optional;
8+
79
@Repository
810
public interface PostBookmarkRepository extends JpaRepository<PostBookmark, Long> {
911
boolean existsByUserIdAndPostId(Long userId, Long postId);
12+
Optional<PostBookmark> findByUserIdAndPostId(Long userId, Long postId);
1013
}

src/main/java/com/back/domain/board/post/service/PostBookmarkService.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,35 @@ public PostBookmarkResponse bookmarkPost(Long postId, Long userId) {
5050
postBookmarkRepository.save(new PostBookmark(post, user));
5151
return PostBookmarkResponse.from(post);
5252
}
53+
54+
/**
55+
* 게시글 북마크 취소 서비스
56+
* 1. User 조회
57+
* 2. Post 조회
58+
* 3. PostBookmark 조회
59+
* 4. PostBookmark 삭제 및 bookmarkCount 감소
60+
* 5. PostBookmarkResponse 반환
61+
*/
62+
public PostBookmarkResponse cancelBookmarkPost(Long postId, Long userId) {
63+
// User 조회
64+
User user = userRepository.findById(userId)
65+
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
66+
67+
// Post 조회
68+
Post post = postRepository.findById(postId)
69+
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
70+
71+
// PostBookmark 조회
72+
PostBookmark postBookmark = postBookmarkRepository.findByUserIdAndPostId(userId, postId)
73+
.orElseThrow(() -> new CustomException(ErrorCode.BOOKMARK_NOT_FOUND));
74+
75+
// PostBookmark 삭제
76+
postBookmarkRepository.delete(postBookmark);
77+
78+
// 북마크 수 감소
79+
post.decreaseBookmarkCount();
80+
81+
// 응답 반환
82+
return PostBookmarkResponse.from(post);
83+
}
5384
}

0 commit comments

Comments
 (0)