-
Notifications
You must be signed in to change notification settings - Fork 277
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단계 - 구간 추가 기능 과제 PR 요청 드립니다! #1013
base: taewonh
Are you sure you want to change the base?
Changes from all commits
632e55f
4da3970
9d6e81e
0147455
b9f5542
ba80c92
b3fb307
2cf6f1a
3073277
ddbdc7b
2c68a76
d5faddf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package nextstep.subway.line; | ||
|
||
import nextstep.subway.section.CreateSectionRequest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
@@ -17,8 +18,8 @@ public LineController(LineService lineService) { | |
} | ||
|
||
@PostMapping("/lines") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 클래스에서 |
||
public ResponseEntity<LineResponse> createLine(@RequestBody CreateLineDto dto) { | ||
LineResponse response = lineService.register(dto); | ||
public ResponseEntity<LineResponse> createLine(@RequestBody CreateLineRequest request) { | ||
LineResponse response = lineService.register(request); | ||
return ResponseEntity.created(URI.create("/lines/" + response.getId())).body(response); | ||
} | ||
|
||
|
@@ -33,8 +34,8 @@ public ResponseEntity<LineResponse> showLine(@PathVariable("id") long id) { | |
} | ||
|
||
@PutMapping("/lines/{id}") | ||
public ResponseEntity<Void> updateLine(@PathVariable("id") long id, @RequestBody UpdateLineDto dto) { | ||
lineService.update(id, dto); | ||
public ResponseEntity<Void> updateLine(@PathVariable("id") long id, @RequestBody UpdateLineRequest request) { | ||
lineService.update(id, request); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
|
@@ -43,4 +44,10 @@ public ResponseEntity<Void> deleteLine(@PathVariable("id") long id) { | |
lineService.delete(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@PostMapping("/lines/{lineId}/sections") | ||
public ResponseEntity<LineResponse> createSection(@RequestBody CreateSectionRequest request, @PathVariable("lineId") long lineId) { | ||
LineResponse response = lineService.registerSection(lineId, request); | ||
return ResponseEntity.created(URI.create("/lines/" + response.getId())).body(response); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import nextstep.subway.section.Distance; | ||
import nextstep.subway.section.Section; | ||
import nextstep.subway.section.CreateSectionRequest; | ||
import nextstep.subway.station.Station; | ||
import nextstep.subway.station.StationRepository; | ||
import org.springframework.stereotype.Service; | ||
|
@@ -23,20 +24,32 @@ public LineService(LineRepository lineRepository, StationRepository stationRepos | |
} | ||
|
||
@Transactional | ||
public LineResponse register(CreateLineDto dto) { | ||
Station upStation = findStationById(dto.getUpStationId()); | ||
Station downStation = findStationById(dto.getDownStationId()); | ||
Distance distance = new Distance(dto.getDistance()); | ||
public LineResponse registerSection(long id, CreateSectionRequest request) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메소드도 단위 테스트를 추가하는건 어떤가요? |
||
Line line = lineRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 노선입니다.")); | ||
Station upStation = findStationById(request.getUpStationId()); | ||
Station downStation = findStationById(request.getDownStationId()); | ||
Distance distance = new Distance(request.getDistance()); | ||
Section section = new Section(upStation, downStation, distance); | ||
line.addSection(section); | ||
return LineResponse.of(line); | ||
} | ||
|
||
@Transactional | ||
public LineResponse register(CreateLineRequest request) { | ||
Line line = request.toLine(); | ||
Station upStation = findStationById(request.getUpStationId()); | ||
Station downStation = findStationById(request.getDownStationId()); | ||
Distance distance = new Distance(request.getDistance()); | ||
Section section = new Section(upStation, downStation, distance); | ||
Line line = dto.toLine(); | ||
line.addSection(section); | ||
return LineResponse.of(lineRepository.save(line)); | ||
} | ||
|
||
@Transactional | ||
public void update(long id, UpdateLineDto dto) { | ||
public void update(long id, UpdateLineRequest request) { | ||
Line line = findLineById(id); | ||
line.update(dto.toLine()); | ||
line.update(request.toLine()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package nextstep.subway.section; | ||
|
||
public class CreateSectionRequest { | ||
|
||
private long upStationId; | ||
private long downStationId; | ||
private long distance; | ||
|
||
public long getUpStationId() { | ||
return upStationId; | ||
} | ||
|
||
public long getDownStationId() { | ||
return downStationId; | ||
} | ||
|
||
public long getDistance() { | ||
return distance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ public class Section { | |
@Embedded | ||
private Distance distance; | ||
|
||
private int sequence = 1; | ||
|
||
protected Section() { } | ||
|
||
public Section(Station upStation, Station downStation, Distance distance) { | ||
|
@@ -46,4 +48,72 @@ public Station getUpStation() { | |
public Station getDownStation() { | ||
return downStation; | ||
} | ||
|
||
public Distance getDistance() { | ||
return distance; | ||
} | ||
|
||
public void increaseSequence() { | ||
this.sequence += 1; | ||
} | ||
|
||
public void increaseSequence(int add) { | ||
this.sequence = add + 1; | ||
} | ||
|
||
public int getSequence() { | ||
return sequence; | ||
} | ||
|
||
public boolean isExtendDownStation(Section section) { | ||
return this.downStation.equals(section.getUpStation()); | ||
} | ||
|
||
public boolean isExtendUpStation(Section section) { | ||
return this.upStation.equals(section.getDownStation()); | ||
} | ||
|
||
public boolean isEqualUpStation(Section section) { | ||
return this.upStation.equals(section.getUpStation()); | ||
} | ||
|
||
public boolean isEqualDownStation(Section section) { | ||
return this.downStation.equals(section.getDownStation()); | ||
} | ||
|
||
public void replace(Section newSection, Distance totalSectionDistance) { | ||
Distance excludeSectionDistance = totalSectionDistance.subtract(distance); | ||
newSection.distance = newSection.distance.subtract(excludeSectionDistance); | ||
newSection.upStation = upStation; | ||
newSection.sequence = sequence; | ||
upStation = newSection.downStation; | ||
distance = distance.subtract(newSection.distance); | ||
} | ||
Comment on lines
+84
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용되지 않는 메소드는 삭제해주세요. |
||
|
||
public void syncUpStation(Section other, Distance distance) { | ||
this.upStation = other.upStation; | ||
this.distance = new Distance(other.distance).add(distance); | ||
this.sequence = other.sequence; | ||
other.upStation = this.downStation; | ||
other.distance = other.distance.subtract(this.distance); | ||
} | ||
|
||
public void syncDownStation(Section other, Distance distance) { | ||
this.downStation = other.downStation; | ||
this.distance = new Distance(other.distance).add(distance); | ||
this.sequence = other.sequence + 1; | ||
other.downStation = this.upStation; | ||
other.distance = other.distance.subtract(this.distance); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Section{" + | ||
"id=" + id + | ||
", sequence=" + sequence + | ||
", upStation=" + upStation.getName() + | ||
", downStation=" + downStation.getName() + | ||
", distance=" + distance.get() + | ||
'}'; | ||
} | ||
} |
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.
빈 생성자가 없어 일부 테스트에서 오류가 발생합니다.
아마 실제로 실행을 하더라도 이 클래스를 사용하는 곳에선 전부 오류가 날거에요.
@RequestBody
가 어떤 방식으로 생성되는지를 공부해보시면 도움이 됩니다.