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

πŸš€ 3단계 - μ§€ν•˜μ²  ꡬ간 관리 #970

Open
wants to merge 19 commits into
base: janeljs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f51f4ec
:memo: step 3 μš”κ΅¬μ‚¬ν•­ μ—…λ°μ΄νŠΈ
janeljs Jul 13, 2023
2a286c1
:recycle: BaseEntity 클래슀 μΆ”κ°€
janeljs Jul 13, 2023
2823721
:package: NotFoundException νŒ¨ν‚€μ§€ 이동
janeljs Jul 13, 2023
03bf561
:art: μ½”λ“œ μ •λ ¬
janeljs Jul 13, 2023
5d020a4
:heavy_plus_sign: mockito-core μ˜μ‘΄μ„± μΆ”κ°€
janeljs Jul 23, 2023
191e611
:recycle: StationResponseλ₯Ό record 클래슀둜 λ³€κ²½, StationMapper λ©”μ„œλ“œ 이름 μˆ˜μ •
janeljs Jul 23, 2023
8774fd2
:recycle: distanceλ₯Ό int type으둜 λ³€κ²½
janeljs Jul 23, 2023
a91f8a0
:sparkles: SectionAcceptanceTest μΆ”κ°€
janeljs Jul 29, 2023
72d5fbc
:sparkles: SectionController μΆ”κ°€, DTO 및 λ©”μ„œλ“œ 이름 ν˜•μ‹ 톡일
janeljs Jul 29, 2023
a47ed9a
:recycle: Station, Line κ΄€λ ¨ λ©”μ„œλ“œ 및 DTO 이름 톡일
janeljs Jul 29, 2023
11e8be0
:sparkles: ꡬ간 등둝, 제거 κΈ°λŠ₯ κ΅¬ν˜„
janeljs Jul 29, 2023
84c0ebd
:arrow_up: Java 16으둜 버전업
janeljs Jul 23, 2023
9b5259e
:package: ν…ŒμŠ€νŠΈ νŒ¨ν‚€μ§€ 이동
janeljs Jul 29, 2023
69d31c6
:pencil2: μ˜€νƒ€ μˆ˜μ •
janeljs Jul 29, 2023
63c094a
:recycle: Section 생성 μš”μ²­ μ‹œ 검증 λ‘œμ§μ„ Sections λ‚΄λΆ€λ‘œ 이동 및 λ¦¬νŒ©ν† λ§
janeljs Jul 30, 2023
348513e
:heavy_minus_sign: μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” μ˜μ‘΄μ„± μ‚­μ œ
janeljs Jul 30, 2023
eb8b58c
:recycle: ꡬ간 생성 ν…ŒμŠ€νŠΈμ½”λ“œ λ¦¬νŒ©ν† λ§
janeljs Jul 30, 2023
cfb49a7
:recycle: SectionService, Controller에 있던 λ‘œμ§μ„ LineService, Controller둜 이동
janeljs Jul 30, 2023
eb1ecaf
:recycle: Line, Sections에 ꡬ간 제거 λ©”μ„œλ“œ μΆ”κ°€
janeljs Aug 9, 2023
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
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ dependencies {
testImplementation 'io.rest-assured:rest-assured:4.5.1'
testImplementation 'com.navercorp.fixturemonkey:fixture-monkey-starter:0.5.8'
testImplementation 'com.google.guava:guava:16+'
testImplementation 'org.mockito:mockito-core'

// mapstruct
implementation 'org.mapstruct:mapstruct' + ":$mapstructVersion"
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/subway/common/Validation.java
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);
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/subway/line/controller/LineController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import subway.line.dto.ModifyLineRequest;
import subway.line.dto.ModifyLineResponse;
import subway.line.service.LineService;
import subway.section.dto.AddSectionRequest;
import subway.section.dto.AddSectionResponse;
import subway.section.dto.SectionResponse;

import java.util.List;

Expand Down Expand Up @@ -46,4 +49,23 @@ public ResponseEntity<Void> deleteLine(@PathVariable Long id) {
lineService.deleteLine(id);
return ResponseEntity.noContent().build();
}

