-
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
π 3λ¨κ³ - μ§νμ² κ΅¬κ° κ΄λ¦¬ #970
base: janeljs
Are you sure you want to change the base?
Changes from 4 commits
f51f4ec
2a286c1
2823721
03bf561
5d020a4
191e611
8774fd2
a91f8a0
72d5fbc
a47ed9a
11e8be0
84c0ebd
9b5259e
69d31c6
63c094a
348513e
eb8b58c
cfb49a7
eb1ecaf
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package subway.common; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
public class Validation { | ||
public static <V> void setIfNotNull(V value, Consumer<V> setter) { | ||
if (Objects.nonNull(value)) { | ||
setter.accept(value); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package subway.line.domain; | ||
|
||
import subway.common.error.InvalidSectionRequestException; | ||
import subway.section.domain.Section; | ||
import subway.station.domain.Station; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.OneToMany; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
public class Sections { | ||
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. μΌκΈ 컬λ μ νμ© π |
||
|
@@ -29,8 +32,14 @@ public Section getLast() { | |
return sections.get(sections.size() - 1); | ||
} | ||
|
||
public void add(Section station) { | ||
sections.add(station); | ||
public void add(Section section) { | ||
if (!sections.isEmpty()) { | ||
validateUpStationId(section); | ||
validateDownStationId(section); | ||
validateDistance(section); | ||
} | ||
|
||
sections.add(section); | ||
Comment on lines
+35
to
+42
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. κ΅¬κ° λ±λ‘ κΈ°λ₯μ²λΌ κ΅¬κ° μ κ±° λ©μλλ μΆκ°νλ©΄ μ΄λ¨κΉμ? 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. 리뷰 λ°μνμ΅λλ€!γ γ eb1ecaf |
||
} | ||
|
||
public List<Station> getStations() { | ||
|
@@ -48,4 +57,37 @@ public Integer getTotalDistance() { | |
public boolean hasLessThanTwoSections() { | ||
return sections.size() < 2; | ||
} | ||
|
||
private void validateUpStationId(Section section) { | ||
Line line = section.getLine(); | ||
Long upStationId = section.getUpStationId(); | ||
|
||
if (!Objects.equals(upStationId, line.getTerminalStationId())) { | ||
throw new InvalidSectionRequestException("ν΄λΉ λ Έμ μ ννμ’ μ μμ΄ μλ μμ΄ μνμμΌλ‘ μ€μ λμμ΅λλ€.", | ||
Map.of( | ||
"lineId", line.getId().toString(), | ||
"upStationId", upStationId.toString(), | ||
"downStationId", section.getDownStationId().toString() | ||
)); | ||
} | ||
} | ||
|
||
private void validateDownStationId(Section section) { | ||
Line line = section.getLine(); | ||
Station downStation = section.getDownStation(); | ||
if (line.getStations().contains(downStation)) { | ||
throw new InvalidSectionRequestException("μ΄λ―Έ λ Έμ μ λ±λ‘λ μμ μλ‘μ΄ κ΅¬κ°μ ννμμΌλ‘ λ±λ‘νμμ΅λλ€.", | ||
Map.of( | ||
"lineId", line.getId().toString(), | ||
"upStationId", section.getUpStationId().toString(), | ||
"downStationId", section.getDownStationId().toString() | ||
)); | ||
} | ||
} | ||
|
||
private void validateDistance(Section section) { | ||
if (section.getDistance() < 1) { | ||
throw new InvalidSectionRequestException("κΈΈμ΄κ° 0μΈ κ΅¬κ°μ λ±λ‘ν μ μμ΅λλ€."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||||
import lombok.RequiredArgsConstructor; | ||||||||
import org.springframework.stereotype.Service; | ||||||||
import org.springframework.transaction.annotation.Transactional; | ||||||||
import subway.common.error.InvalidSectionRequestException; | ||||||||
import subway.common.error.NotFoundException; | ||||||||
import subway.line.domain.Line; | ||||||||
import subway.line.dto.CreateLineRequest; | ||||||||
|
@@ -11,19 +12,27 @@ | |||||||
import subway.line.dto.ModifyLineResponse; | ||||||||
import subway.line.repository.LineRepository; | ||||||||
import subway.section.domain.Section; | ||||||||
import subway.section.dto.AddSectionRequest; | ||||||||
import subway.section.dto.AddSectionResponse; | ||||||||
import subway.section.dto.SectionResponse; | ||||||||
import subway.section.repository.SectionRepository; | ||||||||
import subway.station.domain.Station; | ||||||||
import subway.station.repository.StationRepository; | ||||||||
|
||||||||
import java.util.List; | ||||||||
import java.util.Map; | ||||||||
import java.util.stream.Collectors; | ||||||||
|
||||||||
import static subway.common.Validation.*; | ||||||||
import static subway.line.mapper.LineMapper.LINE_MAPPER; | ||||||||
import static subway.section.mapper.SectionMapper.SECTION_MAPPER; | ||||||||
|
||||||||
@Service | ||||||||
@Transactional(readOnly = true) | ||||||||
@RequiredArgsConstructor | ||||||||
public class LineService { | ||||||||
private final LineRepository lineRepository; | ||||||||
private final SectionRepository sectionRepository; | ||||||||
private final StationRepository stationRepository; | ||||||||
|
||||||||
@Transactional | ||||||||
|
@@ -38,8 +47,8 @@ public LineResponse createLine(CreateLineRequest createLineRequest) { | |||||||
} | ||||||||
|
||||||||
public List<LineResponse> findAllLines() { | ||||||||
List<Line> lines = lineRepository.findAll(); | ||||||||
return lines.stream() | ||||||||
return lineRepository.findAll() | ||||||||
.stream() | ||||||||
.map(LINE_MAPPER::toLineResponse) | ||||||||
.collect(Collectors.toList()); | ||||||||
} | ||||||||
|
@@ -52,9 +61,9 @@ public LineResponse findLine(Long id) { | |||||||
@Transactional | ||||||||
public ModifyLineResponse modifyLine(Long id, ModifyLineRequest modifyLineRequest) { | ||||||||
Line line = findLineById(id); | ||||||||
line.setName(modifyLineRequest.getName()); | ||||||||
Line modifiedLine = lineRepository.save(line); | ||||||||
return LINE_MAPPER.toModifyLineResponse(modifiedLine); | ||||||||
setIfNotNull(modifyLineRequest.getName(), line::setName); | ||||||||
setIfNotNull(modifyLineRequest.getColor(), line::setColor); | ||||||||
Comment on lines
+62
to
+63
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. setIfNotNull λ©μλλ₯Ό κ±°μΉμ§ μκ³ line μ κ°μ λ³κ²½νλ©΄ λλ©μΈμ μ μ½μ¬νμ΄ μ§μΌμ§μ§ μμ κ² κ°μμ |
||||||||
return LINE_MAPPER.toModifyLineResponse(line); | ||||||||
} | ||||||||
|
||||||||
@Transactional | ||||||||
|
@@ -65,6 +74,51 @@ public void deleteLine(Long id) { | |||||||
lineRepository.deleteById(id); | ||||||||
} | ||||||||
|
||||||||
@Transactional | ||||||||
public AddSectionResponse addSection(Long lineId, AddSectionRequest addSectionRequest) { | ||||||||
Line line = findLineById(lineId); | ||||||||
Station upStation = findStationById(addSectionRequest.getUpStationId()); | ||||||||
Station downStation = findStationById(addSectionRequest.getDownStationId()); | ||||||||
|
||||||||
Section section = Section.builder() | ||||||||
.line(line) | ||||||||
.upStation(upStation) | ||||||||
.downStation(downStation) | ||||||||
.distance(addSectionRequest.getDistance()) | ||||||||
.build(); | ||||||||
|
||||||||
line.addSection(section); | ||||||||
return SECTION_MAPPER.mapToCreateSectionResponse(section); | ||||||||
} | ||||||||
|
||||||||
public List<SectionResponse> findSections(Long lineId) { | ||||||||
List<Section> sections = sectionRepository.findAllByLine_Id(lineId); | ||||||||
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 κ°μ²΄λ₯Ό νμ©ν΄μ κ°μ Έμ¬ μκ° μλλ°μ
Suggested change
|
||||||||
return sections.stream() | ||||||||
.map(SECTION_MAPPER::mapToSectionResponse) | ||||||||
.collect(Collectors.toList()); | ||||||||
} | ||||||||
|
||||||||
public void deleteSection(Long lineId, Long stationId) { | ||||||||
Line line = findLineById(lineId); | ||||||||
|
||||||||
if (line.hasLessThanTwoSections()) { | ||||||||
throw new InvalidSectionRequestException("ꡬκ°μ΄ 2κ° μ΄μμΌ λλ§ μμ ν μ μμ΅λλ€."); | ||||||||
} | ||||||||
|
||||||||
if (!line.isTerminalStationId(stationId)) { | ||||||||
throw new InvalidSectionRequestException("λ§μ§λ§ ꡬκ°λ§ μμ ν μ μμ΅λλ€."); | ||||||||
} | ||||||||
|
||||||||
Section section = sectionRepository.findByLine_IdAndDownStation_Id(lineId, stationId) | ||||||||
.orElseThrow(() -> new NotFoundException( | ||||||||
Map.of( | ||||||||
"lineId", lineId.toString(), | ||||||||
"stationId", stationId.toString() | ||||||||
))); | ||||||||
|
||||||||
sectionRepository.delete(section); | ||||||||
} | ||||||||
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. addSection λ©μλμ²λΌ line μ μ‘°νν νμ λ©μμ§λ₯Ό 보λ΄μ ꡬκ°μ μμ νλ©΄ μ΄λ¨κΉμ? 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. 리뷰 λ°μνμ΅λλ€!γ γ eb1ecaf |
||||||||
|
||||||||
private Line findLineById(Long id) { | ||||||||
return lineRepository.findById(id).orElseThrow(() -> new NotFoundException(id)); | ||||||||
} | ||||||||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package subway.section.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class AddSectionResponse { | ||
private Long upStationId; | ||
private Long downStationId; | ||
private int distance; | ||
} |
This file was deleted.
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.
setterλ₯Ό μ 곡νκΈ° 보λ€
update
ormodify
λ©μλλ₯Ό μ 곡νκ³λ©μλ λ΄λΆμμ κ°μ μμ νλλ‘ νλ©΄ μ΄λ¨κΉμ?