From 7b77acd160cb222c10304fe0393c32a84e05f26a Mon Sep 17 00:00:00 2001 From: Junyong Lee Date: Thu, 9 Jan 2025 15:32:48 +0900 Subject: [PATCH] :sparkles: Like, Unlike, getArticlesThatUserLikes added --- .../article/controller/ArticleController.kt | 31 +++++++++++++ .../persistence/ArticleLikesRepository.kt | 8 +++- .../karrot/article/service/ArticleService.kt | 46 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt b/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt index 1a2881c..0fb31d5 100644 --- a/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt +++ b/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt @@ -46,6 +46,24 @@ class ArticleController( return ResponseEntity.ok("Deleted Successfully") } + @PostMapping("api/item/like/{articleId}") + fun likeArticle( + @PathVariable articleId: Long, + @AuthUser user: User, + ): ResponseEntity { + articleService.likeArticle(articleId, user.id) + return ResponseEntity.ok("Liked Successfully") + } + + @PostMapping("api/item/unlike/{articleId}") + fun unlikeArticle( + @PathVariable articleId: Long, + @AuthUser user: User, + ): ResponseEntity { + articleService.unlikeArticle(articleId, user.id) + return ResponseEntity.ok("Unliked Successfully") + } + @GetMapping("api/item/get/{articleId}") fun getArticle( @PathVariable articleId: Long, @@ -65,6 +83,19 @@ class ArticleController( return ResponseEntity.ok(response) } + @GetMapping("/api/mypage/likes") + fun getArticlesThatUserLikes( + @RequestParam("articleId") articleId: Long, + @AuthUser user: User, + ): ResponseEntity> { + val articles: List = articleService.getArticlesThatUserLikes(user.id, articleId) + val response = + articles.map { article -> + Item.fromArticle(Article.fromEntity(article)) + } + return ResponseEntity.ok(response) + } + @GetMapping("/api/mypage/sells") fun getArticlesBySeller( @RequestParam("articleId") articleId: Long, diff --git a/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleLikesRepository.kt b/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleLikesRepository.kt index 90b4027..2306282 100644 --- a/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleLikesRepository.kt +++ b/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleLikesRepository.kt @@ -1,5 +1,11 @@ package com.toyProject7.karrot.article.persistence +import com.toyProject7.karrot.user.persistence.UserEntity import org.springframework.data.jpa.repository.JpaRepository -interface ArticleLikesRepository : JpaRepository +interface ArticleLikesRepository : JpaRepository { + fun findTop10ByUserAndArticleIdLessThanOrderByArticleIdDesc( + user: UserEntity, + articleId: Long, + ): List +} diff --git a/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt b/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt index 46a533b..bdf8ff9 100644 --- a/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt +++ b/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt @@ -5,6 +5,8 @@ import com.toyProject7.karrot.article.ArticlePermissionDeniedException import com.toyProject7.karrot.article.controller.Article import com.toyProject7.karrot.article.controller.PostArticleRequest import com.toyProject7.karrot.article.persistence.ArticleEntity +import com.toyProject7.karrot.article.persistence.ArticleLikesEntity +import com.toyProject7.karrot.article.persistence.ArticleLikesRepository import com.toyProject7.karrot.article.persistence.ArticleRepository import com.toyProject7.karrot.user.service.UserService import org.springframework.data.repository.findByIdOrNull @@ -15,6 +17,7 @@ import java.time.Instant @Service class ArticleService( private val articleRepository: ArticleRepository, + private val articleLikesRepository: ArticleLikesRepository, private val userService: UserService, ) { @Transactional @@ -73,6 +76,37 @@ class ArticleService( articleRepository.delete(articleEntity) } + @Transactional + fun likeArticle( + articleId: Long, + id: String, + ) { + val articleEntity = articleRepository.findByIdOrNull(articleId) ?: throw ArticleNotFoundException() + val userEntity = userService.getUserEntityById(id) + if (articleEntity.articleLikes.any { it.user.id == userEntity.id }) { + return + } + val articleLikesEntity = + articleLikesRepository.save( + ArticleLikesEntity(article = articleEntity, user = userEntity, createdAt = Instant.now(), updatedAt = Instant.now()), + ) + articleEntity.articleLikes += articleLikesEntity + articleRepository.save(articleEntity) + } + + @Transactional + fun unlikeArticle( + articleId: Long, + id: String, + ) { + val articleEntity = articleRepository.findByIdOrNull(articleId) ?: throw ArticleNotFoundException() + val userEntity = userService.getUserEntityById(id) + val toBeRemoved: ArticleLikesEntity = articleEntity.articleLikes.find { it.user.id == userEntity.id } ?: return + articleEntity.articleLikes -= toBeRemoved + articleLikesRepository.delete(toBeRemoved) + articleRepository.save(articleEntity) + } + @Transactional fun getArticle(articleId: Long): Article { val articleEntity = articleRepository.findByIdOrNull(articleId) ?: throw ArticleNotFoundException() @@ -84,6 +118,18 @@ class ArticleService( return articleRepository.findTop10ByIdBeforeOrderByCreatedAtDesc(articleId) } + @Transactional + fun getArticlesThatUserLikes( + id: String, + articleId: Long, + ): List { + val userEntity = userService.getUserEntityById(id) + return articleLikesRepository.findTop10ByUserAndArticleIdLessThanOrderByArticleIdDesc( + userEntity, + articleId, + ).map { it.article } + } + @Transactional fun getArticlesBySeller( id: String,