-
Notifications
You must be signed in to change notification settings - Fork 280
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
๐ 1๋จ๊ณ - ๊ตฌ๊ฐ ์ถ๊ฐ ๊ธฐ๋ฅ ๋ณ๊ฒฝ #225
base: sanhee
Are you sure you want to change the base?
Changes from 30 commits
6b26f4a
7bb6b7c
41e128f
68c3026
1c8d780
dc13b4b
a88d7d9
136bacf
5ea7fe7
64f3bb2
6006960
16c15d6
73ef079
d4c6841
a0dcf15
497bdf2
b8ec505
3198710
cf3985c
58da291
e296504
ca081f9
80ade28
41a1510
3d7384a
1dd43a5
c9c1f1c
17f5336
dad7461
34c2341
0a252c0
cd6e402
13e4d71
7ada152
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,13 @@ | |
import nextstep.subway.applicaion.dto.LineRequest; | ||
import nextstep.subway.applicaion.dto.LineResponse; | ||
import nextstep.subway.applicaion.dto.SectionRequest; | ||
import nextstep.subway.applicaion.dto.StationResponse; | ||
import nextstep.subway.domain.Line; | ||
import nextstep.subway.domain.LineRepository; | ||
import nextstep.subway.domain.Section; | ||
import nextstep.subway.domain.Station; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Transactional | ||
|
@@ -27,29 +23,33 @@ public LineService(LineRepository lineRepository, StationService stationService) | |
} | ||
|
||
public LineResponse saveLine(LineRequest request) { | ||
Line line = lineRepository.save(new Line(request.getName(), request.getColor())); | ||
Line line = lineRepository.save(new Line( | ||
request.getName(), | ||
request.getColor() | ||
)); | ||
if (request.getUpStationId() != null && request.getDownStationId() != null && request.getDistance() != 0) { | ||
Station upStation = stationService.findById(request.getUpStationId()); | ||
Station downStation = stationService.findById(request.getDownStationId()); | ||
line.getSections().add(new Section(line, upStation, downStation, request.getDistance())); | ||
line.addSection(upStation, downStation, request.getDistance()); | ||
} | ||
return createLineResponse(line); | ||
return LineResponse.from(line); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<LineResponse> showLines() { | ||
return lineRepository.findAll().stream() | ||
.map(this::createLineResponse) | ||
.collect(Collectors.toList()); | ||
return LineResponse.from(lineRepository.findAll()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public LineResponse findById(Long id) { | ||
return createLineResponse(lineRepository.findById(id).orElseThrow(IllegalArgumentException::new)); | ||
Line findLine = lineRepository.findById(id) | ||
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. findById(id).orElseThrow ์ด ์ฝ๋๊ฐ ์ค๋ณตํด์ ๋ฐ์ํ๊ณ ์์ผ๋ ๋ฉ์๋๋ก ์ถ์ถํด์ฃผ๋ฉด ์ด๋จ๊น์ |
||
.orElseThrow(IllegalArgumentException::new); | ||
return LineResponse.from(findLine); | ||
} | ||
|
||
public void updateLine(Long id, LineRequest lineRequest) { | ||
Line line = lineRepository.findById(id).orElseThrow(IllegalArgumentException::new); | ||
Line line = lineRepository.findById(id) | ||
.orElseThrow(IllegalArgumentException::new); | ||
|
||
if (lineRequest.getName() != null) { | ||
line.setName(lineRequest.getName()); | ||
|
@@ -66,46 +66,32 @@ public void deleteLine(Long id) { | |
public void addSection(Long lineId, SectionRequest sectionRequest) { | ||
Station upStation = stationService.findById(sectionRequest.getUpStationId()); | ||
Station downStation = stationService.findById(sectionRequest.getDownStationId()); | ||
Line line = lineRepository.findById(lineId).orElseThrow(IllegalArgumentException::new); | ||
Line findLine = lineRepository.findById(lineId) | ||
.orElseThrow(IllegalArgumentException::new); | ||
|
||
line.getSections().add(new Section(line, upStation, downStation, sectionRequest.getDistance())); | ||
} | ||
|
||
private LineResponse createLineResponse(Line line) { | ||
return new LineResponse( | ||
line.getId(), | ||
line.getName(), | ||
line.getColor(), | ||
createStationResponses(line), | ||
line.getCreatedDate(), | ||
line.getModifiedDate() | ||
findLine.addSection( | ||
upStation, | ||
downStation, | ||
sectionRequest.getDistance() | ||
); | ||
} | ||
|
||
private List<StationResponse> createStationResponses(Line line) { | ||
if (line.getSections().isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
List<Station> stations = line.getSections().stream() | ||
.map(Section::getDownStation) | ||
.collect(Collectors.toList()); | ||
|
||
stations.add(0, line.getSections().get(0).getUpStation()); | ||
|
||
return stations.stream() | ||
.map(it -> stationService.createStationResponse(it)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public void deleteSection(Long lineId, Long stationId) { | ||
Line line = lineRepository.findById(lineId).orElseThrow(IllegalArgumentException::new); | ||
Line line = lineRepository.findById(lineId) | ||
.orElseThrow(IllegalArgumentException::new); | ||
Station station = stationService.findById(stationId); | ||
|
||
if (!line.getSections().get(line.getSections().size() - 1).getDownStation().equals(station)) { | ||
if (!line.getSections() | ||
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 ๊ฐ์ฒด ๋ด๋ถ๋ก ์กฐ๊ฑด์ ์ฎ๊ฒจ๋ณด์๊ธฐ ๋ฐ๋๋๋ค. |
||
.get(line.getSections() | ||
.size() - 1) | ||
.getDownStation() | ||
.equals(station)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
line.getSections().remove(line.getSections().size() - 1); | ||
line.getSections() | ||
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. ๊ฐ์ฒด์งํฅ ์ํ์ฒด์กฐ |
||
.remove(line.getSections() | ||
.size() - 1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
package nextstep.subway.applicaion.dto; | ||||||
|
||||||
import org.springframework.data.annotation.CreatedDate; | ||||||
import org.springframework.http.HttpStatus; | ||||||
|
||||||
import java.time.LocalDateTime; | ||||||
|
||||||
public class ErrorResponse{ | ||||||
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.
Suggested change
|
||||||
|
||||||
private HttpStatus status; | ||||||
|
||||||
private String reason; | ||||||
|
||||||
@CreatedDate | ||||||
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. ์ด ์ ๋ํ
์ด์
์ spring data์์ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ด๋ค์. |
||||||
private LocalDateTime date; | ||||||
|
||||||
public ErrorResponse(HttpStatus status, String reason, LocalDateTime date) { | ||||||
this.status = status; | ||||||
this.reason = reason; | ||||||
this.date = date; | ||||||
} | ||||||
|
||||||
public HttpStatus getStatus() { | ||||||
return status; | ||||||
} | ||||||
|
||||||
public String getReason() { | ||||||
return reason; | ||||||
} | ||||||
|
||||||
public LocalDateTime getDate() { | ||||||
return date; | ||||||
} | ||||||
} |
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.
if์ ์กฐ๊ฑด์ด ๋ณต์กํ๋ ๋ฉ์๋๋ก ์ถ์ถํด๋ณด๋๊ฒ ์ข๊ฒ ๋ค์