-
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
Week1 step3 #724
base: sanghou
Are you sure you want to change the base?
Week1 step3 #724
Changes from all commits
f9d6204
0048d9c
f6c7aac
5719092
ad987a4
b7147a1
2d2c49f
f38468d
54e05c1
a2176c4
4eb3fe1
1ed21d6
59c4c8e
33eeb02
7a87a68
93a2671
32d0bde
155cf3d
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,19 @@ | ||
package subway; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class SubwayExceptionHandler { | ||
|
||
@ExceptionHandler(value = IllegalArgumentException.class) | ||
public ResponseEntity<Void> handleIllegalStateException(IllegalArgumentException error) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
@ExceptionHandler(value = IllegalStateException.class) | ||
public ResponseEntity<Void> handleIllegalStateException(IllegalStateException error) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package subway.line; | ||
|
||
import javax.persistence.CascadeType; | ||
import java.util.List; | ||
import javax.persistence.Embedded; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToOne; | ||
import subway.section.Section; | ||
import subway.sections.Sections; | ||
import subway.station.Station; | ||
|
||
@Entity | ||
|
@@ -19,23 +21,15 @@ public class Line { | |
|
||
private String color; | ||
|
||
@ManyToOne(cascade = CascadeType.ALL) | ||
private Station upStation; // 상행역 | ||
|
||
@ManyToOne(cascade = CascadeType.ALL) | ||
private Station downStation; // 하행역 | ||
|
||
private Long distance; | ||
@Embedded | ||
private Sections 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. 이런 컬렉션(with 일급 컬렉션)자료는 필드 초기화를 해주면 NPE에서 좀 더 자유로워 질 수 있습니다 ㅎㅎ |
||
|
||
public Line() {} | ||
|
||
public Line(String name, String color, Station upStation, Station downStation, | ||
Long distance) { | ||
public Line(String name, String color) { | ||
this.name = name; | ||
this.color = color; | ||
this.upStation = upStation; | ||
this.downStation = downStation; | ||
this.distance = distance; | ||
this.sections = new Sections(); | ||
} | ||
|
||
public Long getId() { | ||
|
@@ -46,25 +40,26 @@ public String getName() { | |
return name; | ||
} | ||
|
||
public Station getUpStation() { | ||
return upStation; | ||
} | ||
|
||
public Station getDownStation() { | ||
return downStation; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public Long getDistance() { | ||
return distance; | ||
public List<Station> getStations() { | ||
return sections.getStations(); | ||
} | ||
|
||
public Line updateLine(String name, String color) { | ||
this.name = name; | ||
this.color = color; | ||
return this; | ||
} | ||
|
||
public Line addSection(Section add) { | ||
sections.addSection(add); | ||
return this; | ||
} | ||
|
||
public void removeSection(Station downStation) { | ||
sections.removeSection(downStation); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,37 +5,44 @@ | |
import java.util.stream.Collectors; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import subway.section.Section; | ||
import subway.section.SectionCreateRequest; | ||
import subway.section.SectionRepository; | ||
import subway.section.SectionResponse; | ||
import subway.station.Station; | ||
import subway.station.StationRepository; | ||
import subway.station.StationService; | ||
|
||
@Service | ||
public class LineService { | ||
|
||
private final LineRepository lineRepository; | ||
private final StationRepository stationRepository; | ||
private final StationService stationService; | ||
private final SectionRepository sectionRepository; | ||
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 LineService(LineRepository repository, StationRepository stationRepository) { | ||
public LineService(LineRepository repository, StationService stationService, SectionRepository sectionRepository) { | ||
this.lineRepository = repository; | ||
this.stationRepository = stationRepository; | ||
this.stationService = stationService; | ||
this.sectionRepository = sectionRepository; | ||
} | ||
|
||
@Transactional | ||
public LineResponse saveLine(String name, String color, Long upStationId, Long downStationId, Long distance) { | ||
Optional<Station> inbound = stationRepository.findById(upStationId); | ||
Optional<Station> outbound = stationRepository.findById(downStationId); | ||
Line created = lineRepository.save(new Line(name, color, inbound.get(), outbound.get(), distance)); | ||
return createServiceResponse(created); | ||
public LineResponse saveLine(LineCreateRequest request) { | ||
Station upStation = stationService.findById(request.getUpStationId()); | ||
Station downStation = stationService.findById(request.getDownStationId()); | ||
|
||
Line created = lineRepository.save(new Line(request.getName(), request.getColor())); | ||
createSection(created.getId(), new SectionCreateRequest(upStation.getId(), downStation.getId(), request.getDistance())); | ||
|
||
return LineResponse.from(created); | ||
Comment on lines
+30
to
+36
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 Optional<LineResponse> showLine(Long id) { | ||
Optional<Line> optionalLine = lineRepository.findById(id); | ||
|
||
return optionalLine.map(this::createServiceResponse); | ||
return lineRepository.findById(id).map(LineResponse::from); | ||
} | ||
|
||
public List<LineResponse> showLines() { | ||
return lineRepository.findAll().stream() | ||
.map(this::createServiceResponse) | ||
.map(LineResponse::from) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
|
@@ -47,11 +54,25 @@ public Optional<LineResponse> updateLine(Long id, String name, String color) { | |
Optional<Line> optionalLine = lineRepository.findById(id); | ||
|
||
return optionalLine.map(line -> line.updateLine(name, color)) | ||
.map(lineRepository::save) | ||
.map(this::createServiceResponse); | ||
.map(line -> LineResponse.from(lineRepository.save(line))); | ||
} | ||
|
||
@Transactional | ||
public SectionResponse createSection(Long lineId, SectionCreateRequest request) { | ||
Station upStation = stationService.findById(request.getUpStationId()); | ||
Station downStation = stationService.findById(request.getDownStationId()); | ||
Line line = lineRepository.findById(lineId).orElseThrow(IllegalArgumentException::new); | ||
|
||
Section created = sectionRepository.save(new Section(upStation, downStation, request.getDistance())); | ||
|
||
lineRepository.save(line.addSection(created)); | ||
|
||
return SectionResponse.from(created); | ||
} | ||
|
||
private LineResponse createServiceResponse(Line line) { | ||
return new LineResponse(line.getId(), line.getName(), line.getColor(), line.getUpStation(), line.getDownStation(), line.getDistance()); | ||
public void removeSection(Long lineId, Long downStationId) { | ||
Line line = lineRepository.findById(lineId).orElseThrow(IllegalArgumentException::new); | ||
Station station = stationService.findById(downStationId); | ||
line.removeSection(station); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package subway.section; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToOne; | ||
import subway.station.Station; | ||
|
||
@Entity | ||
public class Section { | ||
|
||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne | ||
private Station upStation; | ||
|
||
@ManyToOne | ||
private Station downStation; | ||
|
||
private Long distance; | ||
|
||
public Long getId() { | ||
return 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. 불필요한 주석은 제거해주세요! |
||
public Station getUpStation() { | ||
return upStation; | ||
} | ||
|
||
// 하행역 | ||
public Station getDownStation() { | ||
return downStation; | ||
} | ||
|
||
public Long getDistance() { | ||
return distance; | ||
} | ||
|
||
public Section() {} | ||
|
||
public Section(Station upStation, Station downStation, Long distance) { | ||
this.upStation = upStation; | ||
this.downStation = downStation; | ||
this.distance = distance; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("[Section id : %d, upStationId : %d, downStationId: %d]", | ||
id, | ||
upStation.getId(), | ||
downStation.getId() | ||
); | ||
} | ||
} |
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.
예외 처리 핸들러를 만드셨네요 👍
그런데 IllegalArgumentException과 IllegalStateException이 같은 동작을 하고 있네요.
두 예외의 차이점이 뭘까요?