-
Notifications
You must be signed in to change notification settings - Fork 305
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
Step3리뷰 요청드립니다. #501
Open
sang-eun
wants to merge
25
commits into
next-step:sang-eun
Choose a base branch
from
sang-eun:step3
base: sang-eun
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step3리뷰 요청드립니다. #501
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8ba6868
first commit
sang-eun 2c883bb
[add] api 테스트 추가
sang-eun 6e12f68
Merge branch 'sang-eun' into step1
sang-eun b4e2282
[add] 테스트 리스트
sang-eun 5aed214
[add] create api 및 테스트 추가
sang-eun 599824c
[modify] line 테스트 beforeAll 추가해서 자원공유하도록 수정
sang-eun 325e5ab
[modify] step1 코드리뷰 반영
sang-eun 2594684
[add] get api 테스트
sang-eun 489e81a
[add] get lines api
sang-eun 8beeb22
[add] get line api
sang-eun e094936
[add] update line api
sang-eun 15ae19d
[add] delete line api
sang-eun 3ecd092
[add] controllerAdvice 추가하려고 했으나 제대로 동작하지 않음. 캐치하고 204에러를 리턴해야 하는데, 계…
sang-eun 3007872
Merge branch 'step1' of github.com:sang-eun/atdd-subway-map into step2
sang-eun 47b5e09
[modify] 불필요한 부분 수정
sang-eun 112e8c0
Merge remote-tracking branch 'upstream/sang-eun' into step2
sang-eun 1d49dc3
코드리뷰 반영
sang-eun 6181976
[add] 섹션 기반 코드 작성
sang-eun b992dd1
[add] 섹션 추가 api
sang-eun bcc0eb3
[modify] Line과 section 관계
sang-eun b5bb1d9
[add] 구간 삭제 테스트 추가
sang-eun 4b11cea
[add] 구간 삭제 api 추가
sang-eun 44a6707
[modify] section 저장 로직 수정
sang-eun b74b646
코드리뷰 반영
sang-eun 33c993d
Merge branch 'sang-eun' of https://github.com/next-step/atdd-subway-m…
sang-eun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/nextstep/subway/applicaion/common/DuplicatedDownStationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package nextstep.subway.applicaion.common; | ||
|
||
import com.sun.jdi.request.DuplicateRequestException; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class DuplicatedDownStationException extends DuplicateRequestException { | ||
|
||
public DuplicatedDownStationException() { | ||
super("하행역이 이미 존재하는 역입니다."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/nextstep/subway/applicaion/common/MinimumSectionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package nextstep.subway.applicaion.common; | ||
|
||
public class MinimumSectionException extends IllegalArgumentException { | ||
public MinimumSectionException() { | ||
super("해당 라인의 구간 갯수가 최소 갯수이므로 삭제가 불가능합니다."); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/nextstep/subway/applicaion/common/SectionNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package nextstep.subway.applicaion.common; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class SectionNotFoundException extends NoSuchElementException { | ||
|
||
public SectionNotFoundException() { | ||
super("해당 구간이 존재하지 않습니다."); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/nextstep/subway/applicaion/common/UnappropriateStationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package nextstep.subway.applicaion.common; | ||
|
||
public class UnappropriateStationException extends IllegalArgumentException { | ||
|
||
public UnappropriateStationException() { | ||
super("역이 해당 라인의 하행종점역이 아닙니다."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 23 additions & 45 deletions
68
src/main/java/nextstep/subway/applicaion/line/domain/Line.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,30 @@ | ||
package nextstep.subway.applicaion.line.domain; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor | ||
public class Line { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(unique = true) | ||
private String name; | ||
private String color; | ||
private Long upStationId; | ||
private Long downStationId; | ||
private Integer distance; | ||
|
||
public Line() { | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public Long getUpStationId() { | ||
return upStationId; | ||
} | ||
|
||
public Long getDownStationId() { | ||
return downStationId; | ||
} | ||
|
||
public Line(String name, String color, Long upStationId, Long downStationId, Integer distance) { | ||
this.name = name; | ||
this.color = color; | ||
this.upStationId = upStationId; | ||
this.downStationId = downStationId; | ||
this.distance = distance; | ||
} | ||
|
||
public void updateLine(String name, String color) { | ||
this.name = name; | ||
this.color = color; | ||
} | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(unique = true) | ||
private String name; | ||
private String color; | ||
@Embedded | ||
private Sections sections = new Sections(); | ||
|
||
public Line(String name, String color) { | ||
this.name = name; | ||
this.color = color; | ||
} | ||
|
||
public void updateLineInfo(String name, String color) { | ||
this.name = name; | ||
this.color = color; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/nextstep/subway/applicaion/line/domain/Sections.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package nextstep.subway.applicaion.line.domain; | ||
|
||
import nextstep.subway.applicaion.common.DuplicatedDownStationException; | ||
import nextstep.subway.applicaion.common.MinimumSectionException; | ||
import nextstep.subway.applicaion.common.UnappropriateStationException; | ||
import nextstep.subway.applicaion.section.domain.Section; | ||
import nextstep.subway.applicaion.station.domain.Station; | ||
|
||
import javax.persistence.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
@Embeddable | ||
public class Sections { | ||
private final Integer MINIMUM_SECTION_SIZE = 1; | ||
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST}, orphanRemoval = true) | ||
@JoinColumn(name = "line_id") | ||
private List<Section> sections = new ArrayList<>(); | ||
|
||
public void add(Line line, Station upStation, Station downStation, Integer distance) { | ||
Section section = new Section(upStation, downStation, distance, line); | ||
sections.add(section); | ||
} | ||
|
||
public List<Station> stations() { | ||
List<Station> stations = sections.stream().map(Section::getUpStation).collect(Collectors.toList()); | ||
stations.add(getLastStation()); | ||
return stations; | ||
} | ||
|
||
public void removeStation(Long stationId) { | ||
if (sections.size() <= MINIMUM_SECTION_SIZE) { | ||
throw new MinimumSectionException(); | ||
} | ||
|
||
if (!Objects.equals(getLastStation().getId(), stationId)) { | ||
throw new UnappropriateStationException(); | ||
} | ||
sections.remove(getLastSection()); | ||
} | ||
|
||
public void checkIsLastStation(Station station) { | ||
if (!Objects.equals(getLastStation().getId(), station.getId())) { | ||
throw new UnappropriateStationException(); | ||
} | ||
} | ||
|
||
public void checkIsNewStation(Station station) { | ||
stations().forEach(it -> { | ||
if (Objects.equals(it.getId(), station.getId())) { | ||
throw new DuplicatedDownStationException(); | ||
} | ||
}); | ||
} | ||
|
||
private Section getLastSection() { | ||
return sections.get(sections.size() - 1); | ||
} | ||
|
||
private Station getLastStation() { | ||
return getLastSection().getDownStation(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/nextstep/subway/applicaion/section/SectionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nextstep.subway.applicaion.section; | ||
|
||
import nextstep.subway.applicaion.common.LineNotFoundException; | ||
import nextstep.subway.applicaion.line.domain.Line; | ||
import nextstep.subway.applicaion.line.domain.LineRepository; | ||
import nextstep.subway.applicaion.section.dto.SectionRequest; | ||
import nextstep.subway.applicaion.station.StationService; | ||
import nextstep.subway.applicaion.station.domain.Station; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
public class SectionService { | ||
private final StationService stationService; | ||
private final LineRepository lineRepository; | ||
|
||
public SectionService(StationService stationService, LineRepository lineRepository) { | ||
this.stationService = stationService; | ||
this.lineRepository = lineRepository; | ||
} | ||
|
||
public void saveSection(Long lineId, SectionRequest sectionRequest) { | ||
Line line = lineRepository.findById(lineId).orElseThrow(LineNotFoundException::new); | ||
|
||
Station upStation = stationService.getStationThrowExceptionIfNotExists(sectionRequest.getUpStationId()); | ||
Station downStation = stationService.getStationThrowExceptionIfNotExists(sectionRequest.getDownStationId()); | ||
|
||
line.getSections().checkIsLastStation(upStation); | ||
line.getSections().checkIsNewStation(downStation); | ||
|
||
line.getSections().add(line, upStation, downStation, sectionRequest.getDistance()); | ||
} | ||
|
||
public void deleteSection(Long lineId, Long stationId) { | ||
Line line = lineRepository.findById(lineId).orElseThrow(LineNotFoundException::new); | ||
|
||
line.getSections().removeStation(stationId); | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/nextstep/subway/applicaion/section/domain/Section.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package nextstep.subway.applicaion.section.domain; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import nextstep.subway.applicaion.line.domain.Line; | ||
import nextstep.subway.applicaion.station.domain.Station; | ||
|
||
import javax.persistence.*; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor | ||
public class Section { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@ManyToOne(cascade = CascadeType.PERSIST) | ||
@JoinColumn(name = "up_station_id") | ||
private Station upStation; | ||
@ManyToOne(cascade = CascadeType.PERSIST) | ||
@JoinColumn(name = "down_station_id") | ||
private Station downStation; | ||
private Integer distance; | ||
@ManyToOne(cascade = CascadeType.PERSIST) | ||
@JoinColumn(name = "line_id") | ||
private Line line; | ||
|
||
public Section(Station upStation, Station downStation, Integer distance, Line line) { | ||
this.upStation = upStation; | ||
this.downStation = downStation; | ||
this.distance = distance; | ||
this.line = line; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/nextstep/subway/applicaion/section/domain/SectionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package nextstep.subway.applicaion.section.domain; | ||
|
||
import nextstep.subway.applicaion.line.domain.Line; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface SectionRepository extends JpaRepository<Section, Long> { | ||
List<Section> findAllByLineId(Long lineId); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 Section 은 Line 에 속해 있는 도메인으로 보이는데요
SectionService 를 선언하지 않아도 해당 로직을 Sections 와 Line 에서 할 수 있을 것으로 보이는데 어떻게 생각하시나요? 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
클린아키텍쳐에서처럼 서비스가 비대해지는 것을 방지하기 위해 한 컨트롤러마다 GetSectionUseCase 와 같이 나누는 것까지는 아니더라도 최소한 중심이 되는 도메인(Section)별로 나누는 것이 좋다고 생각했습니다
응집시켜두는 것이 좋다고 생각하시는 이유가 있으실까요?