Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] FE 통합에 따른 API 변경 (#65) #66

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;
Expand All @@ -21,24 +22,32 @@
public class BusRouteController {
private final BusRouteService busRouteService;

@GetMapping("/name/{name}")
public ResponseEntity<ApiRespDto<List<BusRouteRespDto>>> findByRouteName(@PathVariable("name") String routeName) {
@GetMapping("")
public ResponseEntity<ApiRespDto<List<BusRouteRespDto>>> getRoutes(
@RequestParam(value = "search", required = false) String routeName) {
List<BusRouteRespDto> routeList;

if (routeName != null) {
routeList = busRouteService.getRoutesByRouteName(routeName);
} else {
routeList = busRouteService.getRoutes();
}
return ResponseEntity.ok(
ApiRespDto.<List<BusRouteRespDto>>builder()
.statusCode(StatusCode.OK.getCode())
.message(StatusCode.OK.getMessage())
.data(busRouteService.findByRouteName(routeName))
.data(routeList)
.build()
);
}

@GetMapping("/id/{id}")
public ResponseEntity<ApiRespDto<BusRouteRespDto>> findByRouteId(@PathVariable("id") Long routeId) {
@GetMapping("/{id}")
public ResponseEntity<ApiRespDto<BusRouteRespDto>> getRouteById(@PathVariable("id") Long routeId) {
return ResponseEntity.ok(
ApiRespDto.<BusRouteRespDto>builder()
.statusCode(StatusCode.OK.getCode())
.message(StatusCode.OK.getMessage())
.data(busRouteService.findByRouteId(routeId))
.data(busRouteService.getRouteById(routeId))
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,38 @@
public class BusRouteStationController {
private final BusRouteStationService routeStationService;

@GetMapping("/id/{id}")
public ResponseEntity<ApiRespDto<List<BusRouteStationRespDto>>> findById(
@GetMapping("/{id}")
public ResponseEntity<ApiRespDto<BusRouteStationRespDto>> getRouteStationById(
@PathVariable("id") Long routeStationId) {
return ResponseEntity.ok(
ApiRespDto.<List<BusRouteStationRespDto>>builder()
.statusCode(StatusCode.OK.getCode())
.message(StatusCode.OK.getMessage())
.data(routeStationService.findByStationId(routeStationId))
.build()
);
}

@GetMapping("/routeId/{routeId}")
public ResponseEntity<ApiRespDto<List<BusRouteStationRespDto>>> findByRouteId(
@RequestParam("routeId") Long routeId) {
return ResponseEntity.ok(
ApiRespDto.<List<BusRouteStationRespDto>>builder()
ApiRespDto.<BusRouteStationRespDto>builder()
.statusCode(StatusCode.OK.getCode())
.message(StatusCode.OK.getMessage())
.data(routeStationService.findByRouteId(routeId))
.data(routeStationService.getRouteStationById(routeStationId))
.build()
);
}

@GetMapping("/stationId/{stationId}")
public ResponseEntity<ApiRespDto<List<BusRouteStationRespDto>>> findByStationId(
@RequestParam("stationId") Long stationId) {
@GetMapping("")
public ResponseEntity<ApiRespDto<List<BusRouteStationRespDto>>> getRouteStations(
@RequestParam(value = "routeId", required = false) Long routeId,
@RequestParam(value = "stationId", required = false) Long stationId) {
List<BusRouteStationRespDto> routeStationList;

if (routeId != null && stationId != null) {
routeStationList = routeStationService.getRouteStationsByRouteIdAndStationId(routeId, stationId);
} else if (routeId != null) {
routeStationList = routeStationService.getRouteStationsByRouteId(routeId);
} else if (stationId != null) {
routeStationList = routeStationService.getRouteStationsByStationId(stationId);
} else {
routeStationList = routeStationService.getRouteStations();
}
return ResponseEntity.ok(
ApiRespDto.<List<BusRouteStationRespDto>>builder()
.statusCode(StatusCode.OK.getCode())
.message(StatusCode.OK.getMessage())
.data(routeStationService.findByStationId(stationId))
.data(routeStationList)
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -21,25 +22,32 @@
public class BusStationController {
private final BusStationService stationService;

@GetMapping("/name/{name}")
public ResponseEntity<ApiRespDto<List<BusStationRespDto>>> findByStationName(
@RequestParam("name") String stationName) {
@GetMapping("")
public ResponseEntity<ApiRespDto<List<BusStationRespDto>>> getStations(
@RequestParam(value = "search", required = false) String stationName) {
List<BusStationRespDto> stationList;

if (stationName != null) {
stationList = stationService.getStationsByStationName(stationName);
} else {
stationList = stationService.getStations();
}
return ResponseEntity.ok(
ApiRespDto.<List<BusStationRespDto>>builder()
.statusCode(StatusCode.OK.getCode())
.message(StatusCode.OK.getMessage())
.data(stationService.findByStationName(stationName))
.data(stationList)
.build()
);
}

@GetMapping("/id/{id}")
public ResponseEntity<ApiRespDto<BusStationRespDto>> findByStationId(@RequestParam("id") Long stationId) {
@GetMapping("/{id}")
public ResponseEntity<ApiRespDto<BusStationRespDto>> getStationById(@PathVariable("id") Long stationId) {
return ResponseEntity.ok(
ApiRespDto.<BusStationRespDto>builder()
.statusCode(StatusCode.OK.getCode())
.message(StatusCode.OK.getMessage())
.data(stationService.findByStationId(stationId))
.data(stationService.getStationById(stationId))
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface BusRouteRepository extends JpaRepository<BusRouteEntity, Long>

Optional<BusRouteEntity> findByApiRouteId(String apiRouteId);

List<BusRouteEntity> findByRouteNameLikeOrderByRouteNameAsc(String routeName);
List<BusRouteEntity> findAllByRouteNameLikeOrderByRouteNameAsc(String routeName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

@Repository
public interface BusRouteStationRepository extends JpaRepository<BusRouteStationEntity, Long> {
List<BusRouteStationEntity> findByRouteId(Long routeId);
List<BusRouteStationEntity> findAllByRouteId(Long routeId);

List<BusRouteStationEntity> findByStationId(Long stationId);
List<BusRouteStationEntity> findAllByStationId(Long stationId);

List<BusRouteStationEntity> findAllByRouteIdAndStationId(Long routeId, Long stationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
@Builder
public record BusRouteStationRespDto(
Long busRouteStationId,
BusRouteRespDto route,
BusStationRespDto station,
Short stationSeq,
String stationName,
Long routeId,
String routeName,
Long stationId,
LocalDateTime createdAt
) {
public static BusRouteStationRespDto of(BusRouteStationEntity busRouteStationEntity) {
return new BusRouteStationRespDto(
busRouteStationEntity.getId(),
BusRouteRespDto.of(busRouteStationEntity.getRoute()),
BusStationRespDto.of(busRouteStationEntity.getStation()),
busRouteStationEntity.getStationSeq(),
busRouteStationEntity.getStationName(),
busRouteStationEntity.getStation().getStationName(),
busRouteStationEntity.getRoute().getId(),
busRouteStationEntity.getRoute().getRouteName(),
busRouteStationEntity.getStation().getId(),
busRouteStationEntity.getCreatedAt()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@
public class BusRouteService {
private final BusRouteRepository busRouteRepository;

public BusRouteRespDto findByRouteId(Long routeId) {
public BusRouteRespDto getRouteById(Long routeId) {
BusRouteEntity busRouteEntity = busRouteRepository.findById(routeId)
.orElseThrow(() -> new BadRequestException("존재하지 않는 노선입니다."));
return BusRouteRespDto.of(busRouteEntity);
}

public List<BusRouteRespDto> findByRouteName(String routeName) {
return busRouteRepository.findByRouteNameLikeOrderByRouteNameAsc(routeName).stream()
public List<BusRouteRespDto> getRoutes() {
return busRouteRepository.findAll().stream()
.map(BusRouteRespDto::of)
.toList();
}

public BusRouteRespDto createBusRoute(BusRouteCreateDto busRouteCreateDto) {
public List<BusRouteRespDto> getRoutesByRouteName(String routeName) {
return busRouteRepository.findAllByRouteNameLikeOrderByRouteNameAsc(routeName).stream()
.map(BusRouteRespDto::of)
.toList();
}

public BusRouteRespDto createRoute(BusRouteCreateDto busRouteCreateDto) {
if (!busRouteRepository.existsByApiRouteId(busRouteCreateDto.apiRouteId())) {
throw new BadRequestException("이미 등록된 버스 노선입니다.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BusRouteStationService {
private final BusStationRepository stationRepository;
private final BusRouteStationRepository routeStationRepository;

public BusRouteStationRespDto createBusRouteStation(BusRouteStationCreateDto busRouteStationCreateDto) {
public BusRouteStationRespDto createRouteStation(BusRouteStationCreateDto busRouteStationCreateDto) {
BusRouteEntity routeEntity = routeRepository.findByApiRouteId(busRouteStationCreateDto.apiRouteId())
.orElseThrow(() -> new BadRequestException("존재하지 않는 노선입니다."));
BusStationEntity stationEntity = stationRepository.findByApiStationId(
Expand All @@ -34,20 +34,32 @@ public BusRouteStationRespDto createBusRouteStation(BusRouteStationCreateDto bus
return BusRouteStationRespDto.of(routeStationRepository.save(busRouteStationEntity));
}

public BusRouteStationRespDto findById(Long id) {
public BusRouteStationRespDto getRouteStationById(Long id) {
BusRouteStationEntity busRouteStationEntity = routeStationRepository.findById(id)
.orElseThrow(() -> new BadRequestException("존재하지 않는 노선정류장입니다."));
return BusRouteStationRespDto.of(busRouteStationEntity);
}

public List<BusRouteStationRespDto> findByRouteId(Long routeId) {
return routeStationRepository.findByRouteId(routeId).stream()
public List<BusRouteStationRespDto> getRouteStationsByRouteIdAndStationId(Long routeId, Long stationId) {
return routeStationRepository.findAllByRouteIdAndStationId(routeId, stationId).stream()
.map(BusRouteStationRespDto::of)
.toList();
}

public List<BusRouteStationRespDto> findByStationId(Long stationId) {
return routeStationRepository.findByStationId(stationId).stream()
public List<BusRouteStationRespDto> getRouteStationsByRouteId(Long routeId) {
return routeStationRepository.findAllByRouteId(routeId).stream()
.map(BusRouteStationRespDto::of)
.toList();
}

public List<BusRouteStationRespDto> getRouteStationsByStationId(Long stationId) {
return routeStationRepository.findAllByStationId(stationId).stream()
.map(BusRouteStationRespDto::of)
.toList();
}

public List<BusRouteStationRespDto> getRouteStations() {
return routeStationRepository.findAll().stream()
.map(BusRouteStationRespDto::of)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@
public class BusStationService {
private final BusStationRepository busStationRepository;

public BusStationRespDto findByStationId(Long stationId) {
public BusStationRespDto getStationById(Long stationId) {
BusStationEntity busStationEntity = busStationRepository.findById(stationId)
.orElseThrow(() -> new BadRequestException("존재하지 않는 정거장입니다."));
return BusStationRespDto.of(busStationEntity);
}

public List<BusStationRespDto> findByStationName(String stationName) {
public List<BusStationRespDto> getStationsByStationName(String stationName) {
return busStationRepository.findByStationNameLikeOrderByStationNameAsc(stationName).stream()
.map(BusStationRespDto::of)
.toList();
}

public BusStationRespDto createBusStation(BusStationCreateDto busStationCreateDto) {
public List<BusStationRespDto> getStations() {
return busStationRepository.findAll().stream()
.map(BusStationRespDto::of)
.toList();
}

public BusStationRespDto createStation(BusStationCreateDto busStationCreateDto) {
if (busStationRepository.existsByApiStationId(busStationCreateDto.apiStationId())) {
throw new BadRequestException("이미 등록된 정거장입니다.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ public class BusReviewController {

@GetMapping("")
public ResponseEntity<ApiRespDto<List<BusReviewRespDto>>> getBusReviewList(
@AuthenticationPrincipal OAuth2UserInfo oAuth2UserInfo,
@RequestParam Long routeId,
@RequestParam Long busRouteStationId,
@RequestParam String timeSlot
@RequestParam(required = true) Long routeId,
@RequestParam(required = false) Long busRouteStationId,
@RequestParam(required = false) String timeSlot
) {
List<BusReviewRespDto> reviewData = busReviewService.getBusReviewList(oAuth2UserInfo.getUserId(), routeId,
busRouteStationId, timeSlot);
List<BusReviewRespDto> reviewData;
if (busRouteStationId != null && timeSlot != null) {
reviewData = busReviewService.getBusReviewList(routeId, busRouteStationId, timeSlot);
} else if (busRouteStationId != null) {
reviewData = busReviewService.getBusReviewList(routeId, busRouteStationId);
} else {
reviewData = busReviewService.getBusReviewList(routeId);
}

return ResponseEntity.ok(
ApiRespDto.<List<BusReviewRespDto>>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@

@Repository
public interface BusReviewRepository extends JpaRepository<BusReviewEntity, Long> {
List<BusReviewEntity> findAllByWriterIdAndRouteIdAndStationIdAndTimeSlot(

List<BusReviewEntity> findAllByWriterIdAndRouteIdAndStationIdAndTimeSlotOrderByUpdatedAtDesc(
Long userId,
Long routeId,
Long busRouteStationId,
TimeSlot timeSlot);

List<BusReviewEntity> findAllByRouteIdOrderByCreatedAtDesc(Long routeId);

List<BusReviewEntity> findAllByRouteIdAndStationIdOrderByCreatedAtDesc(Long routeId, Long stationId);

List<BusReviewEntity> findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc(
Long routeId,
Long stationId,
TimeSlot timeSlot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
@Builder
public record BusReviewRespDto(
Long userId,
String userName,
Long routeId,
String routeName,
Long busRouteStationId,
String stationName,
String content,
String timeSlot,
Integer rating
) {
public static BusReviewRespDto of(BusReviewEntity busEntity) {
return new BusReviewRespDto(
busEntity.getWriter().getId(),
busEntity.getWriter().getName(),
busEntity.getRoute().getId(),
busEntity.getRoute().getRouteName(),
busEntity.getStation().getId(),
busEntity.getStation().getStationName(),
busEntity.getContent(),
busEntity.getTimeSlot().getCode(),
busEntity.getRating()
Expand Down
Loading
Loading