From ba9b4a9e167451751e6df3376c2a950325252def Mon Sep 17 00:00:00 2001 From: xxeol2 Date: Mon, 11 Sep 2023 17:43:07 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20feat:=20=ED=95=99=EA=B5=90=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20(#431)=20(#432)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: School 목록 조회 기능 구현 * feat: School 목록 조회 API 구현 * feat: Transactional(readonly=true) 적용 --- .../festago/application/SchoolService.java | 22 +++++++ .../java/com/festago/dto/SchoolResponse.java | 17 ++++++ .../java/com/festago/dto/SchoolsResponse.java | 17 ++++++ .../presentation/SchoolController.java | 24 ++++++++ .../presentation/SchoolControllerTest.java | 61 +++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 backend/src/main/java/com/festago/application/SchoolService.java create mode 100644 backend/src/main/java/com/festago/dto/SchoolResponse.java create mode 100644 backend/src/main/java/com/festago/dto/SchoolsResponse.java create mode 100644 backend/src/main/java/com/festago/presentation/SchoolController.java create mode 100644 backend/src/test/java/com/festago/presentation/SchoolControllerTest.java diff --git a/backend/src/main/java/com/festago/application/SchoolService.java b/backend/src/main/java/com/festago/application/SchoolService.java new file mode 100644 index 000000000..edb571b4a --- /dev/null +++ b/backend/src/main/java/com/festago/application/SchoolService.java @@ -0,0 +1,22 @@ +package com.festago.application; + +import com.festago.domain.SchoolRepository; +import com.festago.dto.SchoolsResponse; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class SchoolService { + + private final SchoolRepository schoolRepository; + + public SchoolService(SchoolRepository schoolRepository) { + this.schoolRepository = schoolRepository; + } + + @Transactional(readOnly = true) + public SchoolsResponse findAll() { + return SchoolsResponse.from(schoolRepository.findAll()); + } +} diff --git a/backend/src/main/java/com/festago/dto/SchoolResponse.java b/backend/src/main/java/com/festago/dto/SchoolResponse.java new file mode 100644 index 000000000..a33a104db --- /dev/null +++ b/backend/src/main/java/com/festago/dto/SchoolResponse.java @@ -0,0 +1,17 @@ +package com.festago.dto; + +import com.festago.domain.School; + +public record SchoolResponse( + Long id, + String domain, + String name) { + + public static SchoolResponse from(School school) { + return new SchoolResponse( + school.getId(), + school.getDomain(), + school.getName() + ); + } +} diff --git a/backend/src/main/java/com/festago/dto/SchoolsResponse.java b/backend/src/main/java/com/festago/dto/SchoolsResponse.java new file mode 100644 index 000000000..f92e33f9e --- /dev/null +++ b/backend/src/main/java/com/festago/dto/SchoolsResponse.java @@ -0,0 +1,17 @@ +package com.festago.dto; + +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toList; + +import com.festago.domain.School; +import java.util.List; + +public record SchoolsResponse( + List schools) { + + public static SchoolsResponse from(List schools) { + return schools.stream() + .map(SchoolResponse::from) + .collect(collectingAndThen(toList(), SchoolsResponse::new)); + } +} diff --git a/backend/src/main/java/com/festago/presentation/SchoolController.java b/backend/src/main/java/com/festago/presentation/SchoolController.java new file mode 100644 index 000000000..4f942e162 --- /dev/null +++ b/backend/src/main/java/com/festago/presentation/SchoolController.java @@ -0,0 +1,24 @@ +package com.festago.presentation; + +import com.festago.application.SchoolService; +import com.festago.dto.SchoolsResponse; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/schools") +public class SchoolController { + + private final SchoolService schoolService; + + public SchoolController(SchoolService schoolService) { + this.schoolService = schoolService; + } + + @GetMapping + public ResponseEntity findAll() { + return ResponseEntity.ok(schoolService.findAll()); + } +} diff --git a/backend/src/test/java/com/festago/presentation/SchoolControllerTest.java b/backend/src/test/java/com/festago/presentation/SchoolControllerTest.java new file mode 100644 index 000000000..f9584d02c --- /dev/null +++ b/backend/src/test/java/com/festago/presentation/SchoolControllerTest.java @@ -0,0 +1,61 @@ +package com.festago.presentation; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.festago.application.SchoolService; +import com.festago.dto.SchoolResponse; +import com.festago.dto.SchoolsResponse; +import com.festago.support.CustomWebMvcTest; +import java.nio.charset.StandardCharsets; +import java.util.List; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +@CustomWebMvcTest(SchoolController.class) +@DisplayNameGeneration(ReplaceUnderscores.class) +@SuppressWarnings("NonAsciiCharacters") +class SchoolControllerTest { + + @Autowired + MockMvc mockMvc; + + @Autowired + ObjectMapper objectMapper; + + @MockBean + SchoolService schoolService; + + @Test + void 모든_학교_정보_조회() throws Exception { + // given + SchoolsResponse expected = new SchoolsResponse( + List.of( + new SchoolResponse(1L, "pooh.ac.kr", "푸우대학"), + new SchoolResponse(2L, "ash.ac.kr", "애쉬대학") + )); + + given(schoolService.findAll()) + .willReturn(expected); + + // when & then + String content = mockMvc.perform(get("/schools") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(print()) + .andReturn() + .getResponse() + .getContentAsString(StandardCharsets.UTF_8); + SchoolsResponse actual = objectMapper.readValue(content, SchoolsResponse.class); + assertThat(actual).isEqualTo(expected); + } +}