Skip to content
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

Open
wants to merge 34 commits into
base: sanhee
Choose a base branch
from
Open
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6b26f4a
chore: ์œˆ๋„์šฐ ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ๋กœ ์ˆ˜์ •
sanhee Feb 2, 2022
7bb6b7c
test: ๋…ธ์„ ์„ ์ƒ์„ฑํ•œ๋‹ค.
sanhee Feb 2, 2022
41e128f
test: ๋ชจ๋“  ๋…ธ์„ ์„ ์ถœ๋ ฅํ•œ๋‹ค.
sanhee Feb 2, 2022
68c3026
test: ํŠน์ • ๋…ธ์„ ์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
sanhee Feb 2, 2022
1c8d780
test: ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋…ธ์„ ์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
sanhee Feb 2, 2022
dc13b4b
test: ๋…ธ์„  ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.
sanhee Feb 2, 2022
a88d7d9
test: ๋…ธ์„ ์„ ์‚ญ์ œํ•œ๋‹ค.
sanhee Feb 2, 2022
136bacf
test: ๋…ธ์„ ์— ๊ตฌ๊ฐ„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
sanhee Feb 2, 2022
5ea7fe7
test: Line ์—”ํ‹ฐํ‹ฐ๋ฅผ LineResponse๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
sanhee Feb 2, 2022
64f3bb2
test: LineServiceMockTest - ๊ฐ€์งœ ํ˜‘๋ ฅ ๊ฐ์ฒด ์‚ฌ์šฉ ์—ฐ์Šต
sanhee Feb 2, 2022
6006960
refactor: ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ ๋กœ์ง Line ๋„๋ฉ”์ธ์œผ๋กœ ์ด์ „
sanhee Feb 2, 2022
16c15d6
refactor: LineResponse, StationResponse ์ƒ์„ฑ ์ฑ…์ž„ ์ž์‹ ์ด ๊ฐ–๋„๋ก ๋ณ€๊ฒฝ
sanhee Feb 2, 2022
73ef079
chore: LineServiceTest, LineServiceMockTest ํŒจํ‚ค์ง€ ์ด๋™
sanhee Feb 2, 2022
d4c6841
feat: ๋…ธ์„ ์— ์†ํ•ด์žˆ๋Š” ์—ญ ๋ชฉ๋ก ์กฐํšŒ
sanhee Feb 2, 2022
a0dcf15
feat: ๊ตฌ๊ฐ„์ด ๋ชฉ๋ก์—์„œ ๋งˆ์ง€๋ง‰ ์—ญ ์‚ญ์ œ
sanhee Feb 2, 2022
497bdf2
refactor: ๋…ธ์„  ์ƒ์„ฑ์‹œ ๊ตฌ๊ฐ„๋„ ๋™์‹œ์— ์ƒ์„ฑ ๊ฐ€๋Šฅํ•˜๊ฒŒ๋” ๋ณ€๊ฒฝ
sanhee Feb 2, 2022
b8ec505
feat: ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ - ์ƒํ–‰์—ญ์ด ๊ฐ™์€ ๊ฒฝ์šฐ / ์ƒˆ๋กœ์šด ๊ตฌ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ
sanhee Feb 2, 2022
3198710
feat: ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ - ์ƒํ–‰์—ญ์ด ๊ฐ™์€ ๊ฒฝ์šฐ / ๊ธฐ์กด ๊ตฌ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ
sanhee Feb 2, 2022
cf3985c
feat: ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ - ์ƒˆ๋กœ์šด ์—ญ์„ ์ƒํ–‰ ์ข…์ ์œผ๋กœ ๋“ฑ๋กํ•  ๊ฒฝ์šฐ
sanhee Feb 2, 2022
58da291
test: ์ƒˆ๋กœ์šด ์—ญ์„ ํ•˜ํ–‰ ์ข…์ ์œผ๋กœ ๋“ฑ๋กํ•  ๊ฒฝ์šฐ
sanhee Feb 2, 2022
e296504
refactor: ๋…ธ์„  ์กฐํšŒ์‹œ ์‘๋‹ต๋˜๋Š” ์—ญ ๋ชฉ๋ก ์ˆ˜์ •
sanhee Feb 2, 2022
ca081f9
feat: ์—ญ ์‚ฌ์ด์— ์ƒˆ๋กœ์šด ์—ญ์„ ๋“ฑ๋กํ•  ๊ฒฝ์šฐ ๊ธฐ์กด ์—ญ ์‚ฌ์ด ๊ธธ์ด๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋“ฑ๋ก์„ ํ•  ์ˆ˜ ์—†์Œ
sanhee Feb 2, 2022
80ade28
feat: ์ƒํ–‰์—ญ๊ณผ ํ•˜ํ–‰์—ญ์ด ์ด๋ฏธ ๋…ธ์„ ์— ๋ชจ๋‘ ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค๋ฉด ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†์Œ
sanhee Feb 3, 2022
41a1510
feat: ์ƒํ–‰์—ญ๊ณผ ํ•˜ํ–‰์—ญ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋„ ํฌํ•จ๋˜์–ด์žˆ์ง€ ์•Š์œผ๋ฉด ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†์Œ
sanhee Feb 3, 2022
3d7384a
test: ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ - ์ƒํ–‰์—ญ์ด ๊ฐ™์€ ๊ฒฝ์šฐ์— ์ƒˆ๋กœ์šด ์—ญ์„ ๋“ฑ๋ก
sanhee Feb 5, 2022
1dd43a5
test: ์ƒˆ๋กœ์šด ์—ญ์„ ํ•˜ํ–‰ ์ข…์ ์œผ๋กœ ๋“ฑ๋กํ•  ๊ฒฝ์šฐ
sanhee Feb 5, 2022
c9c1f1c
test: ์—ญ ์‚ฌ์ด์— ์ƒˆ๋กœ์šด ์—ญ์„ ๋“ฑ๋กํ•  ๊ฒฝ์šฐ ๊ธฐ์กด ์—ญ ์‚ฌ์ด ๊ธธ์ด๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋“ฑ๋ก์„ ํ•  ์ˆ˜ ์—†์Œ
sanhee Feb 5, 2022
17f5336
test: ์ƒํ–‰์—ญ๊ณผ ํ•˜ํ–‰์—ญ์ด ์ด๋ฏธ ๋…ธ์„ ์— ๋ชจ๋‘ ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค๋ฉด ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†์Œ
sanhee Feb 5, 2022
dad7461
test: ์ƒํ–‰์—ญ๊ณผ ํ•˜ํ–‰์—ญ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋„ ํฌํ•จ๋˜์–ด์žˆ์ง€ ์•Š์œผ๋ฉด ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†์Œ
sanhee Feb 5, 2022
34c2341
refactor: ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ ์ธ์ˆ˜ํ…Œ์ŠคํŠธ
sanhee Feb 5, 2022
0a252c0
refactor: ๋…ธ์„  ์ €์žฅ์‹œ ์ถ”๊ฐ€๋กœ ๊ตฌ๊ฐ„์„ ์ €์žฅํ•˜๋Š” ๋กœ์ง ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌ
sanhee Feb 7, 2022
cd6e402
refactor: ๊ฐ์ฒด์ง€ํ–ฅ ์ƒํ™œ์ฒด์กฐ ์ฐธ๊ณ ํ•˜์—ฌ lineService ๋ฆฌํŒฉํ† ๋ง
sanhee Feb 7, 2022
13e4d71
refactor: DTO์—์„œ spring ์˜์กด ์• ๋…ธํ…Œ์ด์…˜ ์ œ๊ฑฐ
sanhee Feb 7, 2022
7ada152
refactor: Line ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ ๋กœ์ง ์˜์กด ์ œ๊ฑฐ ์‹œ๋„ (์•„์ง ์ง„ํ–‰์ค‘)
sanhee Feb 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.

