Skip to content

Commit 07b9e4a

Browse files
committed
댓글 기능 추가
1 parent 1f63c14 commit 07b9e4a

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

src/main/kotlin/io/devchw/coderabbit/BoardController.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import org.springframework.web.bind.annotation.RequestBody
1111
import org.springframework.web.bind.annotation.RequestMapping
1212
import org.springframework.web.bind.annotation.RestController
1313
import java.util.concurrent.atomic.AtomicLong
14+
import org.springframework.beans.factory.annotation.Autowired
1415

1516
/**
1617
* @author DevCHW
1718
* @since 2025-06-05
1819
*/
1920
@RestController
2021
@RequestMapping("/api/board")
21-
class BoardController {
22+
class BoardController(
23+
@Autowired private val commentController: CommentController
24+
) {
2225

2326
private val posts = mutableMapOf<Long, Post>()
2427
private val idGenerator = AtomicLong(1)
@@ -98,6 +101,10 @@ class BoardController {
98101
if (!posts.containsKey(id)) {
99102
return ResponseEntity.status(HttpStatus.NOT_FOUND).build()
100103
}
104+
105+
// 게시물과 관련된 모든 댓글 삭제
106+
commentController.deleteAllCommentsForPost(id)
107+
101108
posts.remove(id)
102109
return ResponseEntity.status(HttpStatus.NO_CONTENT).build()
103110
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.devchw.coderabbit
2+
3+
/**
4+
* @author DevCHW
5+
* @since 2025-06-05
6+
*/
7+
data class Comment(
8+
var id: Long? = null,
9+
var postId: Long,
10+
var content: String
11+
)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.devchw.coderabbit
2+
3+
import org.springframework.http.HttpStatus
4+
import org.springframework.http.ResponseEntity
5+
import org.springframework.web.bind.annotation.*
6+
import java.util.concurrent.atomic.AtomicLong
7+
8+
/**
9+
* @author DevCHW
10+
* @since 2025-06-05
11+
*/
12+
@RestController
13+
@RequestMapping("/api/board")
14+
class CommentController {
15+
16+
private val comments = mutableMapOf<Long, Comment>()
17+
private val idGenerator = AtomicLong(1)
18+
19+
// 댓글 생성
20+
@PostMapping("/{postId}/comments")
21+
fun createComment(
22+
@PathVariable postId: Long,
23+
@RequestBody comment: Comment
24+
): ResponseEntity<Comment> {
25+
val id = idGenerator.getAndIncrement()
26+
comment.id = id
27+
comment.postId = postId
28+
comments[id] = comment
29+
return ResponseEntity.status(HttpStatus.CREATED).body(comment)
30+
}
31+
32+
// 게시물의 모든 댓글 조회
33+
@GetMapping("/{postId}/comments")
34+
fun getCommentsByPost(@PathVariable postId: Long): ResponseEntity<List<Comment>> {
35+
val postComments = comments.values.filter { it.postId == postId }
36+
return ResponseEntity.ok(postComments)
37+
}
38+
39+
// 댓글 상세 조회
40+
@GetMapping("/comments/{commentId}")
41+
fun getComment(@PathVariable commentId: Long): ResponseEntity<Comment> {
42+
val comment = comments[commentId]
43+
return if (comment != null) {
44+
ResponseEntity.ok(comment)
45+
} else {
46+
ResponseEntity.status(HttpStatus.NOT_FOUND).build()
47+
}
48+
}
49+
50+
// 댓글 수정
51+
@PutMapping("/comments/{commentId}")
52+
fun updateComment(
53+
@PathVariable commentId: Long,
54+
@RequestBody updatedComment: Comment
55+
): ResponseEntity<Comment> {
56+
val existingComment = comments[commentId]
57+
if (existingComment == null) {
58+
return ResponseEntity.status(HttpStatus.NOT_FOUND).build()
59+
}
60+
61+
// 기존 postId 유지
62+
updatedComment.id = commentId
63+
updatedComment.postId = existingComment.postId
64+
comments[commentId] = updatedComment
65+
return ResponseEntity.ok(updatedComment)
66+
}
67+
68+
// 댓글 삭제
69+
@DeleteMapping("/comments/{commentId}")
70+
fun deleteComment(@PathVariable commentId: Long): ResponseEntity<Void> {
71+
if (!comments.containsKey(commentId)) {
72+
return ResponseEntity.status(HttpStatus.NOT_FOUND).build()
73+
}
74+
comments.remove(commentId)
75+
return ResponseEntity.status(HttpStatus.NO_CONTENT).build()
76+
}
77+
78+
// 게시물의 모든 댓글 삭제 (게시물이 삭제될 때 호출)
79+
fun deleteAllCommentsForPost(postId: Long) {
80+
val commentIdsToRemove = comments.values
81+
.filter { it.postId == postId }
82+
.mapNotNull { it.id }
83+
84+
commentIdsToRemove.forEach { comments.remove(it) }
85+
}
86+
}

src/main/kotlin/io/devchw/coderabbit/Post.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ package io.devchw.coderabbit
77
data class Post(
88
var id: Long? = null,
99
var title: String,
10-
var content: String
10+
var content: String,
11+
var comments: MutableList<Comment> = mutableListOf()
1112
)

0 commit comments

Comments
 (0)