From 1f46696fc79cb3c463887f3c9a295f4a887ad777 Mon Sep 17 00:00:00 2001 From: PSH Date: Tue, 6 Aug 2024 11:51:52 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=B2=84=EC=8A=A4=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 노선 검색 API - 노선 등록 service - 정류장 검색 service - 정류장 등록 service - 노선별 경유 정류장 검색 service - 노선별 경유 정류장 등록 service --- .../bus/controller/BusRouteController.java | 34 +++++ .../talkka/server/bus/dao/BusRouteEntity.java | 11 +- .../server/bus/dao/BusRouteRepository.java | 12 +- .../server/bus/dao/BusRouteStationEntity.java | 2 +- .../bus/dao/BusRouteStationRepository.java | 5 + .../server/bus/dao/BusStationEntity.java | 12 +- .../server/bus/dao/BusStationRepository.java | 8 ++ .../server/bus/dto/BusRouteCreateDto.java | 70 +++++++++ .../server/bus/dto/BusRouteRespDto.java | 65 +++++++++ .../bus/dto/BusRouteStationCreateDto.java | 26 ++++ .../bus/dto/BusRouteStationRespDto.java | 40 ++++++ .../server/bus/dto/BusStationCreateDto.java | 47 ++++++ .../server/bus/dto/BusStationRespDto.java | 50 +++++++ .../server/bus/service/BusRouteService.java | 39 +++++ .../bus/service/BusRouteStationService.java | 59 ++++++++ .../server/bus/service/BusStationService.java | 38 +++++ .../server/review/dto/BusReviewRespDto.java | 4 +- .../bus/service/BusRouteServiceTest.java | 134 ++++++++++++++++++ .../review/service/BusReviewServiceTest.java | 4 +- 19 files changed, 649 insertions(+), 11 deletions(-) create mode 100644 server/src/main/java/com/talkka/server/bus/controller/BusRouteController.java create mode 100644 server/src/main/java/com/talkka/server/bus/dto/BusRouteCreateDto.java create mode 100644 server/src/main/java/com/talkka/server/bus/dto/BusRouteRespDto.java create mode 100644 server/src/main/java/com/talkka/server/bus/dto/BusRouteStationCreateDto.java create mode 100644 server/src/main/java/com/talkka/server/bus/dto/BusRouteStationRespDto.java create mode 100644 server/src/main/java/com/talkka/server/bus/dto/BusStationCreateDto.java create mode 100644 server/src/main/java/com/talkka/server/bus/dto/BusStationRespDto.java create mode 100644 server/src/main/java/com/talkka/server/bus/service/BusRouteService.java create mode 100644 server/src/main/java/com/talkka/server/bus/service/BusRouteStationService.java create mode 100644 server/src/main/java/com/talkka/server/bus/service/BusStationService.java create mode 100644 server/src/test/java/com/talkka/server/bus/service/BusRouteServiceTest.java diff --git a/server/src/main/java/com/talkka/server/bus/controller/BusRouteController.java b/server/src/main/java/com/talkka/server/bus/controller/BusRouteController.java new file mode 100644 index 00000000..88a64eed --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/controller/BusRouteController.java @@ -0,0 +1,34 @@ +package com.talkka.server.bus.controller; + +import java.util.List; + +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.talkka.server.bus.dto.BusRouteRespDto; +import com.talkka.server.bus.service.BusRouteService; +import com.talkka.server.common.dto.ApiRespDto; +import com.talkka.server.common.enums.StatusCode; + +import lombok.RequiredArgsConstructor; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/bus/route") +public class BusRouteController { + private final BusRouteService busRouteService; + + @GetMapping("") + public ResponseEntity>> findByRouteName(@RequestParam("search") String routeName) { + return ResponseEntity.ok( + ApiRespDto.>builder() + .statusCode(StatusCode.OK.getCode()) + .message(StatusCode.OK.getMessage()) + .data(busRouteService.findByRouteName(routeName)) + .build() + ); + } +} diff --git a/server/src/main/java/com/talkka/server/bus/dao/BusRouteEntity.java b/server/src/main/java/com/talkka/server/bus/dao/BusRouteEntity.java index 99d59742..ebb9aea5 100644 --- a/server/src/main/java/com/talkka/server/bus/dao/BusRouteEntity.java +++ b/server/src/main/java/com/talkka/server/bus/dao/BusRouteEntity.java @@ -36,8 +36,11 @@ public class BusRouteEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "route_id", nullable = false) - private Long routeId; + @Column(name = "route_id") + private Long id; + + @Column(name = "api_route_id", nullable = false, length = 40) + private String apiRouteId; // 공공 api 에서 제공해주는 식별자 @Column(name = "route_name", nullable = false, length = 50) private String routeName; // 노선 번호 @@ -132,11 +135,11 @@ public boolean equals(Object o) { } BusRouteEntity that = (BusRouteEntity)o; - return getRouteId().equals(that.getRouteId()); + return getId().equals(that.getId()); } @Override public int hashCode() { - return getRouteId().hashCode(); + return getId().hashCode(); } } diff --git a/server/src/main/java/com/talkka/server/bus/dao/BusRouteRepository.java b/server/src/main/java/com/talkka/server/bus/dao/BusRouteRepository.java index a2be9fe3..64562d95 100644 --- a/server/src/main/java/com/talkka/server/bus/dao/BusRouteRepository.java +++ b/server/src/main/java/com/talkka/server/bus/dao/BusRouteRepository.java @@ -1,8 +1,18 @@ package com.talkka.server.bus.dao; +import java.util.List; +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; - + @Repository public interface BusRouteRepository extends JpaRepository { + + boolean existsByApiRouteId(String apiRouteId); + + Optional findByApiRouteId(String apiRouteId); + + List findByRouteNameLikeOrderByRouteNameAsc(String routeName); + } diff --git a/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationEntity.java b/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationEntity.java index e8fe23a7..1ed763ff 100644 --- a/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationEntity.java +++ b/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationEntity.java @@ -32,7 +32,7 @@ public class BusRouteStationEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "bus_route_station_id", nullable = false) - private Long busRouteStationId; + private Long id; @ManyToOne @JoinColumn(name = "route_id") diff --git a/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationRepository.java b/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationRepository.java index 08e103d0..ad1785b8 100644 --- a/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationRepository.java +++ b/server/src/main/java/com/talkka/server/bus/dao/BusRouteStationRepository.java @@ -1,8 +1,13 @@ package com.talkka.server.bus.dao; +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface BusRouteStationRepository extends JpaRepository { + List findByRouteId(Long routeId); + + List findByStationId(Long stationId); } diff --git a/server/src/main/java/com/talkka/server/bus/dao/BusStationEntity.java b/server/src/main/java/com/talkka/server/bus/dao/BusStationEntity.java index 2f3f82a3..0ac5fac3 100644 --- a/server/src/main/java/com/talkka/server/bus/dao/BusStationEntity.java +++ b/server/src/main/java/com/talkka/server/bus/dao/BusStationEntity.java @@ -17,6 +17,8 @@ import jakarta.persistence.Convert; import jakarta.persistence.Entity; import jakarta.persistence.EntityListeners; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import lombok.AllArgsConstructor; import lombok.Builder; @@ -32,7 +34,15 @@ public class BusStationEntity { @Id - private Long stationId; + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "station_id") + private Long id; + + @Column(name = "api_station_id", nullable = false, length = 40) + private String apiStationId; + + @Column(name = "station_name", nullable = false, length = 100) + private String stationName; @Column(name = "region_name", nullable = false, length = 100) private String regionName; diff --git a/server/src/main/java/com/talkka/server/bus/dao/BusStationRepository.java b/server/src/main/java/com/talkka/server/bus/dao/BusStationRepository.java index 32b2a30b..c0da3389 100644 --- a/server/src/main/java/com/talkka/server/bus/dao/BusStationRepository.java +++ b/server/src/main/java/com/talkka/server/bus/dao/BusStationRepository.java @@ -1,8 +1,16 @@ package com.talkka.server.bus.dao; +import java.util.List; +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface BusStationRepository extends JpaRepository { + Optional findByApiStationId(String apiStationId); + + List findByStationNameLikeOrderByStationNameAsc(String stationName); + + boolean existsByApiStationId(String apiStationId); } diff --git a/server/src/main/java/com/talkka/server/bus/dto/BusRouteCreateDto.java b/server/src/main/java/com/talkka/server/bus/dto/BusRouteCreateDto.java new file mode 100644 index 00000000..3d25ddbd --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/dto/BusRouteCreateDto.java @@ -0,0 +1,70 @@ +package com.talkka.server.bus.dto; + +import com.talkka.server.bus.dao.BusRouteEntity; +import com.talkka.server.bus.enums.BusRouteType; +import com.talkka.server.bus.enums.DistrictCode; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@Builder +@ToString +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class BusRouteCreateDto { + + private String apiRouteId; // 공공 api 에서 제공해주는 식별자 + private String routeName; // 노선 번호 + private BusRouteType routeTypeCd; // 노선 유형 코드 + private String routeTypeName; // 노선 유형명 + private String companyId; // 운수업체 아이디 + private String companyName; // 운수업체명 + private String companyTel; // 운수업체 전화번호 + private DistrictCode districtCd; // 관할 지역 코드 + private String upFirstTime; // 평일 기점 첫차 시간 + private String upLastTime; // 평일 기점 막차 시간 + private String downFirstTime; // 평일 종점 첫차 시간 + private String downLastTime; // 평일 종점 막차 시간 + private String startMobileNo; // 기점 정류소 번호 + private Long startStationId; // 기점 정류소 아이디 + private String startStationName; // 기점 정류소명 + private Long endStationId; // 종점 정류소 아이디 + private String endMobileNo; // 종점 정류소 번호 + private String endStationName; // 종점 정류소명 + private String regionName; // 지역명 + private Integer peekAlloc; // 평일 최소 배차 시간 + private Integer nPeekAlloc; // 평일 최대 배차 시간 + + public BusRouteEntity toEntity() { + return BusRouteEntity.builder() + .apiRouteId(apiRouteId) + .routeName(routeName) + .routeTypeCd(routeTypeCd) + .routeTypeName(routeTypeName) + .companyId(companyId) + .companyName(companyName) + .companyTel(companyTel) + .districtCd(districtCd) + .upFirstTime(upFirstTime) + .upLastTime(upLastTime) + .downFirstTime(downFirstTime) + .downLastTime(downLastTime) + .startMobileNo(startMobileNo) + .startStationId(startStationId) + .startStationName(startStationName) + .endStationId(endStationId) + .endMobileNo(endMobileNo) + .endStationName(endStationName) + .regionName(regionName) + .peekAlloc(peekAlloc) + .nPeekAlloc(nPeekAlloc) + .build(); + } +} diff --git a/server/src/main/java/com/talkka/server/bus/dto/BusRouteRespDto.java b/server/src/main/java/com/talkka/server/bus/dto/BusRouteRespDto.java new file mode 100644 index 00000000..07cd217f --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/dto/BusRouteRespDto.java @@ -0,0 +1,65 @@ +package com.talkka.server.bus.dto; + +import com.talkka.server.bus.dao.BusRouteEntity; +import com.talkka.server.bus.enums.BusRouteType; +import com.talkka.server.bus.enums.DistrictCode; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@Builder +@ToString +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class BusRouteRespDto { + + private Long routeId; + private String routeName; // 노선 번호 + private BusRouteType routeTypeCd; // 노선 유형 코드 + private String routeTypeName; // 노선 유형명 + private DistrictCode districtCd; // 관할 지역 코드 + private String upFirstTime; // 평일 기점 첫차 시간 + private String upLastTime; // 평일 기점 막차 시간 + private String downFirstTime; // 평일 종점 첫차 시간 + private String downLastTime; // 평일 종점 막차 시간 + private String startMobileNo; // 기점 정류소 번호 + private Long startStationId; // 기점 정류소 아이디 + private String startStationName; // 기점 정류소명 + private Long endStationId; // 종점 정류소 아이디 + private String endMobileNo; // 종점 정류소 번호 + private String endStationName; // 종점 정류소명 + private String regionName; // 지역명 + private Integer peekAlloc; // 평일 최소 배차 시간 + private Integer nPeekAlloc; // 평일 최대 배차 시간 + + public static BusRouteRespDto of(BusRouteEntity busRouteEntity) { + return BusRouteRespDto.builder() + .routeId(busRouteEntity.getId()) + .routeName(busRouteEntity.getRouteName()) + .routeTypeCd(busRouteEntity.getRouteTypeCd()) + .routeTypeName(busRouteEntity.getRouteTypeName()) + .districtCd(busRouteEntity.getDistrictCd()) + .upFirstTime(busRouteEntity.getUpFirstTime()) + .upLastTime(busRouteEntity.getUpLastTime()) + .downFirstTime(busRouteEntity.getDownFirstTime()) + .downLastTime(busRouteEntity.getDownLastTime()) + .startMobileNo(busRouteEntity.getStartMobileNo()) + .startStationId(busRouteEntity.getStartStationId()) + .startStationName(busRouteEntity.getStartStationName()) + .endStationId(busRouteEntity.getEndStationId()) + .endMobileNo(busRouteEntity.getEndMobileNo()) + .endStationName(busRouteEntity.getEndStationName()) + .regionName(busRouteEntity.getRegionName()) + .peekAlloc(busRouteEntity.getPeekAlloc()) + .nPeekAlloc(busRouteEntity.getNPeekAlloc()) + .build(); + } + +} diff --git a/server/src/main/java/com/talkka/server/bus/dto/BusRouteStationCreateDto.java b/server/src/main/java/com/talkka/server/bus/dto/BusRouteStationCreateDto.java new file mode 100644 index 00000000..a3004543 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/dto/BusRouteStationCreateDto.java @@ -0,0 +1,26 @@ +package com.talkka.server.bus.dto; + +import com.talkka.server.bus.dao.BusStationEntity; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@Builder +@ToString +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class BusRouteStationCreateDto { + + private String apiRouteId; + private String apiStationId; + private BusStationEntity station; + private Short stationSeq; + private String stationName; +} diff --git a/server/src/main/java/com/talkka/server/bus/dto/BusRouteStationRespDto.java b/server/src/main/java/com/talkka/server/bus/dto/BusRouteStationRespDto.java new file mode 100644 index 00000000..d2738a64 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/dto/BusRouteStationRespDto.java @@ -0,0 +1,40 @@ +package com.talkka.server.bus.dto; + +import java.time.LocalDateTime; + +import com.talkka.server.bus.dao.BusRouteStationEntity; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@Builder +@ToString +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class BusRouteStationRespDto { + + private Long busRouteStationId; + private BusRouteRespDto route; + private BusStationRespDto station; + private Short stationSeq; + private String stationName; + private LocalDateTime createdAt; + + public static BusRouteStationRespDto of(BusRouteStationEntity busRouteStationEntity) { + return BusRouteStationRespDto.builder() + .busRouteStationId(busRouteStationEntity.getId()) + .route(BusRouteRespDto.of(busRouteStationEntity.getRoute())) + .station(BusStationRespDto.of(busRouteStationEntity.getStation())) + .stationSeq(busRouteStationEntity.getStationSeq()) + .stationName(busRouteStationEntity.getStationName()) + .createdAt(busRouteStationEntity.getCreatedAt()) + .build(); + } +} diff --git a/server/src/main/java/com/talkka/server/bus/dto/BusStationCreateDto.java b/server/src/main/java/com/talkka/server/bus/dto/BusStationCreateDto.java new file mode 100644 index 00000000..4ac65cad --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/dto/BusStationCreateDto.java @@ -0,0 +1,47 @@ +package com.talkka.server.bus.dto; + +import java.math.BigDecimal; + +import com.talkka.server.bus.dao.BusStationEntity; +import com.talkka.server.bus.enums.CenterStation; +import com.talkka.server.bus.enums.DistrictCode; +import com.talkka.server.bus.enums.TurnStation; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@Builder +@ToString +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class BusStationCreateDto { + + private String apiStationId; + private String stationName; + private String regionName; + private DistrictCode districtCd; + private CenterStation centerYn; + private TurnStation turnYn; + private BigDecimal longitude; + private BigDecimal latitude; + + public BusStationEntity toEntity() { + return BusStationEntity.builder() + .apiStationId(apiStationId) + .stationName(stationName) + .regionName(regionName) + .districtCd(districtCd) + .centerYn(centerYn) + .turnYn(turnYn) + .longitude(longitude) + .latitude(latitude) + .build(); + } +} diff --git a/server/src/main/java/com/talkka/server/bus/dto/BusStationRespDto.java b/server/src/main/java/com/talkka/server/bus/dto/BusStationRespDto.java new file mode 100644 index 00000000..f7e02d32 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/dto/BusStationRespDto.java @@ -0,0 +1,50 @@ +package com.talkka.server.bus.dto; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +import com.talkka.server.bus.dao.BusStationEntity; +import com.talkka.server.bus.enums.CenterStation; +import com.talkka.server.bus.enums.DistrictCode; +import com.talkka.server.bus.enums.TurnStation; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@Builder +@ToString +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class BusStationRespDto { + + private Long stationId; + private String stationName; + private String regionName; + private DistrictCode districtCd; + private CenterStation centerYn; + private TurnStation turnYn; + private BigDecimal longitude; + private BigDecimal latitude; + private LocalDateTime createdAt; + + public static BusStationRespDto of(BusStationEntity busStationEntity) { + return BusStationRespDto.builder() + .stationId(busStationEntity.getId()) + .stationName(busStationEntity.getStationName()) + .regionName(busStationEntity.getRegionName()) + .districtCd(busStationEntity.getDistrictCd()) + .centerYn(busStationEntity.getCenterYn()) + .turnYn(busStationEntity.getTurnYn()) + .longitude(busStationEntity.getLongitude()) + .latitude(busStationEntity.getLatitude()) + .createdAt(busStationEntity.getCreatedAt()) + .build(); + } +} diff --git a/server/src/main/java/com/talkka/server/bus/service/BusRouteService.java b/server/src/main/java/com/talkka/server/bus/service/BusRouteService.java new file mode 100644 index 00000000..1d500b25 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/service/BusRouteService.java @@ -0,0 +1,39 @@ +package com.talkka.server.bus.service; + +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.talkka.server.bus.dao.BusRouteEntity; +import com.talkka.server.bus.dao.BusRouteRepository; +import com.talkka.server.bus.dto.BusRouteCreateDto; +import com.talkka.server.bus.dto.BusRouteRespDto; +import com.talkka.server.common.exception.http.BadRequestException; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class BusRouteService { + private final BusRouteRepository busRouteRepository; + + public BusRouteRespDto findByRouteId(Long routeId) { + BusRouteEntity busRouteEntity = busRouteRepository.findById(routeId) + .orElseThrow(() -> new BadRequestException("존재하지 않는 노선입니다.")); + return BusRouteRespDto.of(busRouteEntity); + } + + public List findByRouteName(String routeName) { + return busRouteRepository.findByRouteNameLikeOrderByRouteNameAsc(routeName).stream() + .map(BusRouteRespDto::of) + .toList(); + } + + public BusRouteRespDto createBusRoute(BusRouteCreateDto busRouteCreateDto) { + if (!busRouteRepository.existsByApiRouteId(busRouteCreateDto.getApiRouteId())) { + throw new BadRequestException("이미 등록된 버스 노선입니다."); + } + return BusRouteRespDto.of(busRouteRepository.save(busRouteCreateDto.toEntity())); + } + +} diff --git a/server/src/main/java/com/talkka/server/bus/service/BusRouteStationService.java b/server/src/main/java/com/talkka/server/bus/service/BusRouteStationService.java new file mode 100644 index 00000000..9227f7f9 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/service/BusRouteStationService.java @@ -0,0 +1,59 @@ +package com.talkka.server.bus.service; + +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.talkka.server.bus.dao.BusRouteEntity; +import com.talkka.server.bus.dao.BusRouteRepository; +import com.talkka.server.bus.dao.BusRouteStationEntity; +import com.talkka.server.bus.dao.BusRouteStationRepository; +import com.talkka.server.bus.dao.BusStationEntity; +import com.talkka.server.bus.dao.BusStationRepository; +import com.talkka.server.bus.dto.BusRouteStationCreateDto; +import com.talkka.server.bus.dto.BusRouteStationRespDto; +import com.talkka.server.common.exception.http.BadRequestException; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class BusRouteStationService { + + private final BusRouteRepository routeRepository; + private final BusStationRepository stationRepository; + private final BusRouteStationRepository routeStationRepository; + + public BusRouteStationRespDto createBusRouteStation(BusRouteStationCreateDto busRouteStationCreateDto) { + BusRouteEntity routeEntity = routeRepository.findByApiRouteId(busRouteStationCreateDto.getApiRouteId()) + .orElseThrow(() -> new BadRequestException("존재하지 않는 노선입니다.")); + BusStationEntity stationEntity = stationRepository.findByApiStationId( + busRouteStationCreateDto.getApiStationId()) + .orElseThrow(() -> new BadRequestException("존재하지 않는 정류장입니다.")); + BusRouteStationEntity busRouteStationEntity = BusRouteStationEntity.builder() + .route(routeEntity) + .station(stationEntity) + .stationSeq(busRouteStationCreateDto.getStationSeq()) + .stationName(busRouteStationCreateDto.getStationName()) + .build(); + return BusRouteStationRespDto.of(routeStationRepository.save(busRouteStationEntity)); + } + + public BusRouteStationRespDto findById(Long id) { + BusRouteStationEntity busRouteStationEntity = routeStationRepository.findById(id) + .orElseThrow(() -> new BadRequestException("존재하지 않는 노선정류장입니다.")); + return BusRouteStationRespDto.of(busRouteStationEntity); + } + + public List findByRouteId(Long routeId) { + return routeStationRepository.findByRouteId(routeId).stream() + .map(BusRouteStationRespDto::of) + .toList(); + } + + public List findByStationId(Long stationId) { + return routeStationRepository.findByStationId(stationId).stream() + .map(BusRouteStationRespDto::of) + .toList(); + } +} diff --git a/server/src/main/java/com/talkka/server/bus/service/BusStationService.java b/server/src/main/java/com/talkka/server/bus/service/BusStationService.java new file mode 100644 index 00000000..e3ee83ed --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/service/BusStationService.java @@ -0,0 +1,38 @@ +package com.talkka.server.bus.service; + +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.talkka.server.bus.dao.BusStationEntity; +import com.talkka.server.bus.dao.BusStationRepository; +import com.talkka.server.bus.dto.BusStationCreateDto; +import com.talkka.server.bus.dto.BusStationRespDto; +import com.talkka.server.common.exception.http.BadRequestException; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class BusStationService { + private final BusStationRepository busStationRepository; + + public BusStationRespDto findByStationId(Long stationId) { + BusStationEntity busStationEntity = busStationRepository.findById(stationId) + .orElseThrow(() -> new BadRequestException("존재하지 않는 정거장입니다.")); + return BusStationRespDto.of(busStationEntity); + } + + public List findByStationName(String stationName) { + return busStationRepository.findByStationNameLikeOrderByStationNameAsc(stationName).stream() + .map(BusStationRespDto::of) + .toList(); + } + + public BusStationRespDto createBusStation(BusStationCreateDto busStationCreateDto) { + if (busStationRepository.existsByApiStationId(busStationCreateDto.getApiStationId())) { + throw new BadRequestException("이미 등록된 정거장입니다."); + } + return BusStationRespDto.of(busStationRepository.save(busStationCreateDto.toEntity())); + } +} diff --git a/server/src/main/java/com/talkka/server/review/dto/BusReviewRespDto.java b/server/src/main/java/com/talkka/server/review/dto/BusReviewRespDto.java index 29abd661..ac465392 100644 --- a/server/src/main/java/com/talkka/server/review/dto/BusReviewRespDto.java +++ b/server/src/main/java/com/talkka/server/review/dto/BusReviewRespDto.java @@ -28,8 +28,8 @@ public class BusReviewRespDto { public static BusReviewRespDto of(BusReviewEntity busEntity) { return new BusReviewRespDto( busEntity.getWriter().getUserId(), - busEntity.getRoute().getRouteId(), - busEntity.getStation().getBusRouteStationId(), + busEntity.getRoute().getId(), + busEntity.getStation().getId(), busEntity.getContent(), busEntity.getTimeSlot(), busEntity.getRating()); diff --git a/server/src/test/java/com/talkka/server/bus/service/BusRouteServiceTest.java b/server/src/test/java/com/talkka/server/bus/service/BusRouteServiceTest.java new file mode 100644 index 00000000..6c6a5102 --- /dev/null +++ b/server/src/test/java/com/talkka/server/bus/service/BusRouteServiceTest.java @@ -0,0 +1,134 @@ +package com.talkka.server.bus.service; + +import static org.assertj.core.api.AssertionsForClassTypes.*; +import static org.mockito.BDDMockito.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.talkka.server.bus.dao.BusRouteEntity; +import com.talkka.server.bus.dao.BusRouteRepository; +import com.talkka.server.bus.dto.BusRouteCreateDto; +import com.talkka.server.bus.dto.BusRouteRespDto; +import com.talkka.server.bus.enums.BusRouteType; +import com.talkka.server.bus.enums.DistrictCode; +import com.talkka.server.common.exception.http.BadRequestException; + +@ExtendWith(MockitoExtension.class) +public class BusRouteServiceTest { + + @InjectMocks + private BusRouteService busRouteService; + + @Mock + private BusRouteRepository busRouteRepository; + + private BusRouteCreateDto getBusRouteReqDto(String id) { + + return BusRouteCreateDto.builder() + .apiRouteId(id) + .routeName("7800" + id) + .routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS) + .routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName()) + .companyId("COMP123") + .companyName("수형운수") + .companyTel("02-123-4567") + .districtCd(DistrictCode.DONGDUCHEON) + .upFirstTime("05:30") + .upLastTime("23:00") + .downFirstTime("06:00") + .downLastTime("00:35") + .startMobileNo("101") + .startStationId(1001L) + .startStationName("기점 정류소") + .endStationId(2002L) + .endMobileNo("202") + .endStationName("종점 정류소") + .regionName("서울") + .peekAlloc(15) + .nPeekAlloc(25) + .build(); + } + + private BusRouteRespDto getBusRouteRespDto(Long id) { + return BusRouteRespDto.builder() + .routeId(id) + .routeName("7800" + id) + .routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS) + .routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName()) + .districtCd(DistrictCode.DONGDUCHEON) + .upFirstTime("05:30") + .upLastTime("23:00") + .downFirstTime("06:00") + .downLastTime("00:35") + .startMobileNo("101") + .startStationId(1001L) + .startStationName("기점 정류소") + .endStationId(2002L) + .endMobileNo("202") + .endStationName("종점 정류소") + .regionName("서울") + .peekAlloc(15) + .nPeekAlloc(25) + .build(); + } + + @Nested + @DisplayName("createBusRoute method") + public class CreateBusRouteTest { + @Test + void 버스_노선을_생성한다() { + // given + BusRouteCreateDto busRouteCreateDto = getBusRouteReqDto("1"); + BusRouteRespDto busRouteRespDto = getBusRouteRespDto(1L); + BusRouteEntity busRouteEntity = BusRouteEntity.builder() + .id(1L) + .apiRouteId(busRouteCreateDto.getApiRouteId()) + .routeName(busRouteCreateDto.getRouteName()) + .routeTypeCd(BusRouteType.DIRECT_SEAT_CITY_BUS) + .routeTypeName(BusRouteType.DIRECT_SEAT_CITY_BUS.getName()) + .companyId("COMP123") + .companyName("수형운수") + .companyTel("02-123-4567") + .districtCd(DistrictCode.DONGDUCHEON) + .upFirstTime("05:30") + .upLastTime("23:00") + .downFirstTime("06:00") + .downLastTime("00:35") + .startMobileNo("101") + .startStationId(1001L) + .startStationName("기점 정류소") + .endStationId(2002L) + .endMobileNo("202") + .endStationName("종점 정류소") + .regionName("서울") + .peekAlloc(15) + .nPeekAlloc(25) + .build(); + given(busRouteRepository.save(any(BusRouteEntity.class))).willReturn(busRouteEntity); + given(busRouteRepository.existsByApiRouteId(any(String.class))).willReturn(true); + // when + BusRouteRespDto result = busRouteService.createBusRoute(busRouteCreateDto); + // then + assertThat(result).isEqualTo(busRouteRespDto); + } + + @Test + void 존재하는_버스_노선일_경우_Exception을_발생시킨다() { + // given + BusRouteCreateDto reqDto = getBusRouteReqDto("1"); + given(busRouteRepository.existsByApiRouteId(any(String.class))).willReturn(false); + // when + // then + assertThatThrownBy( + () -> busRouteService.createBusRoute(reqDto) + ).isInstanceOf(BadRequestException.class) + .hasMessage("이미 등록된 버스 노선입니다."); + } + } +} diff --git a/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java b/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java index 22dc39e8..c2d625be 100644 --- a/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java +++ b/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java @@ -62,13 +62,13 @@ private UserEntity getUserFixture(Long userId) { private BusRouteStationEntity getBusRouteStationFixture(Long busRouteStationId) { return BusRouteStationEntity.builder() - .busRouteStationId(busRouteStationId) + .id(busRouteStationId) .build(); } private BusRouteEntity getBusRouteFixture(Long routeId) { return BusRouteEntity.builder() - .routeId(routeId) + .id(routeId) .build(); }