๐ 1๋จ๊ณ - ๊ตฌ๊ฐ ์ถ๊ฐ ๊ธฐ๋ฅ ๋ณ๊ฒฝ#225
Conversation
์ํ ์ข ์ ์ด ์ํ์ญ์ธ ๊ตฌ๊ฐ์ ๋จผ์ ์ฐพ๋๋ค. ๊ทธ ๋ค์, ํด๋น ๊ตฌ๊ฐ์ ํํ์ญ์ด ์ํ์ญ์ธ ๋ค๋ฅธ ๊ตฌ๊ฐ์ ์ฐพ๋๋ค. 2๋ฒ์ ๋ฐ๋ณตํ๋ค๊ฐ ํํ ์ข ์ ์ญ์ ์ฐพ์ผ๋ฉด ์กฐํ๋ฅผ ๋ฉ์ถ๋ค.
์ค๋์ ๊ตํ - ํจ์ ์ค๋ฒ๋ผ์ด๋๋ฅผ ์กฐ์ฌํ์.. List<Section> addSection(Section newSection) List<Section> addSection(Station upStation, Station downStation, int distance)
kang-hyungu
left a comment
There was a problem hiding this comment.
์๋
ํ์ธ์ ๋
ธ์๋, ๋ง๋์ ๋ฐ๊ฐ์ต๋๋ค. ๋ฆฌ๋ทฐ์ด ๊ฐํ๊ตฌ์
๋๋ค.
๋จผ์ ๋ช ๊ฐ์ง ํผ๋๋ฐฑ ๋จ๊ฒผ์ต๋๋ค. ์ฝ๋ ๋ฆฌํฉํฐ๋ง ํด๋ณด์๊ณ ๋ฆฌ๋ทฐ ์ฌ์์ฒญ์ฃผ์ธ์.
| request.getName(), | ||
| request.getColor() | ||
| )); | ||
| if (request.getUpStationId() != null && request.getDownStationId() != null && request.getDistance() != 0) { |
There was a problem hiding this comment.
if์ ์กฐ๊ฑด์ด ๋ณต์กํ๋ ๋ฉ์๋๋ก ์ถ์ถํด๋ณด๋๊ฒ ์ข๊ฒ ๋ค์
| @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.
findById(id).orElseThrow ์ด ์ฝ๋๊ฐ ์ค๋ณตํด์ ๋ฐ์ํ๊ณ ์์ผ๋ ๋ฉ์๋๋ก ์ถ์ถํด์ฃผ๋ฉด ์ด๋จ๊น์
| 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.
๊ฐ์ฒด์งํฅ ์ํ์ฒด์กฐ
4. ํ ์ค์ ์ ์ ํ๋๋ง ์ฐ๋๋ค.
line ๊ฐ์ฒด ๋ด๋ถ๋ก ์กฐ๊ฑด์ ์ฎ๊ฒจ๋ณด์๊ธฐ ๋ฐ๋๋๋ค.
| } | ||
|
|
||
| line.getSections().remove(line.getSections().size() - 1); | ||
| line.getSections() |
There was a problem hiding this comment.
๊ฐ์ฒด์งํฅ ์ํ์ฒด์กฐ
4. ํ ์ค์ ์ ์ ํ๋๋ง ์ฐ๋๋ค.
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public class ErrorResponse{ |
There was a problem hiding this comment.
| public class ErrorResponse{ | |
| public class ErrorResponse { |
|
|
||
| private List<Section> addSection(Section newSection) { | ||
|
|
||
| AtomicBoolean normalCondition = new AtomicBoolean(true); |
There was a problem hiding this comment.
- Atomic ํด๋์ค๋ฅผ ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์๊น์? ๋์์ฑ ๋ฌธ์ ๊ฐ ์๋ ๊ฑธ๋ก ๋ณด์ด๋ค์
- ๊ฐ ๋ฉ์๋๋ค์ด ์ด ๋ณ์์ ์์กดํ๋ ๋ก์ง์ ๋ง๋์ ๊ฒ ๊ฐ๋ค์. ์ธ๋ถ ์กฐ๊ฑด์ ์์กดํ์ง ์๋ ๋ ๋ฆฝ์ ์ธ ๋ฉ์๋๋ก ๋ง๋ค ๋ฐฉ๋ฒ์ ์์๊น์?
| .filter(station -> !getStations().contains(newSection.getUpStation())) | ||
| .filter(section -> !getStations().contains(newSection.getDownStation())) | ||
| .findAny() | ||
| .ifPresent(unused -> { |
There was a problem hiding this comment.
ifPresent๋ ์์ธ ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํ์ง ์์ต๋๋ค.
orElseThrow๋ก ์ ์ฉํด๋ณด์๊ธฐ ๋ฐ๋๋๋ค.
|
|
||
| for (Section section : sections) { | ||
| Station upStation = section | ||
| .getUpStation(); |
There was a problem hiding this comment.
์ํฐ๋ก ๊ตฌ๋ถํด์ผ ๋ ๋งํผ ๋ณต์กํ ์ฝ๋๊ฐ ์๋ ๊ฒ ๊ฐ์ผ๋ ๋ถํ์ํ ์ํฐ๋ ์ง์ํฉ์๋ค.
| } | ||
|
|
||
| if (upStation.equals(prevDownStation)) { | ||
| if (downStation.equals(newSection.getDownStation())) { |
There was a problem hiding this comment.
for, if๋ก ์ด์ด์ง๋ 2๋์ค ์ฝ๋๋ค์ด ๋ณด์ด๋ค์
- 1๋์ค๋ง ์ฌ์ฉํ๋๋ก ๋ฉ์๋๋ฅผ์์ ํด๋ณด์๊ณ
- ๋ฉ์๋๋ฅผ 10์ค ์ด๋ด๋ก ์ค์ฌ๋ณด์ธ์.
| newSection.getUpStation(), | ||
| (oldSection.getDistance() - newSection.getDistance()) | ||
| )); | ||
| } else if (newSection.getDistance() > oldSection.getDistance()) { |
There was a problem hiding this comment.
- else if๋ฅผ ์ฌ์ฉํ์ง ์๋๋ก ๋ฐ๊ฟ๋ณด์๊ธฐ ๋ฐ๋๋๋ค. else if๊ฐ ๋ง์์ง๋ฉด ์ฝ๋ ๋ณต์ก๋๊ฐ ์ฌ๋ผ๊ฐ๋๋ค.
- dto๋ก ๊ฐ์ ์ ๋ฌํ ๋ ์ธ์๋ getter๋ฅผ ์ฌ์ฉํ์ง ๋ง์ธ์. section ๊ฐ์ฒด ๋ด๋ถ์์ ์ฒ๋ฆฌํ๋๋ก ์ฝ๋๋ฅผ ์์ ํ๋ฉด line ํด๋์ค์ ๋ณต์ก๋๋ฅผ ๋ฎ์ถ ์ ์์๊ฒ๋๋ค.
๋ค๋ฆ๊ฒ ๋ฆฌ๋ทฐ ์์ฒญ๋๋ฆฝ๋๋ค.
์๊ตฌ์ฌํญ์ ์๋ Line ๋จ์ํ ์คํธ์ ๊ตฌ๊ฐ๊ด๋ฆฌ ์ธ์ํ ์คํธ๋ฅผ ์๋ฃํ์ต๋๋ค.
์๊ฐ๋ด์ด ๋ฆฌ๋ทฐํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.