This file was deleted.

Empty file.

This file was deleted.

70 changes: 28 additions & 42 deletions src/main/java/nextstep/subway/applicaion/LineService.java
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if์ ˆ ์กฐ๊ฑด์ด ๋ณต์žกํ•˜๋‹ˆ ๋ฉ”์„œ๋“œ๋กœ ์ถ”์ถœํ•ด๋ณด๋Š”๊ฒŒ ์ข‹๊ฒ ๋„ค์š”

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)

Choose a reason for hiding this comment

The 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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ฐ์ฒด์ง€ํ–ฅ ์ƒํ™œ์ฒด์กฐ
4. ํ•œ ์ค„์— ์ ์„ ํ•˜๋‚˜๋งŒ ์ฐ๋Š”๋‹ค.

line ๊ฐ์ฒด ๋‚ด๋ถ€๋กœ ์กฐ๊ฑด์„ ์˜ฎ๊ฒจ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

.get(line.getSections()
.size() - 1)
.getDownStation()
.equals(station)) {
throw new IllegalArgumentException();
}

line.getSections().remove(line.getSections().size() - 1);
line.getSections()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ฐ์ฒด์ง€ํ–ฅ ์ƒํ™œ์ฒด์กฐ
4. ํ•œ ์ค„์— ์ ์„ ํ•˜๋‚˜๋งŒ ์ฐ๋Š”๋‹ค.

