12/24/28(토)
//response
{
"todoTitleId": 3,
"title": "가사노동"
"todos": [
{
"id": 2,
"content": "빨래하기",
"isCompleted": false
},
{
"id": 3,
"content": "청소기 돌리기",
"isCompleted": true
}
]
}
GET /lists/{listId}
Request
Response
//controller
@GetMapping("/todoTitles/{todoTitleId}")
public TitleContentResponse readContentInTitle(@PathVariable Long todoTitleId) {
return todoTitleService.getContentInTitle(todoTitleId);
}
//response
public record TitleContentResponse(
String title,
Long id,
List<TodoResponse> todo
) {
}
리스트 형태의 response를 받으면 된다.
//service
public TitleContentResponse getContentInTitle(Long todoTitleId) {
//제목의 id값을 찾아.
TodoTitle todoTitle = todoTitleRepository.findById(todoTitleId).orElseThrow();
//todoRepository에서 findByTodoTitleId를 할 수 있도록 함수를 만들어둔다.
List<Todo> byTodoTitleId = todoRepository.findByTodoTitleId(todoTitleId);
//todo의 컨텐츠들
return new TitleContentResponse(
todoTitle.getTitle(),
todoTitleId,
byTodoTitleId.stream().map(content -> new TodoResponse(
content.getContent(),
content.getId())).toList());
}
byTodoTitleId.stream().map(content -> new TodoResponse( content.getContent(), content.getId())).toList(); 이 코드를 잘 기억해두자!
리스트 형태니까 당연히 작성하는건 맞지만, 그게 꼭 밖에서만 이루어지는건 아니다!
😐 느낀점
id가 있어야 프론트에서 그 id값을 받아 어떤 일을 할 수 있다고 했다.
나는 처음에 그냥 목록들만 출력했는데, [id와 목록]을 같이 출력해야한다.
모든 건 id값으로 통하기 때문에 id값은 의무라고 생각하고 하면 된다.