From bb57a81445dca69ba69051ef991d9012ae9c48ac Mon Sep 17 00:00:00 2001 From: swdevsw98 <102571918+swdevsw98@users.noreply.github.com> Date: Sat, 2 Dec 2023 12:45:39 +0900 Subject: [PATCH] =?UTF-8?q?[#80]=20=EB=82=B4=EA=B0=80=20=EC=93=B4=20?= =?UTF-8?q?=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20(#96)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat : 내가 쓴 글 조회 * test : 내가 쓴 글 조회 * fix : 내가 쓴 글 생성일자 기준 내림차순 * fix : 내가 쓴 글 생성일자 기준 내림차순 * fix : develop conflict 해결 * feat : 내가 쓴 글 조회 * test : 내가 쓴 글 조회 * fix : 내가 쓴 글 생성일자 기준 내림차순 * develop rebase * fix : develop conflict 해결 * fix : 내가 쓴 글 정렬 방법 변경, comment에서 isDeleted 필터링 추가 * fix : 테스트 수정 * fix : unit 테스트 삭제 --- .../model/repository/ArticleRepository.kt | 3 ++ .../mypage/controller/MypageController.kt | 24 ++++++++++ .../mypage/dto/response/MyArticleResponse.kt | 12 +++++ .../domain/mypage/service/MypageService.kt | 35 +++++++++++++++ .../mypage/controller/MypageControllerTest.kt | 39 ++++++++++++++++ .../mypage/service/MypageServiceTest.kt | 44 +++++++++++++++++++ 6 files changed, 157 insertions(+) create mode 100644 src/main/kotlin/com/example/daitssuapi/domain/mypage/controller/MypageController.kt create mode 100644 src/main/kotlin/com/example/daitssuapi/domain/mypage/dto/response/MyArticleResponse.kt create mode 100644 src/main/kotlin/com/example/daitssuapi/domain/mypage/service/MypageService.kt create mode 100644 src/test/kotlin/com/example/daitssuapi/domain/mypage/controller/MypageControllerTest.kt create mode 100644 src/test/kotlin/com/example/daitssuapi/domain/mypage/service/MypageServiceTest.kt diff --git a/src/main/kotlin/com/example/daitssuapi/domain/main/model/repository/ArticleRepository.kt b/src/main/kotlin/com/example/daitssuapi/domain/main/model/repository/ArticleRepository.kt index f9bbd52e..6e4b128e 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/main/model/repository/ArticleRepository.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/main/model/repository/ArticleRepository.kt @@ -1,6 +1,7 @@ package com.example.daitssuapi.domain.main.model.repository import com.example.daitssuapi.domain.main.model.entity.Article +import com.example.daitssuapi.domain.main.model.entity.User import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.data.jpa.repository.JpaRepository @@ -16,4 +17,6 @@ interface ArticleRepository : JpaRepository { fun findAllByCreatedAtIsGreaterThanEqual( createdAt: LocalDateTime ): List
+ + fun findAllByWriterOrderByCreatedAtDesc(writer: User) : List
} \ No newline at end of file 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 new file mode 100644 index 00000000..5e7ba5f7 --- /dev/null +++ b/src/main/kotlin/com/example/daitssuapi/domain/mypage/controller/MypageController.kt @@ -0,0 +1,24 @@ +package com.example.daitssuapi.domain.mypage.controller + +import com.example.daitssuapi.common.dto.Response +import com.example.daitssuapi.domain.mypage.dto.response.MyArticleResponse +import com.example.daitssuapi.domain.mypage.service.MypageService +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + + +@RestController +@RequestMapping("/mypage") +class MypageController( + private val mypageService: MypageService +) { + + @GetMapping("/{userId}/articles") + fun getMyArticle( + @PathVariable("userId") userId: Long + ) : Response> = + Response(data = mypageService.getMyArticle(userId = userId)) + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/daitssuapi/domain/mypage/dto/response/MyArticleResponse.kt b/src/main/kotlin/com/example/daitssuapi/domain/mypage/dto/response/MyArticleResponse.kt new file mode 100644 index 00000000..3ef07510 --- /dev/null +++ b/src/main/kotlin/com/example/daitssuapi/domain/mypage/dto/response/MyArticleResponse.kt @@ -0,0 +1,12 @@ +package com.example.daitssuapi.domain.mypage.dto.response + +import java.time.LocalDateTime + +data class MyArticleResponse ( + 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 new file mode 100644 index 00000000..e7790c9d --- /dev/null +++ b/src/main/kotlin/com/example/daitssuapi/domain/mypage/service/MypageService.kt @@ -0,0 +1,35 @@ +package com.example.daitssuapi.domain.mypage.service + +import com.example.daitssuapi.common.enums.ErrorCode +import com.example.daitssuapi.common.exception.DefaultException +import com.example.daitssuapi.domain.main.model.repository.ArticleRepository +import com.example.daitssuapi.domain.main.model.repository.CommentRepository +import com.example.daitssuapi.domain.main.model.repository.UserRepository +import com.example.daitssuapi.domain.mypage.dto.response.MyArticleResponse +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Service + +@Service +class MypageService ( + private val userRepository: UserRepository, + private val articleRepository: ArticleRepository, + private val commentRepository: CommentRepository, +){ + + fun getMyArticle(userId: Long) : List { + val user = userRepository.findByIdOrNull(userId) + ?: throw DefaultException(errorCode = ErrorCode.USER_NOT_FOUND) + return articleRepository.findAllByWriterOrderByCreatedAtDesc(user).map { + MyArticleResponse( + id = it.id, + topic = it.topic.value, + title = it.title, + content = it.content, + createdAt = it.createdAt, + commentSize = it.comments.count { comment -> !comment.isDeleted } + ) + } + } + + +} \ No newline at end of file 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 new file mode 100644 index 00000000..aab0b18a --- /dev/null +++ b/src/test/kotlin/com/example/daitssuapi/domain/mypage/controller/MypageControllerTest.kt @@ -0,0 +1,39 @@ +package com.example.daitssuapi.domain.mypage.controller + +import com.example.daitssuapi.utils.ControllerTest +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.* + +@ControllerTest +class MypageControllerTest { + @Autowired + private lateinit var mockMvc: MockMvc + + private val url = "/mypage" + + @Test + @DisplayName("게시글이 있는 userId를 이용하여 게시글 조회시 _ 1개 이상의 게시글이 조회된다") + fun get_my_articles_with_user_id() { + val userId = 4L + + mockMvc.perform(get("$url/$userId/articles")) + .andExpect(status().isOk) + .andExpect(jsonPath("$.data").isNotEmpty) + + } + + @Test + @DisplayName("게시글이 없는 userId를 이용하여 게시글 조회시 _ 빈 List를 받는다") + fun get_my_articles_with_wrong_user_id() { + val wrongUserID = 1L + + mockMvc.perform(get("$url/$wrongUserID/articles")) + .andExpect(status().isOk) + .andExpect(jsonPath("$.data").isEmpty) + } + +} \ No newline at end of file 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 new file mode 100644 index 00000000..e959939d --- /dev/null +++ b/src/test/kotlin/com/example/daitssuapi/domain/mypage/service/MypageServiceTest.kt @@ -0,0 +1,44 @@ +package com.example.daitssuapi.domain.mypage.service + +import com.example.daitssuapi.common.exception.DefaultException +import com.example.daitssuapi.domain.main.model.repository.ArticleRepository +import com.example.daitssuapi.domain.main.model.repository.UserRepository +import com.example.daitssuapi.utils.IntegrationTest +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll +import org.junit.jupiter.api.assertThrows + + +@IntegrationTest +class MypageServiceTest ( + private val articleRepository: ArticleRepository, + private val userRepository: UserRepository, + private val mypageService:MypageService +) { + + @Test + @DisplayName("올바른 userId를 받으면 작성한 게시글이 조회된다") + fun get_my_articles_with_user_id() { + val userId =4L + val user = userRepository.findById(userId).get() + val articles = articleRepository.findAllByWriterOrderByCreatedAtDesc(user) + val findArticles = mypageService.getMyArticle(userId = userId) + + assertAll( + { assertThat(findArticles).isNotEmpty }, + { assertThat(findArticles.size).isEqualTo(articles.size) } + ) + } + + @Test + @DisplayName("없는 userId를 받으면 USER_NOT_FOUND 에러가 발생한다") + fun get_my_articles_with_wrong_user_id(){ + val wrongUserId = 999L + + assertThrows { + mypageService.getMyArticle(userId = wrongUserId) + } + } +} \ No newline at end of file