Skip to content

Commit

Permalink
[#80] 내가 쓴 글 조회 (#96)
Browse files Browse the repository at this point in the history
* feat : 내가 쓴 글 조회

* test : 내가 쓴 글 조회

* fix : 내가 쓴 글 생성일자 기준 내림차순

* fix : 내가 쓴 글 생성일자 기준 내림차순

* fix : develop conflict 해결

* feat : 내가 쓴 글 조회

* test : 내가 쓴 글 조회

* fix : 내가 쓴 글 생성일자 기준 내림차순

* develop rebase

* fix : develop conflict 해결

* fix : 내가 쓴 글 정렬 방법 변경, comment에서 isDeleted 필터링 추가

* fix : 테스트 수정

* fix : unit 테스트 삭제
  • Loading branch information
swdevsw98 authored Dec 2, 2023
1 parent fca6da7 commit bb57a81
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,4 +17,6 @@ interface ArticleRepository : JpaRepository<Article, Long> {
fun findAllByCreatedAtIsGreaterThanEqual(
createdAt: LocalDateTime
): List<Article>

fun findAllByWriterOrderByCreatedAtDesc(writer: User) : List<Article>
}
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))

}
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
)
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 }
)
}
}


}
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)
}

}
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)
}
}
}

0 comments on commit bb57a81

Please sign in to comment.