diff --git a/src/main/kotlin/org/meogo/domain/comment/domain/Comment.kt b/src/main/kotlin/org/meogo/domain/comment/domain/Comment.kt index 25e2051..dcc3b3f 100644 --- a/src/main/kotlin/org/meogo/domain/comment/domain/Comment.kt +++ b/src/main/kotlin/org/meogo/domain/comment/domain/Comment.kt @@ -3,7 +3,6 @@ package org.meogo.domain.comment.domain import org.meogo.domain.post.domain.Post import org.meogo.domain.user.domain.User import java.time.LocalDateTime -import javax.persistence.CascadeType.REMOVE import javax.persistence.Column import javax.persistence.Entity import javax.persistence.FetchType @@ -15,7 +14,6 @@ import javax.persistence.ManyToOne @Entity class Comment( - @GeneratedValue(strategy = GenerationType.IDENTITY) @Id val id: Long = 0, @@ -26,12 +24,11 @@ class Comment( @Column(nullable = false) val date: LocalDateTime, - @ManyToOne(cascade = [REMOVE], fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_id", nullable = false) val post: Post, - @ManyToOne(cascade = [REMOVE], fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) val user: User - ) diff --git a/src/main/kotlin/org/meogo/domain/comment/domain/CommentRepository.kt b/src/main/kotlin/org/meogo/domain/comment/domain/CommentRepository.kt index d9b5f97..01a88d0 100644 --- a/src/main/kotlin/org/meogo/domain/comment/domain/CommentRepository.kt +++ b/src/main/kotlin/org/meogo/domain/comment/domain/CommentRepository.kt @@ -1,5 +1,10 @@ package org.meogo.domain.comment.domain -import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.repository.Repository -interface CommentRepository : JpaRepository +interface CommentRepository : Repository { + fun save(comment: Comment) + + fun findById(id: Long): Comment? + fun deleteById(id: Long) +} diff --git a/src/main/kotlin/org/meogo/domain/comment/exception/CommentNotFoundException.kt b/src/main/kotlin/org/meogo/domain/comment/exception/CommentNotFoundException.kt new file mode 100644 index 0000000..f2f2b9f --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/comment/exception/CommentNotFoundException.kt @@ -0,0 +1,8 @@ +package org.meogo.domain.comment.exception + +import org.meogo.global.error.exception.ErrorCode +import org.meogo.global.error.exception.MeogoException + +object CommentNotFoundException : MeogoException( + ErrorCode.COMMENT_NOT_FOUND +) diff --git a/src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt b/src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt new file mode 100644 index 0000000..be44918 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt @@ -0,0 +1,35 @@ +package org.meogo.domain.comment.presentation + +import lombok.RequiredArgsConstructor +import org.meogo.domain.comment.presentation.dto.request.CommentRequest +import org.meogo.domain.comment.service.CreateCommentService +import org.meogo.domain.comment.service.DeleteCommentService +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.bind.annotation.RestController +import javax.validation.Valid + +@RequiredArgsConstructor +@RestController +@RequestMapping("/comment") +class CommentController( + private val createCommentService: CreateCommentService, + private val deleteCommentService: DeleteCommentService +) { + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + fun create( + @Valid @RequestBody + request: CommentRequest + ) = createCommentService.execute(request) + + @DeleteMapping + @ResponseStatus(HttpStatus.NO_CONTENT) + fun delete(@RequestParam(name = "comment_id")id: Long) = + deleteCommentService.execute(id) +} diff --git a/src/main/kotlin/org/meogo/domain/comment/presentation/dto/request/CommentRequest.kt b/src/main/kotlin/org/meogo/domain/comment/presentation/dto/request/CommentRequest.kt new file mode 100644 index 0000000..2f432df --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/comment/presentation/dto/request/CommentRequest.kt @@ -0,0 +1,9 @@ +package org.meogo.domain.comment.presentation.dto.request + +import javax.validation.constraints.Size + +data class CommentRequest( + @field: Size(min = 1, max = 100) + val content: String, + val postId: Long +) diff --git a/src/main/kotlin/org/meogo/domain/comment/service/CreateCommentService.kt b/src/main/kotlin/org/meogo/domain/comment/service/CreateCommentService.kt new file mode 100644 index 0000000..f64b2e8 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/comment/service/CreateCommentService.kt @@ -0,0 +1,34 @@ +package org.meogo.domain.comment.service + +import org.meogo.domain.comment.domain.Comment +import org.meogo.domain.comment.domain.CommentRepository +import org.meogo.domain.comment.presentation.dto.request.CommentRequest +import org.meogo.domain.post.domain.PostRepository +import org.meogo.domain.post.exception.PostNotFoundException +import org.meogo.domain.user.facade.UserFacade +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import java.time.LocalDateTime + +@Service +class CreateCommentService( + private val commentRepository: CommentRepository, + private val postRepository: PostRepository, + private val userFacade: UserFacade +) { + + @Transactional + fun execute(request: CommentRequest) { + val user = userFacade.currentUser()!! + val post = postRepository.findById(request.postId) ?: throw PostNotFoundException + + commentRepository.save( + Comment( + content = request.content, + date = LocalDateTime.now(), + post = post, + user = user + ) + ) + } +} diff --git a/src/main/kotlin/org/meogo/domain/comment/service/DeleteCommentService.kt b/src/main/kotlin/org/meogo/domain/comment/service/DeleteCommentService.kt new file mode 100644 index 0000000..f43a1a3 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/comment/service/DeleteCommentService.kt @@ -0,0 +1,26 @@ +package org.meogo.domain.comment.service + +import org.meogo.domain.comment.domain.CommentRepository +import org.meogo.domain.comment.exception.CommentNotFoundException +import org.meogo.domain.user.exception.UserMisMatchException +import org.meogo.domain.user.facade.UserFacade +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class DeleteCommentService( + private val commentRepository: CommentRepository, + private val userFacade: UserFacade +) { + @Transactional + fun execute(commentId: Long) { + val user = userFacade.currentUser()!! + val comment = commentRepository.findById(commentId) ?: throw CommentNotFoundException + + if (user.id != comment.user.id) { + throw UserMisMatchException + } + + commentRepository.deleteById(comment.id) + } +} diff --git a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt index 1cd2167..ed9c0a9 100644 --- a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt +++ b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt @@ -15,6 +15,7 @@ enum class ErrorCode( USER_NOT_FOUND(404, "User not found"), REVIEW_NOT_FOUND(404, "Review not found"), POST_NOT_FOUND(404, "Post not found"), + COMMENT_NOT_FOUND(404, "Comment not found"), ALREADY_WRITE_EXCEPTION(409, "You have already submitted a review"),