Skip to content

Commit

Permalink
[Step2] 인수 테스트 리팩터링 (#184)
Browse files Browse the repository at this point in the history
* docs: 기능 요구 사항 정리

* docs: 기능 요구 사항 정리

* style: 메소드 매개변수간 개행 문자 추가

* style: Class 끝에 개행문자 추가

* feat: 노선 생성 시 종점역(상행, 하행) 정보를 요청 파라미터에 함께 추가하기

* feat: 노선 조회 시 응답 결과에 역 목록 추가하기

* refactor: service > getStations 을 Line Entity 에 위임

* refactor: line 의 데이터 변경시, 역 목록이 보일 수 있도록 변경

* refactor: line의 역 정보가 상행에서 하행으로 보여지도록 수정

* docs: 피드백으로 받은 부분 리팩토링 Todo 정의

* refactor: Pull Request 피드백 코드 변경
  • Loading branch information
LenKIM committed Jun 6, 2021
1 parent 905e455 commit 594a363
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 42 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ This project is [MIT](https://github.com/next-step/atdd-subway-admin/blob/master
- [x] 지하철 노선을 수정한다.
- [x] 지하철 노선을 제거한다.

- [ ] [**2단계**] 지하철 노선에 구간 등록
- [x] [**2단계**] 지하철 노선에 구간 등록
- [x] 노선 생성 시 종점역(상행, 하행) 정보를 요청 파라미터에 함께 추가하기
- 두 종점역은 **구간**의 형태로 관리되어야 함
- [x] 노선 조회 시 응답 결과에 역 목록 추가하기
- **상행역 부터 하행역 순으로 정렬되어야 함**

- [ ] [**3단계**] 노선에 등록된 역 목록 조회
- [ ] [**4단계**] 지하철 노선에 구간 제외

Expand Down Expand Up @@ -101,3 +106,54 @@ This project is [MIT](https://github.com/next-step/atdd-subway-admin/blob/master
- 노선 이름(name)
- 노선 색(color)

## [2단계] 인수 테스트 리팩터링

## TODO

[**2단계**] 지하철 노선에 구간 등록

- [x] 노선 생성 시 종점역(상행, 하행) 정보를 요청 파라미터에 함께 추가하기
- 두 종점역은 **구간**의 형태로 관리되어야 함
- [x] 노선 조회 시 응답 결과에 역 목록 추가하기
- **상행역 부터 하행역 순으로 정렬되어야 함**
---
- [x] service > getStations 을 Line Entity에 위임하기
- [x] 노선 정보 업데이트시 역 목록 보이도록 변경
--- 피드백 리팩토링
- [x] stationRepository.findById(request.getDownStationId()) 중복 코드 제거
- [x] Inline Variable 제거
- [x] getStations() 중복 코드 제거

## 노선 생성 시 두 종점역 추가하기

- 인수 테스트와 DTO 등 수정이 필요함

```java
public class LineRequest {
private String name;
private String color;
private Long upStationId; // 추가
private Long downStationId; // 추가
private int distance; // 추가
...
}
```

## 노선 객체에서 구간 정보를 관리하기

- 노선 생성시 전달되는 두 종점역은 노선의 상태로 관리되는 것이 아니라 구간으로 관리되어야 함

```java
public class Line {
...
private List<Section> sections;
...
}
```

## 노선의 역 목록을 조회하는 기능 구현하기

- 노선 조회 시 역 목록을 함께 응답할 수 있도록 변경
- 노선에 등록된 구간을 순서대로 정렬하여 상행 종점부터 하행 종점까지 목록을 응답하기
- 필요시 노선과 구간(혹은 역)의 관계를 새로 맺기

37 changes: 28 additions & 9 deletions src/main/java/nextstep/subway/line/application/LineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import nextstep.subway.line.domain.Line;
import nextstep.subway.line.domain.LineRepository;
import nextstep.subway.line.domain.Section;
import nextstep.subway.line.dto.LineRequest;
import nextstep.subway.line.dto.LineResponse;
import nextstep.subway.station.domain.Station;
import nextstep.subway.station.domain.StationRepository;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,9 +18,11 @@
@Transactional
public class LineService {
private final LineRepository lineRepository;
private final StationRepository stationRepository;

public LineService(LineRepository lineRepository) {
public LineService(LineRepository lineRepository, StationRepository stationRepository) {
this.lineRepository = lineRepository;
this.stationRepository = stationRepository;
}

@Transactional(readOnly = true)
Expand All @@ -28,21 +33,35 @@ public List<LineResponse> findAllStations() {

@Transactional(readOnly = true)
public LineResponse findById(final Long lineId) {
Line id = lineRepository.findById(lineId)
Line line = lineRepository.findById(lineId)
.orElseThrow(() -> new DataIntegrityViolationException("Not Found lineId" + lineId));
return LineResponse.of(id);

return LineResponse.of(line);
}

public LineResponse saveLine(final LineRequest request) {
Line persistLine = lineRepository.save(request.toLine());
return LineResponse.of(persistLine);

Line line = request.toLine();
line.addSection(new Section(getStation(request.getUpStationId()), getStation(request.getDownStationId()),
request.getDistance()));

Line save = lineRepository.save(line);
return LineResponse.of(save);
}

public LineResponse updateById(final Long lineId,final LineRequest lineRequest) {
Line line = lineRepository.findById(lineId)
public LineResponse updateById(final Long lineId, final LineRequest lineRequest) {
Line originLine = lineRepository.findById(lineId)
.orElseThrow(() -> new DataIntegrityViolationException("Not Found lineId" + lineId));
line.update(lineRequest.toLine());
return LineResponse.of(line);

Section section = new Section(getStation(lineRequest.getUpStationId()), getStation(lineRequest.getDownStationId()),
lineRequest.getDistance());
originLine.update(lineRequest.getName(), lineRequest.getColor(), section);

return LineResponse.of(originLine);
}

private Station getStation(Long stationId) {
return stationRepository.findById(stationId).orElseThrow(() -> new DataIntegrityViolationException("Not Fount downStationId" + stationId));
}

public void deleteLineById(final Long id) {
Expand Down
43 changes: 39 additions & 4 deletions src/main/java/nextstep/subway/line/domain/Line.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
package nextstep.subway.line.domain;

import nextstep.subway.common.BaseEntity;
import nextstep.subway.station.domain.Station;
import org.springframework.beans.BeanUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Entity
public class Line extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique = true)
private String name;

private String color;

public Line() {
@Embedded
private Sections sections = Sections.of(new ArrayList<>());

public Line() { }

public Line(String name, String color, Sections sections) {
this.name = name;
this.color = color;
this.sections = sections;
}

public Line(String name, String color) {
this.name = name;
this.color = color;
}

public void update(Line line) {
this.name = line.getName();
this.color = line.getColor();
public void addSection(Section section){
this.sections.getValues().add(section);
section.toLine(this);
}

public Long getId() {
Expand All @@ -37,4 +53,23 @@ public String getName() {
public String getColor() {
return color;
}

public Sections getSections() {
return sections;
}

public List<Station> getStations() {
return this.sections.getValues().stream()
.map(section -> Stream.of(section.getUpStation(), section.getDownStation()))
.flatMap(Stream::distinct)
.collect(Collectors.toList());
}

public void update(String name, String color, Section section) {
this.name = name;
this.color = color;
List<Section> values = this.sections.getValues();
values.clear();
values.add(section);
}
}
56 changes: 56 additions & 0 deletions src/main/java/nextstep/subway/line/domain/Section.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package nextstep.subway.line.domain;

import nextstep.subway.common.BaseEntity;
import nextstep.subway.station.domain.Station;

import javax.persistence.*;

@Entity
public class Section extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Line line;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "up_station_id")
private Station upStation;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "down_station_id")
private Station downStation;

private int distance;

public Section() {
}

public Section(Station upStation, Station downStation, int distance) {
this.upStation = upStation;
this.downStation = downStation;
this.distance = distance;
}

public Line getLine() {
return line;
}

public Station getUpStation() {
return upStation;
}

public Station getDownStation() {
return downStation;
}

public int getDistance() {
return distance;
}

public void toLine(Line line) {
this.line = line;
}
}
27 changes: 27 additions & 0 deletions src/main/java/nextstep/subway/line/domain/Sections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nextstep.subway.line.domain;

import javax.persistence.*;
import java.util.List;

@Embeddable
public class Sections {

@OneToMany(mappedBy = "line", cascade = CascadeType.ALL)
@Column(name = "sections")
private List<Section> values;

private Sections(List<Section> values) {
this.values = values;
}

public Sections() {
}

public List<Section> getValues() {
return values;
}

public static Sections of(List<Section> sections){
return new Sections(sections);
}
}
27 changes: 21 additions & 6 deletions src/main/java/nextstep/subway/line/dto/LineRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import nextstep.subway.line.domain.Line;

public class LineRequest {
private String name;
private String color;
private final String name;
private final String color;
private final Long upStationId;
private final Long downStationId;
private final int distance;

public LineRequest() {
}

public LineRequest(String name, String color) {
public LineRequest(String name, String color, Long upStationId, Long downStationId, int distance) {
this.name = name;
this.color = color;
this.upStationId = upStationId;
this.downStationId = downStationId;
this.distance = distance;
}

public String getName() {
Expand All @@ -22,6 +25,18 @@ public String getColor() {
return color;
}

public Long getUpStationId() {
return upStationId;
}

public Long getDownStationId() {
return downStationId;
}

public int getDistance() {
return distance;
}

public Line toLine() {
return new Line(name, color);
}
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/nextstep/subway/line/dto/LineResponse.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package nextstep.subway.line.dto;

import nextstep.subway.line.domain.Line;
import nextstep.subway.station.domain.Station;

import java.time.LocalDateTime;
import java.util.List;

public class LineResponse {
private Long id;
private String name;
private String color;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;

public LineResponse() {
}

public LineResponse(Long id, String name, String color, LocalDateTime createdDate, LocalDateTime modifiedDate) {
private final Long id;
private final String name;
private final String color;
private final List<Station> stations;
private final LocalDateTime createdDate;
private final LocalDateTime modifiedDate;

public LineResponse(Long id, String name, String color,
List<Station> stations,
LocalDateTime createdDate,
LocalDateTime modifiedDate) {
this.id = id;
this.name = name;
this.color = color;
this.stations = stations;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
}

public static LineResponse of(Line line) {
return new LineResponse(line.getId(), line.getName(), line.getColor(), line.getCreatedDate(), line.getModifiedDate());
return new LineResponse(line.getId(), line.getName(), line.getColor(), line.getStations(),
line.getCreatedDate(), line.getModifiedDate());
}

public Long getId() {
Expand All @@ -38,6 +43,10 @@ public String getColor() {
return color;
}

public List<Station> getStations() {
return stations;
}

public LocalDateTime getCreatedDate() {
return createdDate;
}
Expand Down

0 comments on commit 594a363

Please sign in to comment.