-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat : 내가 쓴 글 조회 * test : 내가 쓴 글 조회 * fix : 내가 쓴 글 생성일자 기준 내림차순 * fix : 내가 쓴 글 생성일자 기준 내림차순 * fix : develop conflict 해결 * feat : 내가 쓴 글 조회 * test : 내가 쓴 글 조회 * fix : 내가 쓴 글 생성일자 기준 내림차순 * develop rebase * fix : develop conflict 해결 * fix : 내가 쓴 글 정렬 방법 변경, comment에서 isDeleted 필터링 추가 * fix : 테스트 수정 * fix : unit 테스트 삭제
- Loading branch information
Showing
6 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/example/daitssuapi/domain/mypage/controller/MypageController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<MyArticleResponse>> = | ||
Response(data = mypageService.getMyArticle(userId = userId)) | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/example/daitssuapi/domain/mypage/dto/response/MyArticleResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 MyArticleResponse ( | ||
val id: Long, | ||
val topic: String, | ||
val title: String, | ||
val content: String, | ||
val createdAt: LocalDateTime, | ||
val commentSize: Int | ||
) |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/com/example/daitssuapi/domain/mypage/service/MypageService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MyArticleResponse> { | ||
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 } | ||
) | ||
} | ||
} | ||
|
||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/test/kotlin/com/example/daitssuapi/domain/mypage/controller/MypageControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/test/kotlin/com/example/daitssuapi/domain/mypage/service/MypageServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DefaultException> { | ||
mypageService.getMyArticle(userId = wrongUserId) | ||
} | ||
} | ||
} |