Skip to content

Commit

Permalink
refactor: 비즈니스 로직 수정
Browse files Browse the repository at this point in the history
- 최단 거리 구하는 로직 수정
- Line, Station 생성, 수정 시간 auditing 사용
- line update 로직에 색상 업데이트 추가
- exception 수정, 공백 제거
- Auditing configuration 추가
- transactional 추가
- test 로직 추가
  • Loading branch information
fucct committed May 15, 2020
1 parent 209a237 commit 70f5771
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/main/java/wooteco/subway/admin/dto/ErrorResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

public class ErrorResponse {

private final String errorMessage;
private String errorMessage;

public ErrorResponse() {
}

public ErrorResponse(String errorMessage) {
this.errorMessage = errorMessage;
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/wooteco/subway/admin/dto/PathType.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package wooteco.subway.admin.dto;

import java.util.Objects;
import java.util.function.Function;

import wooteco.subway.admin.domain.LineStation;
import wooteco.subway.admin.exception.IllegalTypeNameException;

public enum PathType {
DISTANCE(LineStation::getDistance),
DURATION(LineStation::getDuration);

private final Function<LineStation, Integer> function;

PathType(
Function<LineStation, Integer> function) {
PathType(Function<LineStation, Integer> function) {
this.function = function;
}

public int getWeight(LineStation lineStation){
public int getWeight(LineStation lineStation) {
return function.apply(lineStation);
}

public static PathType of(String typeName){
return valueOf(typeName.toUpperCase());
public static PathType of(String typeName) {
String upperCaseName = typeName.toUpperCase();
if (!Objects.equals(upperCaseName, DURATION.name()) && !Objects.equals(upperCaseName,
DISTANCE.name())) {
throw new IllegalTypeNameException(typeName);
}
return valueOf(upperCaseName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package wooteco.subway.admin.exception;

public class IllegalTypeNameException extends BusinessException {

public IllegalTypeNameException(String typeName) {
super(typeName +"방식의 경로는 지원하지 않습니다.");
}
}
7 changes: 7 additions & 0 deletions src/main/java/wooteco/subway/admin/service/LineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wooteco.subway.admin.domain.Line;
import wooteco.subway.admin.domain.LineStation;
Expand All @@ -17,6 +18,7 @@
import wooteco.subway.admin.repository.LineRepository;

@Service
@Transactional
public class LineService {
private final LineRepository lineRepository;
private final StationService stationService;
Expand All @@ -31,22 +33,26 @@ public LineResponse save(Line line) {
return LineResponse.of(lineRepository.save(line));
}

@Transactional(readOnly = true)
public List<LineResponse> findLines() {
return LineResponse.listOf(lineRepository.findAll());
}

@Transactional(readOnly = true)
public LineResponse findLine(Long id) {
Line line = lineRepository.findById(id).orElseThrow(() -> new NotFoundLineException(id));

return LineResponse.of(line);
}

@Transactional(readOnly = true)
public LineDetailResponse findDetailLine(Long id) {
Line line = lineRepository.findById(id).orElseThrow(() -> new NotFoundLineException(id));
List<Station> stations = stationService.findAllById(line.getLineStationsId());
return LineDetailResponse.of(line, stations);
}

@Transactional(readOnly = true)
public WholeSubwayResponse findDetailLines() {
List<Line> lines = lineRepository.findAll();
List<LineDetailResponse> lineDetailResponses = lines.stream()
Expand Down Expand Up @@ -83,6 +89,7 @@ public void removeLineStation(Long lineId, Long stationId) {
lineRepository.save(line);
}

@Transactional(readOnly = true)
public List<Line> findAll() {
return lineRepository.findAll();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/wooteco/subway/admin/service/PathService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wooteco.subway.admin.domain.Line;
import wooteco.subway.admin.dto.GraphResponse;
Expand All @@ -15,6 +16,7 @@
import wooteco.subway.admin.exception.IllegalStationNameException;

@Service
@Transactional(readOnly = true)
public class PathService {

private final StationService stationService;
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/wooteco/subway/admin/service/StationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wooteco.subway.admin.domain.Station;
import wooteco.subway.admin.dto.PathRequest;
Expand All @@ -16,6 +17,7 @@
import wooteco.subway.admin.repository.StationRepository;

@Service
@Transactional(readOnly = true)
public class StationService {

private final StationRepository stationRepository;
Expand All @@ -24,6 +26,7 @@ public StationService(StationRepository stationRepository) {
this.stationRepository = stationRepository;
}

@Transactional
public StationResponse save(StationCreateRequest request) {
Station persistStation = stationRepository.save(request.toStation());

Expand All @@ -36,17 +39,18 @@ public List<StationResponse> findAll() {
return StationResponse.listOf(stations);
}

public void deleteById(Long id) {
stationRepository.deleteById(id);
}

public List<Station> findAllById(List<Long> lineStationsId) {
return stationRepository.findAllById(lineStationsId);
}

public Long findIdByName(String name) {
return stationRepository.findByName(name)
.orElseThrow(() -> new NotFoundStationException(name)).getId();
}

public List<Station> findAllById(List<Long> lineStationsId) {
return stationRepository.findAllById(lineStationsId);
@Transactional
public void deleteById(Long id) {
stationRepository.deleteById(id);
}
}
19 changes: 19 additions & 0 deletions src/test/java/wooteco/subway/admin/acceptance/AcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;
import wooteco.subway.admin.dto.ErrorResponse;
import wooteco.subway.admin.dto.LineDetailResponse;
import wooteco.subway.admin.dto.LineResponse;
import wooteco.subway.admin.dto.PathResponse;
Expand Down Expand Up @@ -213,7 +214,25 @@ PathResponse findPath(String source, String target, String type) {
log().all().
statusCode(HttpStatus.OK.value()).
extract().as(PathResponse.class);
}

ErrorResponse findPathByWrongType(String source, String target, String type) {
Map<String, String> params = new HashMap<>();
params.put("sourceName", source);
params.put("targetName", target);
params.put("type", type);

return
given().
body(params).
contentType(MediaType.APPLICATION_JSON_VALUE).
accept(MediaType.APPLICATION_JSON_VALUE).
when().
post("/path").
then().
log().all().
statusCode(HttpStatus.BAD_REQUEST.value()).
extract().as(ErrorResponse.class);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import wooteco.subway.admin.dto.ErrorResponse;
import wooteco.subway.admin.dto.LineResponse;
import wooteco.subway.admin.dto.PathResponse;
import wooteco.subway.admin.dto.StationResponse;
import wooteco.subway.admin.exception.IllegalTypeNameException;

public class PathAcceptanceTest extends AcceptanceTest {

Expand Down Expand Up @@ -39,14 +41,28 @@ void findPath() {
addLineStation(line8.getId(), jamsil.getId(), sukchon.getId(), 1, 10);

//when
PathResponse path = findPath(jamsil.getName(), samjun.getName(), "distance");
PathResponse pathByDistance = findPath(jamsil.getName(), samjun.getName(), "distance");

//then
assertThat(path.getStations()).hasSize(4);
assertThat(path.getStations()).extracting(StationResponse::getName)
assertThat(pathByDistance.getStations()).hasSize(4);
assertThat(pathByDistance.getStations()).extracting(StationResponse::getName)
.containsExactly("잠실", "석촌", "석촌고분", "삼전");
assertThat(path.getTotalDistance()).isEqualTo(3);
assertThat(path.getTotalDuration()).isEqualTo(30);
assertThat(pathByDistance.getTotalDistance()).isEqualTo(3);
assertThat(pathByDistance.getTotalDuration()).isEqualTo(30);

//when
PathResponse pathByDuration = findPath(jamsil.getName(), samjun.getName(), "duration");

//then
assertThat(pathByDuration.getStations()).hasSize(4);
assertThat(pathByDuration.getStations()).extracting(StationResponse::getName)
.containsExactly("잠실", "잠실새내", "종합운동장", "삼전");
assertThat(pathByDuration.getTotalDistance()).isEqualTo(30);
assertThat(pathByDuration.getTotalDuration()).isEqualTo(3);

//when
assertThat(findPathByWrongType(jamsil.getName(), samjun.getName(), "transfer"))
.isInstanceOf(ErrorResponse.class).extracting(ErrorResponse::getErrorMessage)
.isEqualTo("transfer방식의 경로는 지원하지 않습니다.");
}
}
16 changes: 8 additions & 8 deletions src/test/java/wooteco/subway/admin/api/LineControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ void ETag() throws Exception {
String uri = "/lines/detail";

MvcResult mvcResult = mockMvc.perform(get(uri))
.andDo(print())
.andExpect(status().isOk())
.andExpect(header().exists("ETag"))
.andReturn();
.andDo(print())
.andExpect(status().isOk())
.andExpect(header().exists("ETag"))
.andReturn();

String eTag = mvcResult.getResponse().getHeader("ETag");

mockMvc.perform(get(uri).header("If-None-Match", eTag))
.andDo(print())
.andExpect(status().isNotModified())
.andExpect(header().exists("ETag"))
.andReturn();
.andDo(print())
.andExpect(status().isNotModified())
.andExpect(header().exists("ETag"))
.andReturn();
}

private LineDetailResponse createMockResponse() {
Expand Down

0 comments on commit 70f5771

Please sign in to comment.