|
| 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 | +} |
0 commit comments