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

๐Ÿš€ 3๋‹จ๊ณ„ - ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ๊ด€๋ฆฌ #938

Open
wants to merge 4 commits into
base: jwoo-o
Choose a base branch
from
Open
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
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@
- ์ง€ํ•˜์ฒ ์—ญ ๋ชฉ๋ก ์กฐํšŒ ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ ์ž‘์„ฑํ•˜๊ธฐ
- ์ง€ํ•˜์ฒ ์—ญ ์‚ญ์ œ ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ ์ž‘์„ฑํ•˜๊ธฐ
## STEP2 ์ง€ํ•˜์ฒ  ๋…ธ์„  ๊ด€๋ฆฌ
### ๊ธฐ๋Šฅ ๋ชฉ๋ก
- ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ƒ์„ฑ
- ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ
- ์ง€ํ•˜์ฒ  ๋…ธ์„  ์กฐํšŒ
- ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ˆ˜์ •
- ์ง€ํ•˜์ฒ  ๋…ธ์„  ์‚ญ์ œ
### ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ
> ์•„๋ž˜์˜ ์ˆœ์„œ๋กœ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.
> ์ธ์ˆ˜ ์กฐ๊ฑด์„ ๊ฒ€์ฆํ•˜๋Š” ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ
> ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ถฉ์กฑํ•˜๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„
> ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์— ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ์•Š๋„๋ก ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์„œ๋กœ ๊ฒฉ๋ฆฌ ์‹œํ‚ค์„ธ์š”.
> ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์˜ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ๊ฐ€๋…์„ฑ, ๊ทธ๋ฆฌ๊ณ  ๋น ๋ฅธ ํ…Œ์ŠคํŠธ ์˜๋„ ํŒŒ์•…์„ ์œ„ํ•ด ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ๋ฆฌํŒฉํ„ฐ๋ง ํ•˜์„ธ์š”
> ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์˜ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ๊ฐ€๋…์„ฑ, ๊ทธ๋ฆฌ๊ณ  ๋น ๋ฅธ ํ…Œ์ŠคํŠธ ์˜๋„ ํŒŒ์•…์„ ์œ„ํ•ด ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ๋ฆฌํŒฉํ„ฐ๋ง ํ•˜์„ธ์š”
### ๊ธฐ๋Šฅ ๋ชฉ๋ก
- [x] ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ƒ์„ฑ
- [x] ์ง€ํ•˜์ฒ  ๋…ธ์„  ๋ชฉ๋ก ์กฐํšŒ
- [x] ์ง€ํ•˜์ฒ  ๋…ธ์„  ์กฐํšŒ
- [x] ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ˆ˜์ •
- [x] ์ง€ํ•˜์ฒ  ๋…ธ์„  ์‚ญ์ œ
## STEP3 ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ๊ด€๋ฆฌ
### ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ
> ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ ์ฃผ๋„ ๊ฐœ๋ฐœ ํ”„๋กœ์„ธ์Šค์— ๋งž์ถฐ์„œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.
- ์š”๊ตฌ์‚ฌํ•ญ ์„ค๋ช…์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ธ์ˆ˜ ์กฐ๊ฑด์„ ์ •์˜
- ์ธ์ˆ˜ ์กฐ๊ฑด์„ ๊ฒ€์ฆํ•˜๋Š” ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ
- ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ถฉ์กฑํ•˜๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„
> ์ธ์ˆ˜ ์กฐ๊ฑด์€ ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ ์ƒ๋‹จ์— ์ฃผ์„์œผ๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.
- ๋ผˆ๋Œ€ ์ฝ”๋“œ์˜ ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ฐธ๊ณ 
> ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์— ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ์•Š๋„๋ก ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์„œ๋กœ ๊ฒฉ๋ฆฌ ์‹œํ‚ค์„ธ์š”.

