Skip to content

Commit

Permalink
Merge pull request #534 from woowacourse-teams/feature/#532
Browse files Browse the repository at this point in the history
[feat] 전체 행정 구역 조회 기능 추가 (#532)
  • Loading branch information
Jaeyoung22 authored Nov 5, 2023
2 parents f0b9c9f + 78fc8b6 commit 172958c
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import dev.tripdraw.area.domain.AreaRepository;
import dev.tripdraw.area.dto.AreaReqeust;
import dev.tripdraw.area.dto.AreaResponse;
import dev.tripdraw.area.dto.FullAreaResponse;
import dev.tripdraw.area.dto.FullAreaResponses;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -72,4 +75,13 @@ public void create(List<Area> areas) {
areaRepository.deleteAllInBatch();
areaRepository.saveAll(areas);
}

public FullAreaResponses readAll() {
List<Area> areas = areaRepository.findAll();
List<FullAreaResponse> areaResponses = areas.stream()
.map(FullAreaResponse::from)
.collect(Collectors.toList());

return FullAreaResponses.from(areaResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.tripdraw.area.domain.Area;
import dev.tripdraw.area.dto.AreaReqeust;
import dev.tripdraw.area.dto.AreaResponse;
import dev.tripdraw.area.dto.FullAreaResponses;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -24,4 +25,8 @@ public void create() {
List<Area> areas = openApiAreaDownloader.download();
areaService.create(areas);
}

public FullAreaResponses readAll() {
return areaService.readAll();
}
}
10 changes: 10 additions & 0 deletions backend/src/main/java/dev/tripdraw/area/dto/FullAreaResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.tripdraw.area.dto;

import dev.tripdraw.area.domain.Area;

public record FullAreaResponse(String sido, String sigungu, String eupmyeondong) {

public static FullAreaResponse from(Area area) {
return new FullAreaResponse(area.sido(), area.sigungu(), area.umd());
}
}
10 changes: 10 additions & 0 deletions backend/src/main/java/dev/tripdraw/area/dto/FullAreaResponses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.tripdraw.area.dto;

import java.util.List;

public record FullAreaResponses(List<FullAreaResponse> areas) {

public static FullAreaResponses from(List<FullAreaResponse> areas) {
return new FullAreaResponses(areas);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.tripdraw.area.application.AreaServiceFacade;
import dev.tripdraw.area.dto.AreaReqeust;
import dev.tripdraw.area.dto.AreaResponse;
import dev.tripdraw.area.dto.FullAreaResponses;
import dev.tripdraw.common.swagger.SwaggerAuthorizationRequired;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -28,6 +29,17 @@ public class AreaController {

private final AreaServiceFacade areaServiceFacade;

@Operation(summary = "전체 행정구역 조회 API", description = "전체 행정구역을 조회합니다.")
@ApiResponse(
responseCode = "200",
description = "전체 행정구역 조회 성공."
)
@GetMapping("/all")
public ResponseEntity<FullAreaResponses> readAll() {
FullAreaResponses responses = areaServiceFacade.readAll();
return ResponseEntity.ok(responses);
}

@Operation(summary = "행정구역 조회 API", description = "행정구역을 조회합니다.")
@ApiResponse(
responseCode = "200",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import dev.tripdraw.area.domain.Area;
import dev.tripdraw.area.domain.AreaRepository;
import dev.tripdraw.area.dto.AreaResponse;
import dev.tripdraw.area.dto.FullAreaResponse;
import dev.tripdraw.area.dto.FullAreaResponses;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -33,6 +35,28 @@ class AreaServiceTest {
@Autowired
private AreaService areaService;

@Test
void 전체_지역을_조회한다() {
// given
Area 지역1 = areaRepository.save(new Area("서울시", "강남구", "개포동"));
Area 지역2 = areaRepository.save(new Area("서울시", "강남구", "강남동"));
Area 지역3 = areaRepository.save(new Area("서울시", "송파구", "잠실동"));
Area 지역4 = areaRepository.save(new Area("부산시", "강남구", "개포동"));

// when
FullAreaResponses responses = areaService.readAll();

// then
assertThat(responses.areas()).usingRecursiveComparison()
.isEqualTo(
List.of(
FullAreaResponse.from(지역1),
FullAreaResponse.from(지역2),
FullAreaResponse.from(지역3),
FullAreaResponse.from(지역4)
));
}

@MethodSource("paramsAndResponse")
@ParameterizedTest
void 해당하는_지역을_조회한다(List<String> sidoAndsigungu, AreaResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static dev.tripdraw.test.fixture.MemberFixture.새로운_사용자;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
Expand All @@ -10,6 +11,8 @@
import dev.tripdraw.area.domain.AreaRepository;
import dev.tripdraw.area.dto.AreaReqeust;
import dev.tripdraw.area.dto.AreaResponse;
import dev.tripdraw.area.dto.FullAreaResponse;
import dev.tripdraw.area.dto.FullAreaResponses;
import dev.tripdraw.auth.application.JwtTokenProvider;
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.member.domain.MemberRepository;
Expand All @@ -23,6 +26,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -43,6 +47,10 @@ class AreaControllerTest extends ControllerTest {

private Member huchu;
private String huchuToken;
private Area 지역1;
private Area 지역2;
private Area 지역3;
private Area 지역4;


@BeforeEach
Expand All @@ -51,10 +59,36 @@ public void setUp() {
huchu = memberRepository.save(새로운_사용자("통후추"));
huchuToken = jwtTokenProvider.generateAccessToken(huchu.id().toString());

areaRepository.save(new Area("서울시", "강남구", "개포동"));
areaRepository.save(new Area("서울시", "강남구", "강남동"));
areaRepository.save(new Area("서울시", "송파구", "잠실동"));
areaRepository.save(new Area("부산시", "강남구", "개포동"));
지역1 = areaRepository.save(new Area("서울시", "강남구", "개포동"));
지역2 = areaRepository.save(new Area("서울시", "강남구", "강남동"));
지역3 = areaRepository.save(new Area("서울시", "송파구", "잠실동"));
지역4 = areaRepository.save(new Area("부산시", "강남구", "개포동"));
}

@Test
void 전체_행정구역을_조회한다() {
// when
ExtractableResponse<Response> actual = RestAssured.given().log().all()
.auth().preemptive().oauth2(huchuToken)
.when().get("/areas/all")
.then().log().all()
.extract();

FullAreaResponses responses = actual.as(FullAreaResponses.class);

// then
assertSoftly(softly -> {
softly.assertThat(actual.statusCode()).isEqualTo(OK.value());
softly.assertThat(responses.areas()).usingRecursiveComparison()
.isEqualTo(
List.of(
FullAreaResponse.from(지역1),
FullAreaResponse.from(지역2),
FullAreaResponse.from(지역3),
FullAreaResponse.from(지역4)
));
});

}

@MethodSource("paramsAndresponse")
Expand All @@ -72,7 +106,7 @@ public void setUp() {

// then
AreaResponse areaResponse = actual.as(AreaResponse.class);
SoftAssertions.assertSoftly(softly -> {
assertSoftly(softly -> {
softly.assertThat(actual.statusCode()).isEqualTo(OK.value());
softly.assertThat(areaResponse).isEqualTo(response);
});
Expand Down

0 comments on commit 172958c

Please sign in to comment.