Skip to content

Commit

Permalink
[BE] feat: 학교 목록 조회 기능 구현 (#431) (#432)
Browse files Browse the repository at this point in the history
* feat: School 목록 조회 기능 구현

* feat: School 목록 조회 API 구현

* feat: Transactional(readonly=true) 적용
  • Loading branch information
xxeol2 authored Sep 11, 2023
1 parent 62c4644 commit ba9b4a9
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
22 changes: 22 additions & 0 deletions backend/src/main/java/com/festago/application/SchoolService.java
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());
}
}
17 changes: 17 additions & 0 deletions backend/src/main/java/com/festago/dto/SchoolResponse.java
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 backend/src/main/java/com/festago/dto/SchoolsResponse.java
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));
}
}
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());
}
}
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);
}
}

0 comments on commit ba9b4a9

Please sign in to comment.