@PostMapping("/lines/{lineId}/sections")
public ResponseEntity<AddSectionResponse> addSection(@PathVariable Long lineId, @RequestBody AddSectionRequest addSectionRequest
) {
AddSectionResponse addSectionResponse = lineService.addSection(lineId, addSectionRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(addSectionResponse);
}

@GetMapping("/lines/{lineId}/sections")
public ResponseEntity<List<SectionResponse>> findSections(@PathVariable Long lineId) {
List<SectionResponse> sections = lineService.findSections(lineId);
return ResponseEntity.ok(sections);
}

@DeleteMapping("/lines/{lineId}/sections")
public ResponseEntity<Void> deleteSection(@PathVariable Long lineId, @RequestParam Long stationId) {
lineService.deleteSection(lineId, stationId);
return ResponseEntity.noContent().build();
}
}
5 changes: 5 additions & 0 deletions src/main/java/subway/line/domain/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Line extends BaseEntity {
@Column(length = 20, nullable = false)
private String name;

@Setter
Copy link
Member

Choose a reason for hiding this comment

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

setterλ₯Ό μ œκ³΅ν•˜κΈ° 보닀 update or modify λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•˜κ³ 
λ©”μ„œλ“œ λ‚΄λΆ€μ—μ„œ 값을 μˆ˜μ •ν•˜λ„λ‘ ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?

@Column(length = 20, nullable = false)
private String color;

Expand Down Expand Up @@ -58,4 +59,8 @@ public void addSection(Section section) {
public boolean hasLessThanTwoSections() {
return sections.hasLessThanTwoSections();
}

public boolean isTerminalStationId(Long id) {
return getTerminalStationId().equals(id);
}
}
46 changes: 44 additions & 2 deletions src/main/java/subway/line/domain/Sections.java
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 {
Copy link
Member

Choose a reason for hiding this comment

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

일급 μ»¬λ ‰μ…˜ ν™œμš© πŸ‘

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

ꡬ간 등둝 κΈ°λŠ₯처럼 ꡬ간 제거 λ©”μ„œλ“œλ„ μΆ”κ°€ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?

Copy link
Author

Choose a reason for hiding this comment

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

리뷰 λ°˜μ˜ν–ˆμŠ΅λ‹ˆλ‹€!γ…Žγ…Ž eb1ecaf

}

public List<Station> getStations() {
Expand All @@ -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인 ꡬ간은 등둝할 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
}
}
64 changes: 59 additions & 5 deletions src/main/java/subway/line/service/LineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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());
}
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

setIfNotNull λ©”μ„œλ“œλ₯Ό κ±°μΉ˜μ§€ μ•Šκ³  line 의 값을 λ³€κ²½ν•˜λ©΄ λ„λ©”μΈμ˜ μ œμ•½μ‚¬ν•­μ΄ μ§€μΌœμ§€μ§€ μ•Šμ„ 것 κ°™μ•„μš”
null 체크λ₯Ό line λ‚΄λΆ€μ—μ„œ κ²€μ¦ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?

return LINE_MAPPER.toModifyLineResponse(line);
}

@Transactional
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

노선에 ν¬ν•¨λœ ꡬ간듀을 line 객체λ₯Ό ν™œμš©ν•΄μ„œ κ°€μ Έμ˜¬ μˆ˜κ°€ μžˆλŠ”λ°μš”
sectionRepository λŒ€μ‹  line 객체λ₯Ό ν™œμš©ν•΄μ„œ ꡬ간 λͺ©λ‘μ„ μ‘°νšŒν•˜λ©΄ μ–΄λ–¨κΉŒμš”?

Suggested change
List<Section> sections = sectionRepository.findAllByLine_Id(lineId);
Line line = findLineById(lineId);
List<Section> sections = line.getSections();

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);
}
Copy link
Member

Choose a reason for hiding this comment

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

addSection λ©”μ„œλ“œμ²˜λŸΌ line 을 μ‘°νšŒν•œ 후에 λ©”μ‹œμ§€λ₯Ό λ³΄λ‚΄μ„œ ꡬ간을 μ‚­μ œν•˜λ©΄ μ–΄λ–¨κΉŒμš”?
μ‚­μ œ κΈ°λŠ₯μ΄λ‹ˆ @Transactional도 μΆ”κ°€λ˜λ©΄ 쒋을 것 κ°™μ•„μš”
line.deleteSection(stationId);

Copy link
Author

Choose a reason for hiding this comment

The 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));
}
Expand Down
38 changes: 0 additions & 38 deletions src/main/java/subway/section/controller/SectionController.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CreateSectionResponse {
public class AddSectionRequest {
private Long upStationId;
private Long downStationId;
private int distance;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/subway/section/dto/AddSectionResponse.java
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;
}
44 changes: 0 additions & 44 deletions src/main/java/subway/section/dto/CreateSectionRequest.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/subway/section/mapper/SectionMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface SectionMapper {

@Mapping(target = "upStationId", source = "upStation.id")
@Mapping(target = "downStationId", source = "downStation.id")
CreateSectionResponse mapToAddSectionResponse(Section section);
AddSectionResponse mapToCreateSectionResponse(Section section);

@Mapping(target = "upStationId", source = "upStation.id")
@Mapping(target = "downStationId", source = "downStation.id")
Expand Down
Loading