-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: School 목록 조회 기능 구현 * feat: School 목록 조회 API 구현 * feat: Transactional(readonly=true) 적용
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/com/festago/application/SchoolService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/com/festago/dto/SchoolsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SchoolResponse> schools) { | ||
|
||
public static SchoolsResponse from(List<School> schools) { | ||
return schools.stream() | ||
.map(SchoolResponse::from) | ||
.collect(collectingAndThen(toList(), SchoolsResponse::new)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/festago/presentation/SchoolController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SchoolsResponse> findAll() { | ||
return ResponseEntity.ok(schoolService.findAll()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
backend/src/test/java/com/festago/presentation/SchoolControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |