Skip to content

Commit

Permalink
Merge pull request #35 from MEOGO-DSM/34-create-delete-comment
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-34) create delete comment
  • Loading branch information
meltapplee authored Sep 16, 2024
2 parents fb8557d + dead928 commit f54b68b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/main/kotlin/org/meogo/domain/comment/domain/Comment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +14,6 @@ import javax.persistence.ManyToOne

@Entity
class Comment(

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
val id: Long = 0,
Expand All @@ -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

)
Original file line number Diff line number Diff line change
@@ -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<Comment, Long>
interface CommentRepository : Repository<Comment, Long> {
fun save(comment: Comment)

fun findById(id: Long): Comment?
fun deleteById(id: Long)
}
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
)
)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

Expand Down

0 comments on commit f54b68b

Please sign in to comment.