>์ธ์ˆ˜ ํ…Œ์ŠคํŠธ์˜ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ๊ฐ€๋…์„ฑ, ๊ทธ๋ฆฌ๊ณ  ๋น ๋ฅธ ํ…Œ์ŠคํŠธ ์˜๋„ ํŒŒ์•…์„ ์œ„ํ•ด ์ธ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ๋ฆฌํŒฉํ„ฐ๋ง ํ•˜์„ธ์š”.
### ๊ตฌ๊ฐ„ ๋“ฑ๋ก ๊ธฐ๋Šฅ
- [x] ์ง€ํ•˜์ฒ  ๋…ธ์„ ์— ๊ตฌ๊ฐ„์„ ๋“ฑ๋กํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„
- [x] ์ƒˆ๋กœ์šด ๊ตฌ๊ฐ„์˜ ์ƒํ–‰์—ญ์€ ํ•ด๋‹น ๋…ธ์„ ์— ๋“ฑ๋ก๋˜์–ด์žˆ๋Š” ํ•˜ํ–‰ ์ข…์ ์—ญ์ด์–ด์•ผ ํ•œ๋‹ค.
- [x] ์ƒˆ๋กœ์šด ๊ตฌ๊ฐ„์˜ ํ•˜ํ–‰์—ญ์€ ํ•ด๋‹น ๋…ธ์„ ์— ๋“ฑ๋ก๋˜์–ด์žˆ๋Š” ์—ญ์ผ ์ˆ˜ ์—†๋‹ค.
- [x] ์ƒˆ๋กœ์šด ๊ตฌ๊ฐ„ ๋“ฑ๋ก์‹œ ์œ„ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ ์ฒ˜๋ฆฌํ•œ๋‹ค
5 changes: 5 additions & 0 deletions src/main/java/subway/common/ErrorAdvice.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public class ErrorAdvice {
public ResponseEntity<String> notFoundEntity(NotFoundException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> illegalArgumentException(IllegalArgumentException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
7 changes: 7 additions & 0 deletions src/main/java/subway/common/exception/ErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package subway.common.exception;

public class ErrorMessage {

public static String SECTION_INTEGRITY_ADD_ERROR_MESSAGE = "์ง€ํ•˜์ฒ ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ ์กฐ๊ฑด์— ์œ„๋ฐฐ๋ฉ๋‹ˆ๋‹ค";
public static String SECTION_INTEGRITY_REMOVE_ERROR_MESSAGE = "์ง€ํ•˜์ฒ ๊ตฌ๊ฐ„ ์‚ญ์ œ ์กฐ๊ฑด์— ์œ„๋ฐฐ๋ฉ๋‹ˆ๋‹ค";
}
24 changes: 23 additions & 1 deletion src/main/java/subway/line/domain/Line.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package subway.line.domain;

import java.util.List;
import subway.line.dto.LineRequest;
import subway.line.dto.SectionRequest;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import subway.station.domain.Station;

@Entity
public class Line {
Expand All @@ -21,11 +25,17 @@ public class Line {
@Column(length = 20, nullable = false)
private String color;

public Line() {}
@Embedded
private Sections sections;

public Line() {
this.sections = new Sections();
}

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

public Long getId() {
Expand All @@ -44,4 +54,16 @@ public void update(LineRequest lineRequest) {
this.name = lineRequest.getName();
this.color = lineRequest.getColor();
}

public void addSection(Section section) {
this.sections.addSection(section);
}
Comment on lines +58 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addSection ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” line ๊ฐ์ฒด์™€
์ธ์ž๋กœ ๋ฐ›์€ Section ๋‚ด๋ถ€์˜ line ํ•„๋“œ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?
์ธ์ž๋กœ ๋ฐ›์€ Section์˜ line์„ ์žฌํ• ๋‹นํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์ง„์šฐ๋‹˜ ์ƒ๊ฐ์€ ์–ด๋– ์‹ ๊ฐ€์š”?
section.setLine(this);


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

public void deleteSection(Station station) {
sections.deleteSection(station);
}
}
63 changes: 63 additions & 0 deletions src/main/java/subway/line/domain/Section.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package subway.line.domain;

import subway.station.domain.Station;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Section {

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "lineId")
private Line line;

@ManyToOne
@JoinColumn(name = "upStationId")
private Station upStation;

@ManyToOne
@JoinColumn(name = "downStationId")
private Station downStation;

private int distance;

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

public Section() {
}

public Long getId() {
return id;
}

public Line getLine() {
return line;
}

public Station getDownStation() {
return downStation;
}

public Station getUpStation() {
return upStation;
}

public int getDistance() {
return distance;
}
}
80 changes: 80 additions & 0 deletions src/main/java/subway/line/domain/Sections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package subway.line.domain;

import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.CascadeType;
import javax.persistence.OneToMany;

import subway.common.exception.ErrorMessage;

import javax.persistence.Embeddable;
import java.util.ArrayList;
import java.util.List;

import subway.station.domain.Station;

@Embeddable
public class Sections {

@OneToMany(mappedBy = "line", orphanRemoval = true, cascade = CascadeType.ALL)
private List<Section> sections;
Comment on lines +16 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜ ํ™œ์šฉ ๐Ÿ‘


public Sections() {
this.sections = new ArrayList<>();
}

public void addSection(Section section) {
if (notValidSection(section) && alreadyValidSection(section)) {
throw new IllegalArgumentException(ErrorMessage.SECTION_INTEGRITY_ADD_ERROR_MESSAGE);
}
sections.add(section);
}

public List<Station> getStations() {
List<Station> stations = sections.stream()
.map(Section::getUpStation)
.collect(Collectors.toList());
setLastStations(stations);
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stream์˜ flatMap ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด ๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ˜ƒ


return stations;
}

public void deleteSection(Station station) {
if(isNotLastStation(station) || sections.size() < 2) {
throw new IllegalArgumentException(ErrorMessage.SECTION_INTEGRITY_REMOVE_ERROR_MESSAGE);
}
sections.remove(sections.size() - 1);
}

private boolean notValidSection(Section section) {
if (sections.size() > 0) {
Station endStation = sections.get(sections.size() - 1).getDownStation();
return !endStation.equals(section.getUpStation());
}
return false;
}

private boolean alreadyValidSection(Section section) {
return sections.stream()
.map(Section::getDownStation)
.anyMatch(station -> station.getId().equals(section.getDownStation().getId()));
}

private void setLastStations(List<Station> stations) {
getLastSection().ifPresent(section -> stations.add(section.getDownStation()));
}

private Optional<Section> getLastSection() {
if (sections.size() > 0) {
return Optional.of(sections.get(sections.size() - 1));
}
return Optional.empty();
}

private boolean isNotLastStation(Station station) {

return getLastSection()
.filter(section -> section.getDownStation().getId().equals(station.getId()))
.isEmpty();
}
}
39 changes: 28 additions & 11 deletions src/main/java/subway/line/dto/LineResponse.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package subway.line.dto;

import java.util.List;
import java.util.stream.Collectors;
import subway.line.domain.Line;
import subway.station.domain.Station;
import subway.station.dto.StationResponse;

public class LineResponse {

private final long id;
private final String name;
private final String color;
private long id;
private String name;
private String color;
private List<StationResponse> stations;

public LineResponse() {
}


public LineResponse(long id, String name, String color, List<StationResponse> stations) {
this.id = id;
this.name = name;
this.color = color;
this.stations = stations;
}

public static LineResponse from(Line line) {
return new LineResponse(line.getId(), line.getName(),
line.getColor(), line.getStations().stream()
.map(StationResponse::new)
.collect(Collectors.toList()));
}

public long getId() {
return id;
Expand All @@ -20,13 +43,7 @@ public String getColor() {
return color;
}

public LineResponse(long id, String name, String color) {
this.id = id;
this.name = name;
this.color = color;
}

public LineResponse(Line line) {
this(line.getId(), line.getName(), line.getColor());
public List<StationResponse> getStations() {
return stations;
}
}
22 changes: 22 additions & 0 deletions src/main/java/subway/line/dto/SectionRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package subway.line.dto;

import subway.line.domain.Section;

public class SectionRequest {

private long upStationId;
private long downStationId;
private int distance;

public long getUpStationId() {
return upStationId;
}

public long getDownStationId() {
return downStationId;
}

public int getDistance() {
return distance;
}
}
29 changes: 29 additions & 0 deletions src/main/java/subway/line/dto/SectionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package subway.line.dto;

import subway.station.dto.StationResponse;

public class SectionResponse {

private Long id;
private StationResponse upStation;
private StationResponse downStation;
private int distance;

public Long getId() {
return id;
}

public StationResponse getUpStation() {
return upStation;
}

public StationResponse getDownStation() {
return downStation;
}

public int getDistance() {
return distance;
}


}
Loading