Skip to content

Commit

Permalink
Merge pull request #9 from gloddy-dev/feat/#2
Browse files Browse the repository at this point in the history
[FEAT]: �댓글 관련 API 구현
  • Loading branch information
jihwan2da authored Jan 23, 2024
2 parents 7c7a483 + 5694219 commit 0066f52
Show file tree
Hide file tree
Showing 79 changed files with 1,420 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gloddy.article.port.`in`

import gloddy.article.port.`in`.dto.command.ArticleCreateRequest
import gloddy.article.port.`in`.dto.command.ArticleUpsertCommentRequest
import gloddy.article.port.`in`.dto.read.ArticleCreateResponse

interface ArticleCommandUseCase {
fun create(userId: Long, command: ArticleCreateRequest): ArticleCreateResponse
fun delete(userId: Long, articleId: Long)
fun upsertLike(userId: Long, articleId: Long)
fun upsertComment(request: ArticleUpsertCommentRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gloddy.article.port.`in`.dto.command

data class ArticleUpsertCommentRequest(
val articleId: Long,
val status: CommentStatus
)

enum class CommentStatus {
CREATE, DELETE
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import gloddy.article.ArticleLike
import gloddy.article.port.`in`.dto.command.ArticleCreateRequest
import gloddy.article.port.`in`.dto.read.ArticleCreateResponse
import gloddy.article.port.`in`.ArticleCommandUseCase
import gloddy.article.port.`in`.dto.command.ArticleUpsertCommentRequest
import gloddy.article.port.`in`.dto.command.CommentStatus
import gloddy.article.port.out.ArticleCommandPersistencePort
import gloddy.article.port.out.ArticleLikeCommandPersistencePort
import gloddy.article.port.out.ArticleLikeQueryPersistencePort
Expand All @@ -19,7 +21,6 @@ class ArticleCommandService(
private val categoryQueryPersistencePort: CategoryQueryPersistencePort,
private val articleQueryPersistencePort: ArticleQueryPersistencePort,
private val articleCommandPersistencePort: ArticleCommandPersistencePort,
private val articleLikeCommandPersistencePort: ArticleLikeCommandPersistencePort,
private val articleLikeQueryPersistencePort: ArticleLikeQueryPersistencePort,
) : ArticleCommandUseCase {

Expand Down Expand Up @@ -60,4 +61,12 @@ class ArticleCommandService(
article = article.like()
)
}

override fun upsertComment(request: ArticleUpsertCommentRequest) {
val article = articleQueryPersistencePort.findById(request.articleId)
when(request.status) {
CommentStatus.CREATE -> articleCommandPersistencePort.save(article.comment())
CommentStatus.DELETE -> articleCommandPersistencePort.save(article.unComment())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ArticleQueryService(
id = request.id,
userId = request.userId
)
val userPreviewUnit = userQueryPort.getUserPreviewUnit(request.userId)
val userPreviewUnit = userQueryPort.getUserPreviewUnit(articleDetailUnit.userId)
return ArticleDetailResponse(
article = articleDetailUnit,
writer = userPreviewUnit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gloddy.comment.dto

data class ChildCommentGetRequest(
val parentId: Long,
val userId: Long
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gloddy.comment.dto

import gloddy.article.port.`in`.dto.command.CommentStatus
import gloddy.core.CommentId

data class CommentChildUpsertRequest(
val commentId: CommentId,
val status: CommentStatus
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gloddy.comment.dto

import gloddy.core.ArticleId
import gloddy.core.CommentId
import gloddy.core.UserId

data class ParentCommentCreateRequest(
val userId: UserId,
val articleId: ArticleId,
val content: String
)

data class ChildCommentCreateRequest(
val userId: UserId,
val articleId: ArticleId,
val parentCommentId: CommentId,
val content: String
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gloddy.comment.dto

import gloddy.comment.Comment

data class CommentCreateResponse(
val commentId: Long
) {
constructor(comment: Comment) : this(
commentId = comment.id!!.value
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gloddy.comment.dto

import gloddy.core.ArticleId
import gloddy.core.CommentId
import gloddy.core.UserId

data class CommentDeleteRequest(
val commentId: CommentId,
val userId: UserId,
val articleId: ArticleId
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gloddy.comment.dto

import gloddy.comment.Comment

data class CommentGetResponse(
val comments: List<CommentUnit>
)

data class CommentUnit(
val commentId: Long,
val content: String,
val depth: Int,
val likeCount: Long,
val commentCount: Long,
val isLiked: Boolean,
val user: UserUnit
)

data class UserUnit(
val userId: Long,
val name: String,
val imageUrl: String,
val reliabilityLevel: String,
val country: UserCountry
)

data class UserCountry(
val name: String,
val imageUrl: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gloddy.comment.dto

import gloddy.core.CommentId
import gloddy.core.UserId

data class CommentLikeUpsertRequest(
val userId: UserId,
val commentId: CommentId
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gloddy.comment.dto

import gloddy.core.ArticleId
import gloddy.core.UserId

data class ParentCommentGetRequest(
val userId: UserId,
val articleId: ArticleId
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gloddy.comment.dto.readModel

import java.time.LocalDateTime

data class ChildCommentUnit(
val id: Long,
val isWriter: Boolean,
val isLiked: Boolean,
val userId: Long,
val articleId: Long,
val parentId: Long,
val content: String,
val likeCount: Int,
val createdAt: LocalDateTime,
val updatedAt: LocalDateTime
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gloddy.comment.dto.readModel

import gloddy.user.port.`in`.dto.UserPreviewUnit

data class FindChildCommentByParentIdResponse(
val childComment: ChildCommentUnit,
val writer: UserPreviewUnit
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gloddy.comment.dto.readModel

data class FindChildCommentsByParentIdResponse(
val childComments: List<FindChildCommentByParentIdResponse>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gloddy.comment.dto.readModel

import gloddy.user.port.`in`.dto.UserPreviewUnit

data class FindParentCommentByArticleIdResponse(
val comment: ParentCommentUnit,
val writer: UserPreviewUnit
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gloddy.comment.dto.readModel

data class FindParentCommentsByArticleIdResponse(
val comments: List<FindParentCommentByArticleIdResponse>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gloddy.comment.dto.readModel

import java.time.LocalDateTime

data class ParentCommentUnit(
val id: Long,
val isWriter: Boolean,
val isLiked: Boolean,
val userId: Long,
val articleId: Long,
val content: String,
val likeCount: Int,
val commentCount: Int,
val createdAt: LocalDateTime,
val updatedAt: LocalDateTime,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gloddy.comment.port.out

import gloddy.comment.Comment
import gloddy.comment.CommentLike

interface CommentCommandPort {
fun save(comment: Comment): Comment
fun delete(comment: Comment)
fun upsertLike(commentLike: CommentLike, comment: Comment)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gloddy.comment.port.out

import gloddy.comment.CommentLike

interface CommentLikeCommandPort {
fun save(commentLike: CommentLike): CommentLike
fun delete(commentLike: CommentLike)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gloddy.comment.port.out

import gloddy.comment.CommentLike
import gloddy.core.CommentId
import gloddy.core.UserId

interface CommentLikeQueryPort {
fun findByCommentIdAndUserIdOrNull(commentId: CommentId, userId: UserId): CommentLike?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gloddy.comment.port.out

import gloddy.comment.Comment
import gloddy.comment.dto.readModel.ChildCommentUnit
import gloddy.comment.dto.readModel.ParentCommentUnit
import gloddy.core.CommentId


interface CommentQueryPort {
fun findById(id: CommentId): Comment
fun findParentComments(articleId: Long, userId: Long): List<ParentCommentUnit>
fun findChildComments(parentId: Long, userId: Long): List<ChildCommentUnit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gloddy.comment.service

import gloddy.article.port.out.ArticleQueryPersistencePort
import gloddy.comment.Comment
import gloddy.comment.dto.ChildCommentCreateRequest
import gloddy.comment.dto.CommentCreateResponse
import gloddy.comment.dto.ParentCommentCreateRequest
import gloddy.comment.port.out.CommentCommandPort
import gloddy.comment.port.out.CommentQueryPort
import org.springframework.stereotype.Service

@Service
class CommentCreateService(
private val commentCommandPort: CommentCommandPort,
private val articleQueryPort: ArticleQueryPersistencePort,
) {
fun createParent(request: ParentCommentCreateRequest): CommentCreateResponse {
val article = articleQueryPort.findById(request.articleId.value)

return Comment.parent(
userId = request.userId,
article = article,
content = request.content
)
.let {
commentCommandPort.save(it)
}
.run {
CommentCreateResponse(this)
}
}

fun createChild(request: ChildCommentCreateRequest): CommentCreateResponse {
val article = articleQueryPort.findById(request.articleId.value)

return Comment.child(
userId = request.userId,
article = article,
content = request.content,
parentCommentId = request.parentCommentId
)
.let {
commentCommandPort.save(it)
}
.run {
CommentCreateResponse(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gloddy.comment.service

import gloddy.comment.dto.CommentDeleteRequest
import gloddy.comment.port.out.CommentCommandPort
import gloddy.comment.port.out.CommentQueryPort
import org.springframework.stereotype.Service

@Service
class CommentDeleteService(
private val commentCommandPort: CommentCommandPort,
private val commentQueryPort: CommentQueryPort,
) {

fun delete(request: CommentDeleteRequest) {
val comment = commentQueryPort.findById(request.commentId)
commentCommandPort.delete(comment.delete(request.userId))
}
}
Loading

0 comments on commit 0066f52

Please sign in to comment.