From c67edd30bc942ece6958535809030c23c4534689 Mon Sep 17 00:00:00 2001 From: swdevsw98 <102571918+swdevsw98@users.noreply.github.com> Date: Tue, 26 Dec 2023 16:56:32 +0900 Subject: [PATCH] =?UTF-8?q?[#82]=20=EC=8A=A4=ED=81=AC=EB=9E=A9=ED=95=9C=20?= =?UTF-8?q?=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat : 내가 스크랩한 글 조회 * feat : 내가 스크랩한 글 조회 테스트 --- .../model/repository/ScrapRepository.kt | 3 ++ .../myPage/controller/MyPageController.kt | 14 ++++++ .../myPage/dto/response/MyScrapResponse.kt | 12 ++++++ .../domain/myPage/service/MyPageService.kt | 19 ++++++++ .../myPage/controller/MyPageControllerTest.kt | 25 ++++++++++- .../mypage/service/MypageServiceTest.kt | 43 ++++++++++++++++++- 6 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/com/example/daitssuapi/domain/myPage/dto/response/MyScrapResponse.kt diff --git a/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ScrapRepository.kt b/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ScrapRepository.kt index 08f9ba86..cb7d327b 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ScrapRepository.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ScrapRepository.kt @@ -1,10 +1,13 @@ package com.example.daitssuapi.domain.article.model.repository import com.example.daitssuapi.domain.article.model.entity.Scrap +import com.example.daitssuapi.domain.user.model.entity.User import org.springframework.data.jpa.repository.JpaRepository interface ScrapRepository : JpaRepository { fun findByArticleIdAndUserId(articleId: Long, userId: Long): Scrap? fun findByArticleIdAndIsActiveTrue(articleId: Long): List + + fun findByUserAndIsActiveTrueOrderByCreatedAtDesc(user: User): List } diff --git a/src/main/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageController.kt b/src/main/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageController.kt index ec63d631..57c81138 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageController.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageController.kt @@ -5,6 +5,7 @@ import com.example.daitssuapi.common.security.component.ArgumentResolver import com.example.daitssuapi.domain.article.dto.response.CommentResponse import com.example.daitssuapi.domain.myPage.dto.request.CommentDeleteRequest import com.example.daitssuapi.domain.myPage.dto.response.MyArticleResponse +import com.example.daitssuapi.domain.myPage.dto.response.MyScrapResponse import com.example.daitssuapi.domain.myPage.service.MyPageService import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.responses.ApiResponse @@ -57,4 +58,17 @@ class MyPageController( return Response(data = myPageService.getMyArticle(userId = userId)) } + + @Operation( + summary = "내가 스크랩한 글 조회", + responses = [ + ApiResponse(responseCode = "200", description = "OK") + ] + ) + @GetMapping("/scraps") + fun getMyScraps() : Response> { + val userId = argumentResolver.resolveUserId() + + return Response(data = myPageService.getMyScrap(userId = userId)) + } } diff --git a/src/main/kotlin/com/example/daitssuapi/domain/myPage/dto/response/MyScrapResponse.kt b/src/main/kotlin/com/example/daitssuapi/domain/myPage/dto/response/MyScrapResponse.kt new file mode 100644 index 00000000..f38e8ddc --- /dev/null +++ b/src/main/kotlin/com/example/daitssuapi/domain/myPage/dto/response/MyScrapResponse.kt @@ -0,0 +1,12 @@ +package com.example.daitssuapi.domain.myPage.dto.response + +import java.time.LocalDateTime + +data class MyScrapResponse ( + val id: Long, + val topic: String, + val title: String, + val content: String, + val createdAt: LocalDateTime, + val commentSize: Int +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/daitssuapi/domain/myPage/service/MyPageService.kt b/src/main/kotlin/com/example/daitssuapi/domain/myPage/service/MyPageService.kt index 24e34b42..26e90386 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/myPage/service/MyPageService.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/myPage/service/MyPageService.kt @@ -5,7 +5,9 @@ import com.example.daitssuapi.common.exception.DefaultException import com.example.daitssuapi.domain.article.dto.response.CommentResponse import com.example.daitssuapi.domain.article.model.repository.ArticleRepository import com.example.daitssuapi.domain.article.model.repository.CommentRepository +import com.example.daitssuapi.domain.article.model.repository.ScrapRepository import com.example.daitssuapi.domain.myPage.dto.response.MyArticleResponse +import com.example.daitssuapi.domain.myPage.dto.response.MyScrapResponse import com.example.daitssuapi.domain.user.model.repository.UserRepository import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service @@ -16,6 +18,7 @@ class MyPageService( private val userRepository: UserRepository, private val articleRepository: ArticleRepository, private val commentRepository: CommentRepository, + private val scrapRepository: ScrapRepository, ) { fun getComments(userId: Long): List { userRepository.findByIdOrNull(id = userId) ?: throw DefaultException(errorCode = ErrorCode.USER_NOT_FOUND) @@ -46,4 +49,20 @@ class MyPageService( ) } } + + fun getMyScrap(userId: Long) : List { + val user = userRepository.findByIdOrNull(userId) + ?: throw DefaultException(errorCode = ErrorCode.USER_NOT_FOUND) + return scrapRepository.findByUserAndIsActiveTrueOrderByCreatedAtDesc(user).map { + MyScrapResponse( + id = it.article.id, + topic = it.article.topic.value, + title = it.article.title, + content = it.article.content, + createdAt = it.article.createdAt, + commentSize = it.article.comments.count { comment -> !comment.isDeleted } + ) + } + + } } diff --git a/src/test/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageControllerTest.kt b/src/test/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageControllerTest.kt index 487e89fa..a0e1f6ec 100644 --- a/src/test/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageControllerTest.kt +++ b/src/test/kotlin/com/example/daitssuapi/domain/myPage/controller/MyPageControllerTest.kt @@ -104,10 +104,31 @@ class MyPageControllerTest( fun get_my_articles_with_wrong_user_id() { val accessToken = tokenProvider.createAccessToken(id = 1L).token - mockMvc.perform(get("$baseUrl/articles") + mockMvc.perform(get("$baseUrl/artices") + .header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken") + ).andExpect(status().isOk) + .andExpect(jsonPath("$.data").isEmpty) + } + + @Test + @DisplayName("스크랩한 userId를 이용하여 게시글 조회시 _ 1개 이상의 게시글이 조회된다") + fun get_my_scraps_with_user_id() { + val accessToken = tokenProvider.createAccessToken(id = 1L).token + + mockMvc.perform(get("$baseUrl/scraps") + .header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken") + ).andExpect(status().isOk) + .andExpect(jsonPath("$.data").isNotEmpty) + } + + @Test + @DisplayName("스크랩하지 않은 userId를 이용하여 게시글 조회시 _ 빈 리스트가 출력된다") + fun get_my_scraps_with_wrong_user_id() { + val accessToken = tokenProvider.createAccessToken(id = 3L).token + + mockMvc.perform(get("$baseUrl/scraps") .header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken") ).andExpect(status().isOk) .andExpect(jsonPath("$.data").isEmpty) } - } diff --git a/src/test/kotlin/com/example/daitssuapi/domain/mypage/service/MypageServiceTest.kt b/src/test/kotlin/com/example/daitssuapi/domain/mypage/service/MypageServiceTest.kt index 23f91851..edf64120 100644 --- a/src/test/kotlin/com/example/daitssuapi/domain/mypage/service/MypageServiceTest.kt +++ b/src/test/kotlin/com/example/daitssuapi/domain/mypage/service/MypageServiceTest.kt @@ -3,6 +3,7 @@ package com.example.daitssuapi.domain.myPage.service import com.example.daitssuapi.common.exception.DefaultException import com.example.daitssuapi.domain.article.model.repository.ArticleRepository import com.example.daitssuapi.domain.article.model.repository.CommentRepository +import com.example.daitssuapi.domain.article.model.repository.ScrapRepository import com.example.daitssuapi.domain.myPage.dto.request.CommentDeleteRequest import com.example.daitssuapi.domain.user.model.repository.UserRepository import com.example.daitssuapi.utils.IntegrationTest @@ -17,7 +18,8 @@ class MyPageServiceTest( private val articleRepository: ArticleRepository, private val commentRepository: CommentRepository, private val userRepository: UserRepository, - private val myPageService: MyPageService + private val myPageService: MyPageService, + private val scrapRepository: ScrapRepository, ) { @Test @DisplayName("성공_자신의 userId를 넘겨줄 때_자신의 댓글이 조회된다") @@ -88,4 +90,43 @@ class MyPageServiceTest( myPageService.getMyArticle(userId = wrongUserId) } } + + @Test + @DisplayName("올바른 userId를 받으면 스크랩한 게시글이 조회된다") + fun get_my_scraps_with_user_id() { + val userId = 1L + val user = userRepository.findById(userId).get() + val scraps = scrapRepository.findByUserAndIsActiveTrueOrderByCreatedAtDesc(user) + val findArticles = myPageService.getMyScrap(userId = userId) + + assertAll( + { assertThat(findArticles).isNotEmpty }, + { assertThat(findArticles.size).isEqualTo(scraps.size) } + ) + } + + @Test + @DisplayName("스크랩이 false이면 스크랩한 게시글이 조회가 안된다") + fun get_my_scraps_with_is_active_false() { + val userId = 3L + val user = userRepository.findById(userId).get() + val scrap = scrapRepository.findByUserAndIsActiveTrueOrderByCreatedAtDesc(user) + val findScrapRepository = myPageService.getMyScrap(userId) + + assertAll( + { assertThat(scrap).isEmpty()}, + { assertThat(findScrapRepository).isEmpty()} + ) + } + + @Test + @DisplayName("없는 userId를 받으면 USER_NOT_FOUND 에러가 발생한다") + fun get_my_scraps_with_wrong_user_id() { + val wrongUserId = 999L + + assertThrows { + myPageService.getMyScrap(userId = wrongUserId) + } + } + }