Skip to content

Commit

Permalink
[#82] 스크랩한 글 조회 (#100)
Browse files Browse the repository at this point in the history
* feat : 내가 스크랩한 글 조회

* feat : 내가 스크랩한 글 조회 테스트
  • Loading branch information
swdevsw98 authored Dec 26, 2023
1 parent d681dd1 commit c67edd3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<Scrap, Long> {
fun findByArticleIdAndUserId(articleId: Long, userId: Long): Scrap?

fun findByArticleIdAndIsActiveTrue(articleId: Long): List<Scrap>

fun findByUserAndIsActiveTrueOrderByCreatedAtDesc(user: User): List<Scrap>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<List<MyScrapResponse>> {
val userId = argumentResolver.resolveUserId()

return Response(data = myPageService.getMyScrap(userId = userId))
}
}
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<CommentResponse> {
userRepository.findByIdOrNull(id = userId) ?: throw DefaultException(errorCode = ErrorCode.USER_NOT_FOUND)
Expand Down Expand Up @@ -46,4 +49,20 @@ class MyPageService(
)
}
}

fun getMyScrap(userId: Long) : List<MyScrapResponse> {
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 }
)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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를 넘겨줄 때_자신의 댓글이 조회된다")
Expand Down Expand Up @@ -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<DefaultException> {
myPageService.getMyScrap(userId = wrongUserId)
}
}

}

0 comments on commit c67edd3

Please sign in to comment.