diff --git a/src/main/java/wooteco/subway/admin/controller/LineController.java b/src/main/java/wooteco/subway/admin/controller/LineController.java index 387114afbf..c3e6b986f9 100644 --- a/src/main/java/wooteco/subway/admin/controller/LineController.java +++ b/src/main/java/wooteco/subway/admin/controller/LineController.java @@ -22,14 +22,14 @@ @RestController @RequestMapping("/lines") public class LineController { - private LineService lineService; + private final LineService lineService; public LineController(LineService lineService) { this.lineService = lineService; } @PostMapping - public ResponseEntity createLines(@RequestBody Request view) { + public ResponseEntity createLines(@RequestBody Request view) { Line line = view.getContent().toLine(); Line persistLine = lineService.save(line); @@ -39,25 +39,25 @@ public ResponseEntity createLines(@RequestBody Request view) { } @GetMapping - public ResponseEntity showLines() { - List lines = lineService.showLines(); - return ResponseEntity.ok().body(lines); + public ResponseEntity> showLines() { + List lineResponses = lineService.showLines(); + return ResponseEntity.ok().body(lineResponses); } @GetMapping("/{id}") - public ResponseEntity getLine(@PathVariable Long id) { + public ResponseEntity getLine(@PathVariable Long id) { LineResponse lineResponse = lineService.findLineWithStationsById(id); return ResponseEntity.ok().body(lineResponse); } @PutMapping("/{id}") - public ResponseEntity updateLine(@PathVariable Long id, @RequestBody Request lineRequest) { + public ResponseEntity updateLine(@PathVariable Long id, @RequestBody Request lineRequest) { lineService.updateLine(id, lineRequest.getContent().toLine()); return ResponseEntity.ok().build(); } @DeleteMapping("/{id}") - public ResponseEntity deleteLine(@PathVariable Long id) { + public ResponseEntity deleteLine(@PathVariable Long id) { lineService.deleteLineById(id); return ResponseEntity.noContent().build(); } diff --git a/src/main/java/wooteco/subway/admin/controller/LineStationController.java b/src/main/java/wooteco/subway/admin/controller/LineStationController.java index 0006611e6e..bf6d4a9f53 100644 --- a/src/main/java/wooteco/subway/admin/controller/LineStationController.java +++ b/src/main/java/wooteco/subway/admin/controller/LineStationController.java @@ -20,7 +20,7 @@ public LineStationController(LineService lineService) { } @PostMapping - public ResponseEntity createLineStation(@PathVariable Long lineId, + public ResponseEntity createLineStation(@PathVariable Long lineId, @RequestBody Request lineStationRequest) { LineStationCreateRequest lineStationCreateRequest = lineStationRequest.getContent(); lineService.addLineStation(lineId, lineStationCreateRequest); @@ -31,13 +31,13 @@ public ResponseEntity createLineStation(@PathVariable Long lineId, } @GetMapping - public ResponseEntity getLineStations(@PathVariable Long lineId) { + public ResponseEntity> getLineStations(@PathVariable Long lineId) { List lineStations = lineService.findLineStations(lineId); return ResponseEntity.ok().body(lineStations); } @DeleteMapping("/{stationId}") - public ResponseEntity deleteLine(@PathVariable Long lineId, @PathVariable Long stationId) { + public ResponseEntity deleteLine(@PathVariable Long lineId, @PathVariable Long stationId) { lineService.removeLineStation(lineId, stationId); return ResponseEntity.noContent().build(); } diff --git a/src/main/java/wooteco/subway/admin/controller/StationController.java b/src/main/java/wooteco/subway/admin/controller/StationController.java index c8204970ab..42e2216e16 100644 --- a/src/main/java/wooteco/subway/admin/controller/StationController.java +++ b/src/main/java/wooteco/subway/admin/controller/StationController.java @@ -1,16 +1,15 @@ package wooteco.subway.admin.controller; -import java.net.URI; - import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; - import wooteco.subway.admin.domain.Station; import wooteco.subway.admin.dto.Request; import wooteco.subway.admin.dto.StationCreateRequest; import wooteco.subway.admin.dto.StationResponse; import wooteco.subway.admin.repository.StationRepository; +import java.net.URI; + @RestController @RequestMapping("/stations") public class StationController { @@ -21,7 +20,7 @@ public StationController(StationRepository stationRepository) { } @PostMapping - public ResponseEntity createStation(@RequestBody Request view) { + public ResponseEntity createStation(@RequestBody Request view) { Station station = view.getContent().toStation(); Station persistStation = stationRepository.save(station); @@ -31,12 +30,12 @@ public ResponseEntity createStation(@RequestBody Request v } @GetMapping - public ResponseEntity showStations() { + public ResponseEntity> showStations() { return ResponseEntity.ok().body(stationRepository.findAll()); } @DeleteMapping("/{id}") - public ResponseEntity deleteStation(@PathVariable Long id) { + public ResponseEntity deleteStation(@PathVariable Long id) { stationRepository.deleteById(id); return ResponseEntity.noContent().build(); } diff --git a/src/main/java/wooteco/subway/admin/domain/LineStation.java b/src/main/java/wooteco/subway/admin/domain/LineStation.java index 2e7395dad4..136f0a1366 100644 --- a/src/main/java/wooteco/subway/admin/domain/LineStation.java +++ b/src/main/java/wooteco/subway/admin/domain/LineStation.java @@ -36,10 +36,6 @@ public int getDuration() { return duration; } - public void updatePreLineStation(Long preStationId) { - this.preStationId = preStationId; - } - public void modifyPreStationId(Long preStationId) { this.preStationId = preStationId; } diff --git a/src/main/java/wooteco/subway/admin/dto/LineResponse.java b/src/main/java/wooteco/subway/admin/dto/LineResponse.java index 34ff2329f8..5134da97db 100644 --- a/src/main/java/wooteco/subway/admin/dto/LineResponse.java +++ b/src/main/java/wooteco/subway/admin/dto/LineResponse.java @@ -1,17 +1,16 @@ package wooteco.subway.admin.dto; +import wooteco.subway.admin.domain.Line; +import wooteco.subway.admin.domain.Station; + import java.time.LocalDateTime; import java.time.LocalTime; +import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -import wooteco.subway.admin.domain.Line; -import wooteco.subway.admin.domain.LineStation; -import wooteco.subway.admin.domain.Station; - public class LineResponse { private Long id; private String name; @@ -28,7 +27,7 @@ public LineResponse() { } public LineResponse(Long id, String name, LocalTime startTime, LocalTime endTime, int intervalTime, - LocalDateTime createdAt, LocalDateTime updatedAt, Set stations) { + LocalDateTime createdAt, LocalDateTime updatedAt, String bgColor, Set stations) { this.id = id; this.name = name; this.startTime = startTime; @@ -36,23 +35,18 @@ public LineResponse(Long id, String name, LocalTime startTime, LocalTime endTime this.intervalTime = intervalTime; this.createdAt = createdAt; this.updatedAt = updatedAt; + this.bgColor = bgColor; this.stations = stations; } public static LineResponse of(Line line) { return new LineResponse(line.getId(), line.getName(), line.getStartTime(), line.getEndTime(), - line.getIntervalTime(), line.getCreatedAt(), line.getUpdatedAt(), new HashSet<>()); + line.getIntervalTime(), line.getCreatedAt(), line.getUpdatedAt(), line.getBgColor(), new HashSet<>()); } public static LineResponse of(Line line, Set stations) { return new LineResponse(line.getId(), line.getName(), line.getStartTime(), line.getEndTime(), - line.getIntervalTime(), line.getCreatedAt(), line.getUpdatedAt(), stations); - } - - public static List listOf(List lines) { - return lines.stream() - .map(it -> LineResponse.of(it)) - .collect(Collectors.toList()); + line.getIntervalTime(), line.getCreatedAt(), line.getUpdatedAt(), line.getBgColor(), stations); } public Long getId() { diff --git a/src/main/java/wooteco/subway/admin/service/LineService.java b/src/main/java/wooteco/subway/admin/service/LineService.java index 6307d6d13a..f3079f810a 100644 --- a/src/main/java/wooteco/subway/admin/service/LineService.java +++ b/src/main/java/wooteco/subway/admin/service/LineService.java @@ -10,6 +10,7 @@ import wooteco.subway.admin.repository.LineRepository; import wooteco.subway.admin.repository.StationRepository; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; @@ -17,8 +18,8 @@ @Service public class LineService { - private LineRepository lineRepository; - private StationRepository stationRepository; + private final LineRepository lineRepository; + private final StationRepository stationRepository; public LineService(LineRepository lineRepository, StationRepository stationRepository) { this.lineRepository = lineRepository; @@ -30,8 +31,16 @@ public Line save(Line line) { return lineRepository.save(line); } - public List showLines() { - return lineRepository.findAll(); + public List showLines() { + List lineResponses = new ArrayList<>(); + List lines = lineRepository.findAll(); + for (Line line : lines) { + List lineStationsId = line.findLineStationsId(); + Set stations = stationRepository.findAllById(lineStationsId); + lineResponses.add(LineResponse.of(line, stations)); + } + + return Collections.unmodifiableList(lineResponses); } public void updateLine(Long id, Line line) { diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000000..9aeafb1644 --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1,44 @@ +TRUNCATE TABLE STATION; +TRUNCATE TABLE LINE; +TRUNCATE TABLE LINE_STATION; + +insert into station values(1, '수원', CURRENT_TIMESTAMP()); +insert into station values(2, '화서', CURRENT_TIMESTAMP()); +insert into station values(3, '성균관대', CURRENT_TIMESTAMP()); + +insert into STATION values(4, '교대', CURRENT_TIMESTAMP()); +insert into STATION values(5, '강남', CURRENT_TIMESTAMP()); +insert into STATION values(6, '역삼', CURRENT_TIMESTAMP()); +insert into STATION values(7, '선릉', CURRENT_TIMESTAMP()); +insert into STATION values(8, '삼성', CURRENT_TIMESTAMP()); +insert into STATION values(9, '종합운동장', CURRENT_TIMESTAMP()); +insert into STATION values(10, '잠실새내', CURRENT_TIMESTAMP()); +insert into STATION values(11, '잠실', CURRENT_TIMESTAMP()); + +insert into STATION values(12, '당고개', CURRENT_TIMESTAMP()); +insert into STATION values(13, '상계', CURRENT_TIMESTAMP()); +insert into STATION values(14, '노원', CURRENT_TIMESTAMP()); +insert into STATION values(15, '창동', CURRENT_TIMESTAMP()); +insert into STATION values(16, '쌍문', CURRENT_TIMESTAMP()); + +insert into LINE values(1, '1호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-blue-700'); +insert into LINE values(2, '2호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-green-500'); +insert into LINE values(3, '3호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-orange-500'); +insert into LINE values(4, '4호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-blue-500'); +insert into LINE values(5, '5호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-purple-500'); +insert into LINE values(6, '6호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-yellow-500'); +insert into LINE values(7, '7호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-green-500'); +insert into LINE values(8, '8호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-pink-500'); + +insert into LINE_STATION values(1, 0, 1, null, 0, 0); +insert into LINE_STATION values(1, 1, 2, 1, 10, 2); +insert into LINE_STATION values(1, 2, 3, 2, 10, 2); + +insert into LINE_STATION values(2, 0, 4, null, 0, 0); +insert into LINE_STATION values(2, 1, 5, 4, 10, 2); +insert into LINE_STATION values(2, 2, 6, 5, 10, 2); +insert into LINE_STATION values(2, 3, 7, 6, 10, 2); +insert into LINE_STATION values(2, 4, 8, 7, 10, 2); +insert into LINE_STATION values(2, 5, 9, 8, 10, 2); +insert into LINE_STATION values(2, 6, 10, 9, 10, 2); +insert into LINE_STATION values(2, 7, 11, 10, 10, 2); \ No newline at end of file diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index be9fa9f912..bf25555382 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -26,39 +26,4 @@ create table if not exists LINE_STATION ( pre_station_id bigint, distance int, duration int -); - -insert into station values(1, '수원', CURRENT_TIMESTAMP()); -insert into station values(2, '화서', CURRENT_TIMESTAMP()); -insert into station values(3, '성균관대', CURRENT_TIMESTAMP()); - -insert into STATION values(4, '교대', CURRENT_TIMESTAMP()); -insert into STATION values(5, '강남', CURRENT_TIMESTAMP()); -insert into STATION values(6, '역삼', CURRENT_TIMESTAMP()); -insert into STATION values(7, '선릉', CURRENT_TIMESTAMP()); -insert into STATION values(8, '삼성', CURRENT_TIMESTAMP()); -insert into STATION values(9, '종합운동장', CURRENT_TIMESTAMP()); -insert into STATION values(10, '잠실새내', CURRENT_TIMESTAMP()); -insert into STATION values(11, '잠실', CURRENT_TIMESTAMP()); - -insert into LINE values(1, '1호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-blue-700'); -insert into LINE values(2, '2호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-green-500'); -insert into LINE values(3, '3호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-orange-500'); -insert into LINE values(4, '4호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-blue-500'); -insert into LINE values(5, '5호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-purple-500'); -insert into LINE values(6, '6호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-yellow-500'); -insert into LINE values(7, '7호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-green-500'); -insert into LINE values(8, '8호선', '05:30', '23:30', 10, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'bg-pink-500'); - -insert into LINE_STATION values(1, null, 1, null, 0, 0); -insert into LINE_STATION values(1, null, 2, 1, 10, 2); -insert into LINE_STATION values(1, null, 3, 2, 10, 2); - -insert into LINE_STATION values(2, null, 4, null, 0, 0); -insert into LINE_STATION values(2, null, 5, 4, 10, 2); -insert into LINE_STATION values(2, null, 6, 5, 10, 2); -insert into LINE_STATION values(2, null, 7, 6, 10, 2); -insert into LINE_STATION values(2, null, 8, 7, 10, 2); -insert into LINE_STATION values(2, null, 9, 8, 10, 2); -insert into LINE_STATION values(2, null, 10, 9, 10, 2); -insert into LINE_STATION values(2, null, 11, 10, 10, 2); \ No newline at end of file +); \ No newline at end of file diff --git a/src/main/resources/static/js/views/AdminEdge.js b/src/main/resources/static/js/views/AdminEdge.js index 2909784d04..87769da123 100644 --- a/src/main/resources/static/js/views/AdminEdge.js +++ b/src/main/resources/static/js/views/AdminEdge.js @@ -7,6 +7,7 @@ import api from "../../api/index.js"; function AdminEdge() { const $subwayLinesSlider = document.querySelector(".subway-lines-slider"); const $createLineStationButton = document.querySelector("#submit-button"); + const $stationSelectOptions = document.querySelector("#line-select-options"); const createSubwayEdgeModal = new Modal(); let subwayLines = []; @@ -22,20 +23,20 @@ function AdminEdge() { for (let i = 0; i < lines.length; i++) { const targetLine = lines[i]; const lineStations = targetLine["stations"]; - const lineStationsIds = lineStations.map(lineStation => lineStation["stationId"]); - const targetLineStations = lineStationsIds.map(id => stations.filter(station => station["id"] === id) - .map(station => station["name"])); + const lineStationsIds = lineStations.map(lineStation => lineStation["id"]); + const targetLineStationNames = lineStationsIds.map(id => stations.find(station => station["id"] === id)) + .map(station => station["name"]); const subwayLine = { lineId: targetLine["id"], title: targetLine["name"], bgColor: targetLine["bgColor"], - stations: targetLineStations + stations: targetLineStationNames } subwayLines = [...subwayLines, subwayLine]; } } - const initSubwayLinesSlider = () => { + const initSubwayLinesSlider = async () => { $subwayLinesSlider.innerHTML = subwayLines .map(line => subwayLinesItemTemplate(line)) .join(""); @@ -53,19 +54,47 @@ function AdminEdge() { }); }; - const initSubwayLineOptions = () => { + const initSubwayLineOptions = async () => { const subwayLineOptionTemplate = subwayLines .map(line => optionTemplate(line.title)) .join(""); - const $stationSelectOptions = document.querySelector( - "#line-select-options" - ); $stationSelectOptions.insertAdjacentHTML( "afterbegin", subwayLineOptionTemplate ); }; + async function initDepartStations() { + const lineName = $stationSelectOptions.options[$stationSelectOptions.selectedIndex].value; + const line = subwayLines.find(subway => subway["title"] === lineName); + const lineStations = ["출발역", ...line["stations"]]; + const departOptionTemplate = lineStations + .map(lineStation => optionTemplate(lineStation)) + .join(""); + const $departStationOptions = document.querySelector("#depart-station-name"); + $departStationOptions.innerHTML = ""; + $departStationOptions.insertAdjacentHTML("afterbegin", departOptionTemplate); + } + + async function initArrivalStations() { + const lineName = $stationSelectOptions.options[$stationSelectOptions.selectedIndex].value; + const line = subwayLines.find(subway => subway["title"] === lineName); + const lineStations = ["출발역", ...line["stations"]]; + const arrivalStations = stations.filter(station => !lineStations.some(name => name === station["name"])) + .map(station => station["name"]) + const arrivalOptionTemplate = arrivalStations + .map(lineStation => optionTemplate(lineStation)) + .join(""); + const $arrivalStationOptions = document.querySelector("#arrival-station-name"); + $arrivalStationOptions.innerHTML = ""; + $arrivalStationOptions.insertAdjacentHTML("afterbegin", arrivalOptionTemplate); + } + + async function initStationOptions() { + await initDepartStations(); + await initArrivalStations(); + } + const onRemoveStationHandler = async event => { const $target = event.target; const isDeleteButton = $target.classList.contains("mdi-delete"); @@ -117,13 +146,15 @@ function AdminEdge() { onRemoveStationHandler ); $createLineStationButton.addEventListener(EVENT_TYPE.CLICK, await onCreateLineStation); + $stationSelectOptions.addEventListener(EVENT_TYPE.CHANGE, await initStationOptions) }; this.init = async () => { await initSubwayLines(); - initSubwayLinesSlider(); - initSubwayLineOptions(); + await initSubwayLinesSlider(); + await initSubwayLineOptions(); await initEventListeners(); + await initStationOptions(); }; } diff --git a/src/main/resources/static/js/views/AdminStation.js b/src/main/resources/static/js/views/AdminStation.js index 13a6f2722d..52514ddefc 100644 --- a/src/main/resources/static/js/views/AdminStation.js +++ b/src/main/resources/static/js/views/AdminStation.js @@ -80,7 +80,7 @@ function AdminStation() { } function validateDuplicateStationName(stationNameInput, input) { - if (stations.includes(input)) { + if (stations.map(station => station["name"]).includes(input)) { alert(ERROR_MESSAGE.DUPLICATE); stationNameInput.value = CONSTANT.EMPTY; return false; diff --git a/src/main/resources/static/utils/constants.js b/src/main/resources/static/utils/constants.js index d29cee00d8..5b068e5ee5 100644 --- a/src/main/resources/static/utils/constants.js +++ b/src/main/resources/static/utils/constants.js @@ -1,6 +1,7 @@ export const EVENT_TYPE = { CLICK: "click", - KEY_PRESS: "keypress" + KEY_PRESS: "keypress", + CHANGE: "change" }; export const ERROR_MESSAGE = { diff --git a/src/main/resources/templates/admin-edge.html b/src/main/resources/templates/admin-edge.html index cf3d50e962..acf2643286 100644 --- a/src/main/resources/templates/admin-edge.html +++ b/src/main/resources/templates/admin-edge.html @@ -107,13 +107,10 @@ - +
대상역 - +