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๋‹จ๊ณ„ - ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ๊ด€๋ฆฌ #968

Open
wants to merge 10 commits into
base: nice7677
Choose a base branch
from
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@
- [X] ์ง€ํ•˜์ฒ  ๋…ธ์„  ์กฐํšŒ
- [X] ์ง€ํ•˜์ฒ  ๋…ธ์„  ์ˆ˜์ •
- [X] ์ง€ํ•˜์ฒ  ๋…ธ์„  ์‚ญ์ œ

# ๐Ÿš€ 3๋‹จ๊ณ„ - ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ๊ด€๋ฆฌ

## ์š”๊ตฌ์‚ฌํ•ญ

### ๊ตฌ๊ฐ„ ๋“ฑ๋ก ๊ธฐ๋Šฅ

- [X] ์ง€ํ•˜์ฒ  ๋…ธ์„ ์— ๊ตฌ๊ฐ„์„ ๋“ฑ๋ก
- [X] ์ƒˆ๋กœ์šด ๊ตฌ๊ฐ„์˜ ์ƒํ–‰์—ญ์€ ํ•ด๋‹น ๋…ธ์„ ์— ๋“ฑ๋ก๋˜์–ด์žˆ๋Š” ํ•˜ํ–‰ ์ข…์ ์—ญ์ด์–ด์•ผ ํ•œ๋‹ค.
- [X] ์ƒˆ๋กœ์šด ๊ตฌ๊ฐ„์˜ ํ•˜ํ–‰์—ญ์€ ํ•ด๋‹น ๋…ธ์„ ์— ๋“ฑ๋ก๋˜์–ด์žˆ๋Š” ์—ญ์ผ ์ˆ˜ ์—†๋‹ค.
- [X] ์ƒˆ๋กœ์šด ๊ตฌ๊ฐ„ ๋“ฑ๋ก์‹œ ์œ„ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ ์ฒ˜๋ฆฌํ•œ๋‹ค.
Empty file.
16 changes: 16 additions & 0 deletions src/main/java/subway/common/execption/CustomExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package subway.common.execption;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import subway.common.execption.response.ErrorResponse;

@ControllerAdvice
public class CustomExceptionHandler {

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e) {
return ResponseEntity.badRequest().body(ErrorResponse.of(e.getMessage()));
}

}
25 changes: 25 additions & 0 deletions src/main/java/subway/common/execption/response/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package subway.common.execption.response;