.remove(line.getSections()
.size() - 1);
}
}
18 changes: 5 additions & 13 deletions src/main/java/nextstep/subway/applicaion/StationService.java
Original file line number Diff line number Diff line change
@@ -21,32 +21,24 @@ public StationService(StationRepository stationRepository) {

public StationResponse saveStation(StationRequest stationRequest) {
Station station = stationRepository.save(new Station(stationRequest.getName()));
return createStationResponse(station);
return StationResponse.from(station);
}

@Transactional(readOnly = true)
public List<StationResponse> findAllStations() {
List<Station> stations = stationRepository.findAll();

return stations.stream()
.map(this::createStationResponse)
.collect(Collectors.toList());
.map(StationResponse::from)
.collect(Collectors.toList());
}

public void deleteStationById(Long id) {
stationRepository.deleteById(id);
}

public StationResponse createStationResponse(Station station) {
return new StationResponse(
station.getId(),
station.getName(),
station.getCreatedDate(),
station.getModifiedDate()
);
}

public Station findById(Long id) {
return stationRepository.findById(id).orElseThrow(IllegalArgumentException::new);
return stationRepository.findById(id)
.orElseThrow(IllegalArgumentException::new);
}
}
34 changes: 34 additions & 0 deletions src/main/java/nextstep/subway/applicaion/dto/ErrorResponse.java
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{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class ErrorResponse{
public class ErrorResponse {


private HttpStatus status;

private String reason;

@CreatedDate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ์• ๋„ˆํ…Œ์ด์…˜์€ spring data์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋„ค์š”.
๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์˜์—ญ์˜ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•˜์‹œ๊ณ  dto์—์„œ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.
dto๋Š” ๋‹จ์ˆœ ๊ฐ’์„ ๋‹ค๋ฅธ ๋ ˆ์ด์–ด๋กœ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.

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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/nextstep/subway/applicaion/dto/LineRequest.java
Original file line number Diff line number Diff line change
@@ -7,6 +7,22 @@ public class LineRequest {
private Long downStationId;
private int distance;

public LineRequest() {
}

public LineRequest(String name, String color) {
this.name = name;
this.color = color;
}

public LineRequest(String name, String color, Long upStationId, Long downStationId, int distance) {
this.name = name;
this.color = color;
this.upStationId = upStationId;
this.downStationId = downStationId;
this.distance = distance;
}

public String getName() {
return name;
}
21 changes: 20 additions & 1 deletion src/main/java/nextstep/subway/applicaion/dto/LineResponse.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package nextstep.subway.applicaion.dto;

import nextstep.subway.domain.Line;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class LineResponse {
private Long id;
@@ -43,5 +46,21 @@ public LocalDateTime getCreatedDate() {
public LocalDateTime getModifiedDate() {
return modifiedDate;
}
}

public static List<LineResponse> from(List<Line> lines) {
return lines.stream()
.map(LineResponse::from)
.collect(Collectors.toList());
}

public static LineResponse from(Line line) {
return new LineResponse(
line.getId(),
line.getName(),
line.getColor(),
StationResponse.from(line),
line.getCreatedDate(),
line.getModifiedDate()
);
}
}
Original file line number Diff line number Diff line change
@@ -5,6 +5,15 @@ public class SectionRequest {
private Long downStationId;
private int distance;

public SectionRequest() {
}

public SectionRequest(Long upStationId, Long downStationId, int distance) {
this.upStationId = upStationId;
this.downStationId = downStationId;
this.distance = distance;
}

public Long getUpStationId() {
return upStationId;
}
Loading