#14 simple blog : get post list api

This commit is contained in:
haerong22
2022-07-26 02:10:47 +09:00
parent 1e1555ff8c
commit 8a482b7d08
6 changed files with 95 additions and 9 deletions

View File

@@ -1,6 +1,5 @@
package com.example.simpleblog.controller;
import com.example.simpleblog.domain.Post;
import com.example.simpleblog.request.PostCreate;
import com.example.simpleblog.response.PostResponse;
import com.example.simpleblog.service.PostService;
@@ -9,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@Slf4j
@RestController
@@ -26,4 +26,9 @@ public class PostController {
public PostResponse getPost(@PathVariable(name = "postId") Long id) {
return postService.getPost(id);
}
@GetMapping("/posts")
public List<PostResponse> getPostList() {
return postService.getPostList();
}
}

View File

@@ -1,5 +1,6 @@
package com.example.simpleblog.response;
import com.example.simpleblog.domain.Post;
import lombok.Builder;
import lombok.Getter;
@@ -13,8 +14,13 @@ public class PostResponse {
private final String title;
private final String content;
@Builder
public PostResponse(Post post) {
this.id = post.getId();
this.title = post.getTitle();
this.content = post.getContent();
}
@Builder
public PostResponse(Long id, String title, String content) {
this.id = id;
this.title = title.substring(0 ,Math.min(title.length(), 10));

View File

@@ -8,6 +8,9 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor
@@ -35,4 +38,10 @@ public class PostService {
.content(postEntity.getContent())
.build();
}
public List<PostResponse> getPostList() {
return postRepository.findAll().stream()
.map(PostResponse::new)
.collect(Collectors.toList());
}
}

View File

@@ -1 +1,11 @@
spring:
h2:
console:
enabled: true
path: /h2-console
datasource:
url: jdbc:h2:mem:simpleblog
username: sa
password:
driver-class-name: org.h2.Driver

View File

@@ -12,6 +12,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -134,4 +135,37 @@ class PostControllerTest {
;
}
@Test
@DisplayName("글 리스트 조회")
void get_post_list_success() throws Exception {
// given
Post post1 = postRepository.save(Post.builder()
.title("제목1")
.content("내용1")
.build());
Post post2 = postRepository.save(Post.builder()
.title("제목2")
.content("내용2")
.build());
// json 응답에서 title 값 길이를 최대 10글자
// expected
mockMvc.perform(get("/posts")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()", is(2)))
.andExpect(jsonPath("$[0].id").value(post1.getId()))
.andExpect(jsonPath("$[0].title").value("제목1"))
.andExpect(jsonPath("$[0].content").value("내용1"))
.andExpect(jsonPath("$[1].id").value(post2.getId()))
.andExpect(jsonPath("$[1].title").value("제목2"))
.andExpect(jsonPath("$[1].content").value("내용2"))
.andDo(print())
;
}
}

View File

@@ -10,6 +10,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@@ -50,21 +52,41 @@ class PostServiceTest {
@DisplayName("postId 로 글 조회")
void get_post_by_id_test() {
// given
Post post = Post.builder()
Post post = postRepository.save(Post.builder()
.title("글 제목입니다.")
.content("글 내용입니다.")
.build();
postRepository.save(post);
Long postId = 1L;
.build());
// when
PostResponse response = postService.getPost(postId);
PostResponse response = postService.getPost(post.getId());
// then
assertNotNull(post);
assertEquals("글 제목입니다.", response.getTitle());
assertEquals("글 내용입니다.", response.getContent());
}
@Test
@DisplayName("글 리스트 조회")
void get_posts_list_test() {
// given
postRepository.saveAll(List.of(
Post.builder()
.title("글 1제목입니다.")
.content("글 1내용입니다.")
.build(),
Post.builder()
.title("글 2제목입니다.")
.content("글 2내용입니다.")
.build()
));
Long postId = 1L;
// when
List<PostResponse> posts = postService.getPostList();
// then
assertEquals(2L, posts.size());
}
}