Skip to content

Commit

Permalink
fix: 게시글 좋아요 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jihwan2da committed Jan 21, 2024
1 parent 86d50a9 commit d87bc2e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import gloddy.article.port.`in`.dto.read.ArticleCreateResponse
interface ArticleCommandUseCase {
fun create(userId: Long, command: ArticleCreateRequest): ArticleCreateResponse
fun delete(userId: Long, articleId: Long)
fun like(userId: Long, articleId: Long)
fun upsertLike(userId: Long, articleId: Long)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gloddy.article.port.out

import gloddy.article.Article
import gloddy.article.ArticleLike

interface ArticleCommandPersistencePort {
fun save(article: Article) : Article
fun delete(id: Long)
fun upsertLike(articleLike: ArticleLike, article: Article)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class ArticleCommandService(
private val articleQueryPersistencePort: ArticleQueryPersistencePort,
private val articleCommandPersistencePort: ArticleCommandPersistencePort,
private val articleLikeCommandPersistencePort: ArticleLikeCommandPersistencePort,
private val articleLikeQueryPersistencePort: ArticleLikeQueryPersistencePort
private val articleLikeQueryPersistencePort: ArticleLikeQueryPersistencePort,
) : ArticleCommandUseCase {

override fun create(userId: Long, command: ArticleCreateRequest) : ArticleCreateResponse {
override fun create(userId: Long, command: ArticleCreateRequest): ArticleCreateResponse {

val category = categoryQueryPersistencePort.findById(CategoryId(command.categoryId))

Expand All @@ -43,17 +43,21 @@ class ArticleCommandService(
articleCommandPersistencePort.delete(article.id!!.value)
}

override fun like(userId: Long, articleId: Long) {

override fun upsertLike(userId: Long, articleId: Long) {
val article = articleQueryPersistencePort.findById(articleId)

articleLikeQueryPersistencePort.findByUserIdAndArticleOrNull(userId, article)
?.run { articleLikeCommandPersistencePort.delete(this) }
?: articleLikeCommandPersistencePort.save(
ArticleLike(
?.run {
articleCommandPersistencePort.upsertLike(
articleLike = this,
article = article.unlike()
)
}
?: articleCommandPersistencePort.upsertLike(
articleLike = ArticleLike(
userId = UserId(userId),
article = article
)
),
article = article.like()
)
}
}
22 changes: 16 additions & 6 deletions community-domain/src/main/kotlin/gloddy/article/Article.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import gloddy.core.UserId

data class Article(
val userId: UserId,
var category: Category,
var title: String,
var content: String,
var image: ArticleImage,
var commentCount: Int = 0,
var likeCount: Int = 0,
val category: Category,
val title: String,
val content: String,
val image: ArticleImage,
val commentCount: Int = 0,
val likeCount: Int = 0,
val id: ArticleId? = null,
) {
constructor(
Expand All @@ -37,4 +37,14 @@ data class Article(
throw ArticleNoAuthorizationException()
}
}

fun like(): Article =
this.copy(
likeCount = this.likeCount + 1
)

fun unlike(): Article =
this.copy(
likeCount = this.likeCount - 1
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ArticleCommandController(
@PostMapping("/articles/{articleId}/like")
override fun like(@RequestHeader("USER_ID") userId: Long, @PathVariable("articleId") articleId: Long)
: ResponseEntity<CommunityApiResponse<Nothing>> {
articleCommandUseCase.like(userId, articleId)
articleCommandUseCase.upsertLike(userId, articleId)
return ApiResponseEntityWrapper<Nothing>().noContent()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gloddy.persistence.article.adapter

import gloddy.article.Article
import gloddy.article.ArticleLike
import gloddy.article.exception.ArticleNotFoundException
import gloddy.article.port.out.ArticleCommandPersistencePort
import gloddy.persistence.article.ArticleJpaEntity
import gloddy.persistence.article.repository.ArticleJpaRepository
import gloddy.persistence.article.repository.ArticleLikeJpaRepository
import gloddy.persistence.util.mapper.toDomain
import gloddy.persistence.util.mapper.toEntity
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -14,7 +16,8 @@ import org.springframework.transaction.annotation.Transactional
@Component
@Transactional
class ArticleCommandPersistenceAdapter(
private val articleJpaRepository: ArticleJpaRepository
private val articleJpaRepository: ArticleJpaRepository,
private val articleLikeJpaRepository: ArticleLikeJpaRepository
) : ArticleCommandPersistencePort {

override fun save(article: Article): Article {
Expand All @@ -26,6 +29,18 @@ class ArticleCommandPersistenceAdapter(
articleJpaEntity.changeDeletedToTrue()
}

override fun upsertLike(articleLike: ArticleLike, article: Article) {
save(article)
articleLike
.run {
if (articleLike.id == null) {
articleLikeJpaRepository.save(this.toEntity())
} else {
articleLikeJpaRepository.delete(this.toEntity())
}
}
}

fun find(id: Long): ArticleJpaEntity {
return articleJpaRepository.findByIdOrNull(id) ?: throw ArticleNotFoundException()
}
Expand Down

0 comments on commit d87bc2e

Please sign in to comment.