Skip to content

Commit

Permalink
feat: response 구조 변경
Browse files Browse the repository at this point in the history
전체 노선 조회 response 구조 변경
  • Loading branch information
kih1015 committed Dec 7, 2024
1 parent 91c34cb commit c835f5f
Showing 1 changed file with 32 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import static com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.annotation.JsonNaming;
Expand All @@ -16,34 +14,44 @@
@JsonNaming(SnakeCaseStrategy.class)
@Schema(description = "셔틀버스 경로 응답")
public record ShuttleBusRoutesResponse(
@Schema(description = "순환 노선 경로 목록")
List<RouteRegion> shuttleRoutes,

@Schema(description = "주중 노선 경로 목록")
List<RouteRegion> commutingRoutes,

@Schema(description = "주말 노선 경로 목록")
List<RouteRegion> weekendRoutes,
@Schema(description = "노선 지역 분류 목록")
List<RouteRegion> routeRegions,

@Schema(description = "학기 정보")
RouteSemester semesterInfo
) {

@JsonNaming(SnakeCaseStrategy.class)
@Schema(description = "경로 지역 정보")
@Schema(description = "노선 지역 정보")
private record RouteRegion(
@Schema(description = "지역 이름", example = "천안") String region,
@Schema(description = "해당 지역의 경로 목록") List<RouteName> routes
) {

public static List<RouteRegion> mapCategories(List<ShuttleBusRoute> shuttleBusRoutes) {
return shuttleBusRoutes.stream()
.collect(Collectors.groupingBy(ShuttleBusRoute::getRegion))
.entrySet().stream()
.map(entry -> new RouteRegion(entry.getKey(), RouteName.mapRouteNames(entry.getValue())))
.toList();
}
}

@JsonNaming(SnakeCaseStrategy.class)
@Schema(description = "경로 이름 정보")
@Schema(description = "노선 세부 정보")
private record RouteName(
@Schema(description = "경로 ID", example = "675013f9465776d6265ddfdb") String id,
@Schema(description = "경로 이름", example = "대학원") String routeName,
@Schema(description = "경로 부가 이름", example = "토요일") String subName
@Schema(description = "노선 ID", example = "675013f9465776d6265ddfdb") String id,
@Schema(description = "노선 종류", example = "주말") String type,
@Schema(description = "노선 이름", example = "대학원") String routeName,
@Schema(description = "노선 부가 이름", example = "토요일") String subName
) {

public static List<RouteName> mapRouteNames(List<ShuttleBusRoute> routes) {
return routes.stream()
.map(route -> new RouteName(route.getId(), route.getRouteType(), route.getRouteName(),
route.getSubName()))
.toList();
}
}

@JsonNaming(SnakeCaseStrategy.class)
Expand All @@ -52,30 +60,17 @@ private record RouteSemester(
@Schema(description = "학기 이름", example = "정규학기") String name,
@Schema(description = "학기 기간", example = "2024-09-02 ~ 2024-12-20") String term
) {

public static RouteSemester from(VersionMessageResponse versionMessageResponse) {
return new RouteSemester(versionMessageResponse.title(),
versionMessageResponse.content());
}
}

public static ShuttleBusRoutesResponse of(List<ShuttleBusRoute> shuttleBusRoutes,
VersionMessageResponse versionMessageResponse) {
Map<String, List<ShuttleBusRoute>> groupedByRouteType = shuttleBusRoutes.stream()
.collect(Collectors.groupingBy(ShuttleBusRoute::getRouteType));
List<RouteRegion> shuttleRoutes = mapRegions(groupedByRouteType.getOrDefault("순환", new ArrayList<>()));
List<RouteRegion> commutingRoutes = mapRegions(groupedByRouteType.getOrDefault("주중", new ArrayList<>()));
List<RouteRegion> weekendRoutes = mapRegions(groupedByRouteType.getOrDefault("주말", new ArrayList<>()));
RouteSemester routeSemester = new RouteSemester(versionMessageResponse.title(), versionMessageResponse.content());
return new ShuttleBusRoutesResponse(shuttleRoutes, commutingRoutes, weekendRoutes, routeSemester);
}

private static List<RouteRegion> mapRegions(List<ShuttleBusRoute> routes) {
return routes.stream()
.collect(Collectors.groupingBy(ShuttleBusRoute::getRegion))
.entrySet().stream()
.map(entry -> new RouteRegion(entry.getKey(), mapRouteNames(entry.getValue())))
.toList();
}

private static List<RouteName> mapRouteNames(List<ShuttleBusRoute> routes) {
return routes.stream()
.map(route -> new RouteName(route.getId(), route.getRouteName(), route.getSubName()))
.toList();
List<RouteRegion> categories = RouteRegion.mapCategories(shuttleBusRoutes);
RouteSemester routeSemester = RouteSemester.from(versionMessageResponse);
return new ShuttleBusRoutesResponse(categories, routeSemester);
}
}
}

0 comments on commit c835f5f

Please sign in to comment.