Skip to content

Commit

Permalink
[FEAT] 지하철 혼잡도 관련 코드 구현 (#85)
Browse files Browse the repository at this point in the history
## 작업내용 #59 
- 지하철 혼잡도 컨트롤러 구현
- 지하철 혼잡도 서비스 리팩토링
- Exception 처리 변경
  • Loading branch information
ss0ngcode authored Aug 19, 2024
1 parent f042173 commit 990176f
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.talkka.server.subway.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
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 com.talkka.server.common.exception.InvalidTypeException;
import com.talkka.server.subway.exception.ConfusionNotFoundException;
import com.talkka.server.subway.exception.StationNotFoundException;
import com.talkka.server.subway.service.SubwayConfusionService;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
@RequestMapping("/api/subway/confusion")
public class SubwayConfusionController {

private final SubwayConfusionService confusionService;

@GetMapping("/{stationId}")
public ResponseEntity<?> getConfusion(
@PathVariable Long stationId,
@RequestParam String dayType,
@RequestParam String updown,
@RequestParam String timeSlot
) {
ResponseEntity<?> response;
try {
response = ResponseEntity.ok(
confusionService.getConfusion(stationId, dayType, updown, timeSlot));
} catch (StationNotFoundException | InvalidTypeException exception) {
response = ResponseEntity.badRequest().body(exception.getMessage());
} catch (ConfusionNotFoundException exception) {
response = ResponseEntity.status(HttpStatus.NOT_FOUND).body(exception.getMessage());
}
return response;
}

@GetMapping("/{stationId}/list")
public ResponseEntity<?> getConfusionList(
@PathVariable Long stationId,
@RequestParam String dayType,
@RequestParam String updown,
@RequestParam(required = false, defaultValue = "0") String startTimeSlot,
@RequestParam(required = false, defaultValue = "47") String endTimeSlot
) {
ResponseEntity<?> response;
try {
response = ResponseEntity.ok(
confusionService.getConfusionList(stationId, dayType, updown, startTimeSlot, endTimeSlot)
);
} catch (StationNotFoundException | InvalidTypeException exception) {
response = ResponseEntity.badRequest().body(exception.getMessage());
}
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class SubwayConfusionEntity {
@Convert(converter = UpdownConverter.class)
private Updown updown;

@Column(name = "time_slot", nullable = false, length = 2)
@Column(name = "time_slot", nullable = false, length = 20)
@Enumerated(EnumType.STRING)
private TimeSlot timeSlot;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.talkka.server.subway.dao;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -13,4 +14,7 @@
public interface SubwayConfusionRepository extends JpaRepository<SubwayConfusionEntity, Long> {
Optional<SubwayConfusionEntity> findBySubwayStationIdAndDayTypeAndUpdownAndTimeSlot(
Long stationId, DayType dayType, Updown updown, TimeSlot timeSlot);

List<SubwayConfusionEntity> findBySubwayStationIdAndDayTypeAndUpdownAndTimeSlotBetween(
Long stationId, DayType dayType, Updown updown, TimeSlot startTimeSlot, TimeSlot endTimeSlot);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package com.talkka.server.subway.dto;

import com.talkka.server.common.enums.TimeSlot;
import com.talkka.server.subway.dao.SubwayConfusionEntity;
import com.talkka.server.subway.enums.DayType;
import com.talkka.server.subway.enums.Line;
import com.talkka.server.subway.enums.Updown;

import lombok.Builder;

@Builder
public record SubwayConfusionRespDto(
Long stationId,
String stationName,
Line line,
String dayType,
String updown,
String timeSlot,
DayType dayType,
Updown updown,
TimeSlot timeSlot,
Double confusion
) {
public static SubwayConfusionRespDto of(SubwayConfusionEntity subwayConfusionEntity) {
return new SubwayConfusionRespDto(
subwayConfusionEntity.getId(),
subwayConfusionEntity.getStationName(),
subwayConfusionEntity.getLine(),
subwayConfusionEntity.getDayType().getCode(),
subwayConfusionEntity.getUpdown().getCode(),
subwayConfusionEntity.getTimeSlot().toString(),
subwayConfusionEntity.getDayType(),
subwayConfusionEntity.getUpdown(),
subwayConfusionEntity.getTimeSlot(),
subwayConfusionEntity.getConfusion()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.talkka.server.subway.exception;

public class ConfusionNotFoundException extends RuntimeException {
private static final String MESSAGE = "조회 가능한 혼잡도 정보가 없습니다.";

public ConfusionNotFoundException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.talkka.server.subway.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.talkka.server.common.enums.TimeSlot;
import com.talkka.server.common.exception.http.NotFoundException;
import com.talkka.server.common.util.EnumCodeConverterUtils;
import com.talkka.server.common.exception.InvalidTypeException;
import com.talkka.server.subway.dao.SubwayConfusionEntity;
import com.talkka.server.subway.dao.SubwayConfusionRepository;
import com.talkka.server.subway.dao.SubwayStationRepository;
import com.talkka.server.subway.dto.SubwayConfusionRespDto;
import com.talkka.server.subway.enums.DayType;
import com.talkka.server.subway.enums.Updown;
import com.talkka.server.subway.exception.ConfusionNotFoundException;
import com.talkka.server.subway.exception.StationNotFoundException;

import lombok.RequiredArgsConstructor;

Expand All @@ -19,24 +22,46 @@
public class SubwayConfusionService {

private final SubwayConfusionRepository confusionRepository;

private final SubwayStationRepository stationRepository;

// TODO 추후 통계쪽으로 넘길지 논의 필요
public SubwayConfusionRespDto getSubwayConfusion(
Long stationId, String dayTypeCode, String updownCode, String timeSlot
) {
if (!stationRepository.existsById(stationId)) {
throw new NotFoundException("존재하지 않는 지하철 역입니다.");
}
public SubwayConfusionRespDto getConfusion(
Long stationId, String dayType, String updown, String timeSlot
) throws StationNotFoundException, ConfusionNotFoundException, InvalidTypeException {
isExisted(stationId);

SubwayConfusionEntity optionalConfusionEntity = confusionRepository.findBySubwayStationIdAndDayTypeAndUpdownAndTimeSlot(
SubwayConfusionEntity entity = confusionRepository.findBySubwayStationIdAndDayTypeAndUpdownAndTimeSlot(
stationId,
EnumCodeConverterUtils.fromCode(DayType.class, dayTypeCode),
EnumCodeConverterUtils.fromCode(Updown.class, updownCode),
DayType.valueOfEnumString(dayType),
Updown.valueOfEnumString(updown),
TimeSlot.valueOfEnumString(timeSlot)
).orElseThrow(() -> new NotFoundException("조회 가능한 혼잡도 정보가 없습니다."));
).orElseThrow(ConfusionNotFoundException::new);

return SubwayConfusionRespDto.of(entity);
}

public List<SubwayConfusionRespDto> getConfusionList(
Long stationId, String dayType, String updown, String startTimeSlot, String endTimeSlot
) throws StationNotFoundException, InvalidTypeException {
isExisted(stationId);

return SubwayConfusionRespDto.of(optionalConfusionEntity);
List<SubwayConfusionEntity> entityList = confusionRepository.findBySubwayStationIdAndDayTypeAndUpdownAndTimeSlotBetween(
stationId,
DayType.valueOfEnumString(dayType),
Updown.valueOfEnumString(updown),
TimeSlot.valueOfEnumString(startTimeSlot),
TimeSlot.valueOfEnumString(endTimeSlot)
);

return entityList.stream()
.map(SubwayConfusionRespDto::of)
.toList();
}

private void isExisted(Long stationId) {
if (!stationRepository.existsById(stationId)) {
throw new StationNotFoundException(stationId);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.talkka.server.subway.dto.SubwayStationRespDto;
import com.talkka.server.subway.exception.StationAlreadyExistsException;
import com.talkka.server.subway.exception.StationNotFoundException;
import com.talkka.server.subway.exception.enums.InvalidLineEnumException;

import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -40,7 +39,7 @@ public List<SubwayStationRespDto> getStationList() {
}

public SubwayStationRespDto createStation(SubwayStationDto stationDto)
throws StationAlreadyExistsException, InvalidLineEnumException {
throws StationAlreadyExistsException {
if (stationRepository.existsByStationCode(stationDto.stationCode())) {
throw new StationAlreadyExistsException(stationDto.stationCode());
}
Expand Down
Loading

0 comments on commit 990176f

Please sign in to comment.