-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from MEOGO-DSM/34-create-delete-comment
🏄 :: (Meogo-34) create delete comment
- Loading branch information
Showing
8 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
src/main/kotlin/org/meogo/domain/comment/domain/CommentRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/org/meogo/domain/comment/exception/CommentNotFoundException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/org/meogo/domain/comment/presentation/dto/request/CommentRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/org/meogo/domain/comment/service/CreateCommentService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/org/meogo/domain/comment/service/DeleteCommentService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters