Skip to content

Commit

Permalink
feature: 엔티티 및 request, response 변경
Browse files Browse the repository at this point in the history
- length 제약조건 추가
- request에 validate 추가
- response enum -> String 반환하도록 변경
- updownCode 오탈자 수정
  • Loading branch information
ss0ngcode committed Aug 12, 2024
1 parent 127e64c commit 7c03c4a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ public class SubwayConfusionEntity {
@JoinColumn(name = "station_id")
private SubwayStationEntity subwayStation;

@Column(name = "day_type", nullable = false)
@Convert(converter = DayTypeConverter.class)
private DayType dayType;
@Column(name = "station_code", nullable = false, length = 10)
private String stationCode;

@Column(name = "station_name", nullable = false, length = 50)
private String stationName;

@Column(name = "line_cd", nullable = false)
@Column(name = "line_code", nullable = false, length = 4)
@Convert(converter = LineConverter.class)
private Line line;

@Column(name = "fr_code", nullable = false)
private String frCode;

@Column(name = "station_name", nullable = false)
private String stationName;
@Column(name = "day_type", nullable = false, length = 3)
@Convert(converter = DayTypeConverter.class)
private DayType dayType;

@Column(name = "updown", nullable = false)
@Column(name = "updown", nullable = false, length = 1)
@Convert(converter = UpdownConverter.class)
private Updown updown;

@Column(name = "time_slot", nullable = false)
@Column(name = "time_slot", nullable = false, length = 2)
@Convert(converter = TimeSlotConverter.class)
private TimeSlot timeSlot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ public class SubwayStationEntity {
@Column(name = "station_id", nullable = false)
private Long id;

@Column(name = "api_station_id")
private String apiStationId;

@Column(name = "station_name", nullable = false)
@Column(name = "station_name", nullable = false, length = 50)
private String stationName;

@Column(name = "fr_code", nullable = false)
private String frCode;
@Column(name = "station_code", nullable = false, length = 10)
private String stationCode;

@Column(name = "line_code", nullable = false)
@Column(name = "line_code", nullable = false, length = 4)
@Convert(converter = LineConverter.class)
private Line line;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public interface SubwayStationRepository extends JpaRepository<SubwayStationEntity, Long> {
List<SubwayStationEntity> findByStationNameLikeOrderByStationNameAsc(String stationName);

boolean existsByApiStationId(String apiStationId);
boolean existsByStationCode(String stationCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,29 @@ public class SubwayTimetableEntity {
@JoinColumn(name = "station_id", nullable = false)
private SubwayStationEntity subwayStation;

@Column(name = "line_code", nullable = false)
@Convert(converter = LineConverter.class)
private Line line;

@Column(name = "fr_code", nullable = false)
private String frCode;
@Column(name = "station_code", nullable = false, length = 10)
private String stationCode;

@Column(name = "station_name", nullable = false)
@Column(name = "station_name", nullable = false, length = 50)
private String stationName;

@Column(name = "day_type", nullable = false)
@Column(name = "line_code", nullable = false, length = 4)
@Convert(converter = LineConverter.class)
private Line line;

@Column(name = "day_type", nullable = false, length = 3)
@Convert(converter = DayTypeConverter.class)
private DayType dayType;

@Column(name = "updown", nullable = false)
@Column(name = "updown", nullable = false, length = 1)
@Convert(converter = UpdownConverter.class)
private Updown updown;

@Column(name = "is_express", nullable = false)
@Column(name = "is_express", nullable = false, length = 1)
@Convert(converter = ExpressConverter.class)
private Express express;

@Column(name = "train_code", nullable = false)
@Column(name = "train_code", nullable = false, length = 10)
private String trainCode;

@Column(name = "arrival_time", nullable = false)
Expand All @@ -74,10 +74,10 @@ public class SubwayTimetableEntity {
@Column(name = "depart_time", nullable = false)
private LocalTime departTime;

@Column(name = "start_station_name", nullable = false)
@Column(name = "start_station_name", nullable = false, length = 50)
private String startStationName;

@Column(name = "end_station_name", nullable = false)
@Column(name = "end_station_name", nullable = false, length = 50)
private String endStationName;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
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,
Line line,
DayType dayType,
Updown updown,
TimeSlot timeSlot,
String dayType,
String updown,
String timeSlot,
Double confusion
) {
public static SubwayConfusionRespDto of(SubwayConfusionEntity subwayConfusionEntity) {
return new SubwayConfusionRespDto(
subwayConfusionEntity.getId(),
subwayConfusionEntity.getLine(),
subwayConfusionEntity.getDayType(),
subwayConfusionEntity.getUpdown(),
subwayConfusionEntity.getTimeSlot(),
subwayConfusionEntity.getDayType().getCode(),
subwayConfusionEntity.getUpdown().getCode(),
subwayConfusionEntity.getTimeSlot().getCode(),
subwayConfusionEntity.getConfusion()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
import com.talkka.server.subway.dao.SubwayStationEntity;
import com.talkka.server.subway.enums.Line;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;

@Builder
public record SubwayStationReqDto(
String apiStationId,
String stationName,
String frCode,
String lineId
@NotNull String stationName,
@NotNull String stationCode,
@NotNull String lineId
) {
public static SubwayStationEntity toEntity(SubwayStationReqDto subwayStationReqDto) {
return SubwayStationEntity.builder()
.apiStationId(subwayStationReqDto.apiStationId)
.stationName(subwayStationReqDto.stationName)
.frCode(subwayStationReqDto.frCode)
.stationCode(subwayStationReqDto.stationCode)
.line(EnumCodeConverterUtils.fromCode(Line.class, subwayStationReqDto.lineId))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.talkka.server.subway.dto;

import com.talkka.server.subway.dao.SubwayStationEntity;
import com.talkka.server.subway.enums.Line;

import lombok.Builder;

@Builder
public record SubwayStationRespDto(
Long stationId,
String stationName,
String frCode,
Line line
String stationCode,
String lineCode
) {
public static SubwayStationRespDto of(SubwayStationEntity subwayStationEntity) {
return new SubwayStationRespDto(
subwayStationEntity.getId(),
subwayStationEntity.getStationName(),
subwayStationEntity.getFrCode(),
subwayStationEntity.getLine()
subwayStationEntity.getStationCode(),
subwayStationEntity.getLine().getCode()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SubwayConfusionService {

// TODO 추후 통계쪽으로 넘길지 논의 필요
public SubwayConfusionRespDto getSubwayConfusion(
Long stationId, String dayTypeCode, String updowncode, String typeSlotCode
Long stationId, String dayTypeCode, String updownCode, String typeSlotCode
) {
if (!stationRepository.existsById(stationId)) {
throw new NotFoundException("존재하지 않는 지하철 역입니다.");
Expand All @@ -33,7 +33,7 @@ public SubwayConfusionRespDto getSubwayConfusion(
SubwayConfusionEntity optionalConfusionEntity = confusionRepository.findBySubwayStationIdAndDayTypeAndUpdownAndTimeSlot(
stationId,
EnumCodeConverterUtils.fromCode(DayType.class, dayTypeCode),
EnumCodeConverterUtils.fromCode(Updown.class, updowncode),
EnumCodeConverterUtils.fromCode(Updown.class, updownCode),
EnumCodeConverterUtils.fromCode(TimeSlot.class, typeSlotCode)
).orElseThrow(() -> new NotFoundException("조회 가능한 혼잡도 정보가 없습니다."));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public List<SubwayStationRespDto> findByStationName(String stationName) {
.toList();
}

public SubwayStationRespDto saveStation(SubwayStationReqDto reqDto) {
if (stationRepository.existsByApiStationId(reqDto.apiStationId())) {
public SubwayStationRespDto createStation(SubwayStationReqDto reqDto) {
if (stationRepository.existsByStationCode(reqDto.stationCode())) {
throw new BadRequestException("이미 존재하는 지하철 역입니다");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ private SubwayStationRespDto stationDtoFixture(Long stationId) {
return SubwayStationRespDto.builder()
.stationId(stationId)
.stationName("서울역")
.frCode("0150")
.line(Line.LINE_ONE)
.stationCode("0150")
.lineCode(Line.LINE_ONE.getCode())
.build();
}

private SubwayStationEntity stationEntityFixture(Long stationId) {
return SubwayStationEntity.builder()
.id(stationId)
.apiStationId("1001000133")
.stationName("서울역")
.frCode("0150")
.stationCode("0150")
.line(Line.LINE_ONE)
.build();
}
Expand All @@ -61,7 +60,7 @@ public class getStation {
SubwayStationEntity subwayStationEntity = SubwayStationEntity.builder()
.id(stationId)
.stationName("서울역")
.frCode("0150")
.stationCode("0150")
.line(Line.LINE_ONE)
.build();

Expand Down Expand Up @@ -127,9 +126,8 @@ public class findByStationName {
public class saveStation {

SubwayStationReqDto subwayStationReqDto = SubwayStationReqDto.builder()
.apiStationId("1001000133")
.stationName("서울역")
.frCode("0150")
.stationCode("0150")
.lineId(Line.LINE_ONE.getCode())
.build();

Expand All @@ -142,7 +140,7 @@ public class saveStation {
stationEntityFixture(stationId));

//when
SubwayStationRespDto result = stationService.saveStation(subwayStationReqDto);
SubwayStationRespDto result = stationService.createStation(subwayStationReqDto);

//then
assertThat(result).isEqualTo(stationDtoFixture(stationId));
Expand All @@ -153,11 +151,11 @@ public class saveStation {
//given
Class<?> exceptionClass = BadRequestException.class; // 추후 변경될 가능성이 있어, 변수로 따로 지정함

given(stationRepository.existsByApiStationId(subwayStationReqDto.apiStationId())).willReturn(true);
given(stationRepository.existsByStationCode(subwayStationReqDto.stationCode())).willReturn(true);

//when
//then
assertThatThrownBy(() -> stationService.saveStation(subwayStationReqDto))
assertThatThrownBy(() -> stationService.createStation(subwayStationReqDto))
.isInstanceOf(exceptionClass)
.hasMessage("이미 존재하는 지하철 역입니다");
}
Expand Down

0 comments on commit 7c03c4a

Please sign in to comment.