public class ErrorResponse {

private final String message;

public ErrorResponse(String message) {
this.message = message;
}

public static ErrorResponse of(String message) {
return new ErrorResponse(message);
}

@Override
public String toString() {
return "ErrorResponse{" +
"message='" + message + '\'' +
'}';
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package subway.route.code;
package subway.line.code;

public enum RouteValidateTypeCode {
public enum LineValidateTypeCode {

SAVE("์ด๋ฆ„"),
UPDATE("์•„์ด๋””์™€ ์ด๋ฆ„"),
Expand All @@ -9,7 +9,7 @@ public enum RouteValidateTypeCode {

private final String description;

RouteValidateTypeCode(String description) {
LineValidateTypeCode(String description) {
this.description = description;
}

Expand Down
75 changes: 75 additions & 0 deletions src/main/java/subway/line/controller/LineController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package subway.line.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import subway.line.code.LineValidateTypeCode;
import subway.line.domain.Line;
import subway.line.dto.request.LineRequest;
import subway.line.dto.request.SectionRequest;
import subway.line.dto.response.LineResponse;
import subway.line.dto.response.SectionResponse;
import subway.line.service.LineService;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/lines")
public class LineController {

private final LineService lineService;

public LineController(LineService LineService) {
this.lineService = LineService;
}

@PostMapping()
public ResponseEntity<LineResponse> createLine(@RequestBody LineRequest LineRequest) {
LineResponse Line = lineService.saveLine(LineValidateTypeCode.SAVE, LineRequest);
return ResponseEntity.created(URI.create("/lines/" + Line.getId())).body(Line);
}

@GetMapping("/{id}")
public ResponseEntity<LineResponse> inquiryLine(@PathVariable Long id) {
LineResponse Line = lineService.inquiryLine(id);
return ResponseEntity.ok().body(Line);
}

@GetMapping("")
public ResponseEntity<List<LineResponse>> inquiryLines() {
List<LineResponse> Lines = lineService.inquiryLines();
return ResponseEntity.ok().body(Lines);
}

@PutMapping("/{id}")
public ResponseEntity<LineResponse> updateLine(@PathVariable Long id, @RequestBody LineRequest LineRequest) {
LineRequest.saveId(id);
LineResponse Line = lineService.saveLine(LineValidateTypeCode.UPDATE, LineRequest);
return ResponseEntity.ok().body(Line);
}


@DeleteMapping("/{id}")
public ResponseEntity<LineResponse> deleteLine(@PathVariable Long id) {
lineService.deleteLine(id);
return ResponseEntity.noContent().build();
}

@PostMapping("/{id}/sections")
public ResponseEntity<SectionResponse> createSection(@PathVariable Long id, @RequestBody SectionRequest sectionRequest) {
return ResponseEntity.created(URI.create("/lines/" + id + "/sections")).body(lineService.updateSection(id, sectionRequest));
}

@GetMapping("/{id}/sections")
public ResponseEntity<List<SectionResponse>> inquirySection(@PathVariable Long id) {
return ResponseEntity.ok().body(lineService.inquirySection(id));
}

@DeleteMapping("/{id}/sections")
public ResponseEntity<SectionResponse> deleteSection(@PathVariable Long id, @RequestParam Long stationId) {
lineService.deleteSection(id, stationId);
return ResponseEntity.noContent().build();
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package subway.route.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import subway.station.domain.Station;
package subway.line.domain;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Entity
public class Route {
public class Line {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -25,24 +23,30 @@ public class Route {
@Column(length = 10, nullable = false)
private Integer distance;

public Route() {
@OneToMany(mappedBy = "line", fetch = FetchType.EAGER)
private List<Section> sections = new ArrayList<>();

public Line() {
}

public Route(Long id, String name, String color, Integer distance) {
public Line(Long id, String name, String color, Integer distance) {
this.id = id;
this.name = name;
this.color = color;
this.distance = distance;
}

public static Route of(Long id, String name, String color, Integer distance) {
return new Route(id, name, color, distance);
public static Line of(Long id, String name, String color, Integer distance) {
return new Line(id, name, color, distance);
}

public void saveStations(Stations stations) {
this.stations = stations;
}

public void saveSections(List<Section> sections) {
this.sections = sections;
}
public Long getId() {
return id;
}
Expand All @@ -63,12 +67,16 @@ public Integer getDistance() {
return distance;
}

public List<Section> getSections() {
return sections;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Route route = (Route) o;
Line route = (Line) o;

if (!Objects.equals(id, route.id)) return false;
return Objects.equals(name, route.name);
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/subway/line/domain/Section.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package subway.line.domain;

import subway.station.domain.Station;

import javax.persistence.*;

@Entity
public class Section {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "line_id", nullable = false)
private Line line;

@ManyToOne
@JoinColumn(name = "station_id", nullable = false)
private Station station;

Comment on lines +18 to +21

Choose a reason for hiding this comment

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

Station์ด ์•„๋‹ˆ๋ผ Stations๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•˜์ง€ ์•Š์„๊นŒ์š”? ๐Ÿค” Stations์™€ Section์˜ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ๊ฐ€ ์—†์–ด๋ณด์—ฌ์š”.
๊ทธ๋ฆฌ๊ณ  line์—์„œ section์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ• ๊นŒ์š”? stations์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ• ๊นŒ์š”? ์š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด๋ด์ฃผ์„ธ์š”!

Copy link
Author

Choose a reason for hiding this comment

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

์ƒ๊ฐ๋ชปํ–ˆ๋„ค์š” ๊ทธ๋ ‡๊ฒŒํ•˜๋ฉด ํ•ด๊ฒฐ๋˜๋Š” ๋ฌธ์ œ์˜€๋Š”๋ฐ ๋Œ๊ณ ๋Œ์•„์„œ ๋งŒ๋“ค์—ˆ๋„ค์š”๐Ÿ˜…

public Section() {
}

public Section(Line line, Station station) {
this.line = line;
this.station = station;
}

public Long getId() {
return id;
}

public Line getLine() {
return line;
}

public Station getStation() {
return station;
}

@Override
public String toString() {
return "Section{" +
"id=" + id +
", line=" + line +
", station=" + station +
'}';
}

}
43 changes: 43 additions & 0 deletions src/main/java/subway/line/domain/Stations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package subway.line.domain;

import subway.station.domain.Station;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import java.io.Serializable;

@Embeddable
public class Stations implements Serializable {

@OneToOne
@JoinColumn(name = "up_station_id", nullable = false)
private Station upStation;

@OneToOne
@JoinColumn(name = "down_station_id", nullable = false)
private Station downStation;

public Stations() {}

public Stations(Station upStation, Station downStation) {
this.upStation = upStation;
this.downStation = downStation;
}

public Station getUpStation() {
return upStation;
}

public Station getDownStation() {
return downStation;
}

@Override
public String toString() {
return "Stations{" +
"upStation=" + upStation +
", downStation=" + downStation +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package subway.route.dto;
package subway.line.dto.request;

import subway.route.domain.Route;
import subway.line.domain.Line;

public class RouteRequest {
public class LineRequest {

private Long id;
private String name;
Expand All @@ -11,8 +11,8 @@ public class RouteRequest {
private Long downStationId;
private Integer distance;

public Route toEntity() {
return Route.of(id, name, color, distance);
public Line toEntity() {
return Line.of(id, name, color, distance);
}

public void saveId(Long id) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/subway/line/dto/request/SectionRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package subway.line.dto.request;

public class SectionRequest {

private final Long id;
private final Long downStationId;
private final Long upStationId;
private final Long distance;

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

public Long getId() {
return id;
}

public Long getDownStationId() {
return downStationId;
}

public Long getUpStationId() {
return upStationId;
}

public Long getDistance() {
return distance;
}
}
Loading