Skip to content

Commit

Permalink
Merge pull request #71 from snuhcs-course/feat/back-getMyPosts
Browse files Browse the repository at this point in the history
feat: feat getMyPosts API
  • Loading branch information
yangchanhk98 authored Nov 24, 2023
2 parents 1e18864 + 085bdae commit 66b44a3
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class PostController(private val postService: PostService) {
return ResponseEntity.ok(postService.getPosts(index, count))
}

@GetMapping("/me")
fun getMyPosts(
@CurrentUser username: String
): ResponseEntity<List<PostDto>>{
return ResponseEntity.ok(postService.getMyPosts(username))
}

@GetMapping("/{id}")
fun getPost(
@PathVariable(value = "id") id: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class PostDao(
return list
}

fun getMyPosts(username: String): List<PostDto> {
val list = mutableListOf<PostDto>()
val postsRef = db.collection(POST_COLLECTION_NAME)
val postQuery = postsRef.whereEqualTo("created_by", username)
.orderBy("created_at", Query.Direction.DESCENDING)
val documents = postQuery.get().get().documents
for (document in documents) {
list.add(document.toObject(PostDto::class.java))
}
return list
}

fun getPost(postId: String): PostDto? {
val future = db.collection(POST_COLLECTION_NAME).document(postId).get()
val document: DocumentSnapshot = future.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class PostService(
return postDao.getPosts(index, count)
}

fun getMyPosts(username: String): List<PostDto> {
if (!userDao.existUser(username)) throw CustomHttp404("User doesn't exist.")
return postDao.getMyPosts(username)
}

fun getPost(postId: String): PostDto? {
if (postDao.existPost(postId).not())
throw CustomHttp404("Post doesn't exist.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,47 @@ internal class PostControllerTest @Autowired constructor(
.andExpect(jsonPath("$[0].content").value(content + 0))
.andExpect(jsonPath("$[0].created_at").value(createdAt + 0))
.andExpect(jsonPath("$[0].modified_at").value(modifiedAt + 0))
verify(postService).getPosts(index, count)
verify(postService, times(1)).getPosts(index, count)
}

@Test
@WithCustomUser
@DisplayName("자신의 게시글 데이터 가져오기 테스트")
fun getMyPosts() {
// given
val username = "custom_username"
val list = mutableListOf<PostDto>()
val size = 2
val id = "test_id"
val content = "test_content"
val createdAt = "test_created_at"
val modifiedAt = "test_modified_at"
for (i in 0 until size) {
list.add(
PostDto(
id = id + i,
created_by = username,
content = content + i,
created_at = createdAt + i,
modified_at = modifiedAt + i
)
)
}
given(postService.getMyPosts(username)).willReturn(list)

// when
val result = this.mockMvc.perform(get("/api/post/me"))

// then
result.andExpect(status().isOk)
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.length()", Matchers.equalTo(size)))
.andExpect(jsonPath("$[0].id").value(id + 0))
.andExpect(jsonPath("$[0].created_by").value(username))
.andExpect(jsonPath("$[0].content").value(content + 0))
.andExpect(jsonPath("$[0].created_at").value(createdAt + 0))
.andExpect(jsonPath("$[0].modified_at").value(modifiedAt + 0))
verify(postService, times(1)).getMyPosts(username)
}

@Test
Expand All @@ -133,7 +173,7 @@ internal class PostControllerTest @Autowired constructor(
.andExpect(jsonPath("$.content").value(postDto.content))
.andExpect(jsonPath("$.created_at").value(postDto.created_at))
.andExpect(jsonPath("$.modified_at").value(postDto.modified_at))
verify(postService).getPost(postDto.id)
verify(postService, times(1)).getPost(postDto.id)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ internal class PostDaoTest {
)
}

@Test
fun getMyPosts() {
// given
val username = userList[0].username
val postListForUser = mutableListOf<PostDto>()
postListForUser.add(postList[1])
postListForUser.add(postList[0])
Mockito.`when`(db.collection(POST_COLLECTION_NAME))
.thenReturn(testDB.collection(POST_COLLECTION_NAME))

// when
val result = postDao.getMyPosts(username)

// then
assertAll(
{ assertEquals(result, postListForUser) }
)
}

@Test
fun getPost() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,45 @@ internal class PostServiceTest {
verify(postDao, times(1)).getPosts(index, count)
}

@Test
@DisplayName("자신의 게시글 데이터 가져오기")
fun getMyPosts() {
// given
val username = "test_username"
val wrongUsername = "wrong_username"
val list = mutableListOf<PostDto>()
val size = 2
for (i in 0 until size) {
list.add(
PostDto(
id = "test_id$i",
created_by = username,
content = "test_content$i",
created_at = "test_created_at$i",
modified_at = "test_modified_at$i"
)
)
}
Mockito.`when`(userDao.existUser(username)).thenReturn(true)
Mockito.`when`(userDao.existUser(wrongUsername)).thenReturn(false)
Mockito.`when`(postDao.getMyPosts(username)).thenReturn(list)

// when
val result = postService.getMyPosts(username)
val assertThrows = assertThrows(CustomHttp404::class.java) {
postService.getMyPosts(wrongUsername)
}

// then
assertAll(
{ assertEquals(result, list) },
{ assertEquals(assertThrows.message, "User doesn't exist.") }
)
verify(userDao, times(1)).existUser(username)
verify(userDao, times(1)).existUser(wrongUsername)
verify(postDao, times(1)).getMyPosts(username)
}

@Test
@DisplayName("특정 게시글 데이터 가져오기")
fun getPost() {
Expand Down

0 comments on commit 66b44a3

Please sign in to comment.