Skip to content

Commit

Permalink
Merge pull request #20 from Team-BomBomBom/feat/19-modifiy_study_list
Browse files Browse the repository at this point in the history
Feat: #19 ์Šคํ„ฐ๋”” ๋ชฉ๋ก ์กฐํšŒ API ์ˆ˜์ •
  • Loading branch information
msjang4 authored Jun 18, 2024
2 parents 06d5ba5 + 872b47f commit c7b93da
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bombombom.devs.study.controller.dto.request.RegisterAlgorithmStudyRequest;
import com.bombombom.devs.study.controller.dto.request.RegisterBookStudyRequest;
import com.bombombom.devs.study.controller.dto.response.StudyPageResponse;
import com.bombombom.devs.study.controller.dto.response.StudyResponse;
import com.bombombom.devs.study.models.Study;
import com.bombombom.devs.study.service.StudyService;
Expand Down Expand Up @@ -30,24 +31,26 @@ public class StudyController {
private final StudyService studyService;

@PostMapping("/algo")
public ResponseEntity<Void> registerAlgorithmStudy(@RequestBody RegisterAlgorithmStudyRequest registerAlgorithmStudyRequest){
public ResponseEntity<Void> registerAlgorithmStudy(
@RequestBody RegisterAlgorithmStudyRequest registerAlgorithmStudyRequest) {
log.info("{}", registerAlgorithmStudyRequest);
Long id = studyService.createAlgorithmStudy(registerAlgorithmStudyRequest.toServiceDto());
return ResponseEntity.created(URI.create(RESOURCE_PATH+"/"+id)).build();
return ResponseEntity.created(URI.create(RESOURCE_PATH + "/" + id)).build();
}

@PostMapping("/book")
public ResponseEntity<Void> registerBookStudy(@RequestBody RegisterBookStudyRequest registerBookStudyRequest){
public ResponseEntity<Void> registerBookStudy(
@RequestBody RegisterBookStudyRequest registerBookStudyRequest) {
log.info("{}", registerBookStudyRequest);
Long id =studyService.createBookStudy(registerBookStudyRequest.toServiceDto());
return ResponseEntity.created(URI.create(RESOURCE_PATH+id)).build();
Long id = studyService.createBookStudy(registerBookStudyRequest.toServiceDto());
return ResponseEntity.created(URI.create(RESOURCE_PATH + id)).build();
}

@GetMapping
public ResponseEntity<List<StudyResponse>> studyList(@PageableDefault(sort="id", direction= Sort.Direction.DESC) Pageable pageable){
List<StudyResponse> studyResponses = studyService.readStudy(pageable);
log.info("{}", studyResponses);
return ResponseEntity.ok(studyResponses);
public ResponseEntity<StudyPageResponse> studyList(
@PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
StudyPageResponse studyPageResponse = studyService.readStudy(pageable);
return ResponseEntity.ok(studyPageResponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.format.annotation.DateTimeFormat;

@Builder
@JsonNaming(SnakeCaseStrategy.class)
public record AlgorithmStudyResponse(
Long id,
String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.format.annotation.DateTimeFormat;

@Builder
@JsonNaming(SnakeCaseStrategy.class)
public record BookStudyResponse(
Long id,
String name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bombombom.devs.study.controller.dto.response;


import java.util.List;
import lombok.Builder;

@Builder
public record StudyPageResponse(

Long totalElements,
Integer totalPages,
Integer pageNumber,
List<StudyResponse> contents
) {

}
18 changes: 15 additions & 3 deletions src/main/java/com/bombombom/devs/study/service/StudyService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bombombom.devs.study.service;


import com.bombombom.devs.study.controller.dto.response.StudyPageResponse;
import com.bombombom.devs.study.controller.dto.response.StudyResponse;
import com.bombombom.devs.study.models.AlgorithmStudy;
import com.bombombom.devs.study.models.BookStudy;
Expand Down Expand Up @@ -44,11 +46,21 @@ public Long createBookStudy(RegisterBookStudyCommand registerBookStudyCommand) {
}

@Transactional(readOnly = true)
public List<StudyResponse> readStudy(Pageable pageable) {
Page<Study> studies = studyRepository.findAll(pageable);
public StudyPageResponse readStudy(Pageable pageable) {
Page<Study> studyPage = studyRepository.findAll(pageable);

return studies.getContent().stream().map(StudyResult::fromEntity).map(StudyResponse::of)
List<StudyResponse> studies = studyPage.getContent().stream()
.map(StudyResult::fromEntity)
.map(StudyResponse::of)
.toList();

return StudyPageResponse.builder()
.contents(studies)
.pageNumber(studyPage.getNumber())
.totalPages(studyPage.getTotalPages())
.totalElements(studyPage.getTotalElements())
.build();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.bombombom.devs.study.controller.StudyController;
import com.bombombom.devs.study.controller.dto.response.StudyPageResponse;
import com.bombombom.devs.study.controller.dto.response.StudyResponse;
import com.bombombom.devs.study.models.AlgorithmStudy;
import com.bombombom.devs.study.models.BookStudy;
Expand Down Expand Up @@ -120,7 +121,14 @@ void can_retrieve_study_list_through_offset_based_pagination()
*/
List<StudyResponse> studies = new ArrayList<>();
studies.add(StudyResponse.of(StudyResult.fromEntity(study1)));
String expectedResponse = objectMapper.writeValueAsString(studies);

StudyPageResponse studyPageResponse = StudyPageResponse.builder()
.totalPages(2)
.totalElements(2L)
.pageNumber(1)
.contents(studies)
.build();
String expectedResponse = objectMapper.writeValueAsString(studyPageResponse);
resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(content().json(expectedResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.bombombom.devs.study.controller.dto.response.AlgorithmStudyResponse;
import com.bombombom.devs.study.controller.dto.response.BookStudyResponse;
import com.bombombom.devs.study.controller.dto.response.StudyPageResponse;
import com.bombombom.devs.study.controller.dto.response.StudyResponse;
import com.bombombom.devs.study.service.StudyService;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -40,8 +41,8 @@ class StudyControllerTest {
private ObjectMapper objectMapper;

@Test
@DisplayName("์Šคํ„ฐ๋”” ์ปจํŠธ๋กค๋Ÿฌ์˜ studyList ๋ฉ”์†Œ๋“œ๋Š” StudyResponse์˜ JSON Array๋ฃฐ ๋ฐ˜ํ™˜ํ•œ๋‹ค ")
void study_controller_study_list_return_json_array_of_study_response() throws Exception {
@DisplayName("์Šคํ„ฐ๋”” ์ปจํŠธ๋กค๋Ÿฌ์˜ studyList ๋ฉ”์†Œ๋“œ๋Š” StudyPageResponse๋ฃฐ ๋ฐ˜ํ™˜ํ•œ๋‹ค ")
void study_controller_study_list_return_study_page_response() throws Exception {

/*
Given
Expand Down Expand Up @@ -82,7 +83,14 @@ void study_controller_study_list_return_json_array_of_study_response() throws Ex
serviceResponse.add(studyResponse1);
serviceResponse.add(studyResponse2);

when(studyService.readStudy(any(Pageable.class))).thenReturn(serviceResponse);
StudyPageResponse studyPageResponse =
StudyPageResponse.builder()
.totalElements(2L)
.totalPages(1)
.pageNumber(0)
.contents(serviceResponse)
.build();
when(studyService.readStudy(any(Pageable.class))).thenReturn(studyPageResponse);

/*
When
Expand All @@ -96,7 +104,8 @@ void study_controller_study_list_return_json_array_of_study_response() throws Ex
/*
Then
*/
String expectedResponse = objectMapper.writeValueAsString(serviceResponse);
String expectedResponse = objectMapper.writeValueAsString(studyPageResponse);
System.out.println("expectedResponse = " + expectedResponse);
resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(content().json(expectedResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.bombombom.devs.study.controller.dto.response.StudyPageResponse;
import com.bombombom.devs.study.controller.dto.response.StudyResponse;
import com.bombombom.devs.study.models.AlgorithmStudy;
import com.bombombom.devs.study.models.BookStudy;
Expand Down Expand Up @@ -34,8 +35,8 @@ class StudyServiceTest {
private StudyService studyService;

@Test
@DisplayName("์Šคํ„ฐ๋”” ์„œ๋น„์Šค์˜ readStudy ๋ฉ”์†Œ๋“œ๋Š” StudyResponse ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค")
void study_service_read_study_returns_list_of_study_response() throws Exception {
@DisplayName("์Šคํ„ฐ๋”” ์„œ๋น„์Šค์˜ readStudy ๋ฉ”์†Œ๋“œ๋Š” StudyPageResponse๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค")
void study_service_read_study_returns_study_page_response() throws Exception {
/*
Given
*/
Expand Down Expand Up @@ -81,15 +82,20 @@ void study_service_read_study_returns_list_of_study_response() throws Exception
/*
When
*/
List<StudyResponse> responses = studyService.readStudy(PageRequest.of(1, 10));
StudyPageResponse studyPageResponse = studyService.readStudy(PageRequest.of(0, 10));

/*
Then
*/
List<StudyResponse> expectedResponse = repositoryResponses.stream()
List<StudyResponse> studyList = repositoryResponses.stream()
.map(StudyResult::fromEntity).map(StudyResponse::of).toList();

Assertions.assertThat(responses).isEqualTo(expectedResponse);
StudyPageResponse expectedResponse = StudyPageResponse.builder()
.contents(studyList)
.pageNumber(0)
.totalPages(1)
.totalElements(2L)
.build();
Assertions.assertThat(studyPageResponse).isEqualTo(expectedResponse);

}

Expand Down

0 comments on commit c7b93da

Please sign in to comment.