Skip to content

Commit

Permalink
Merge pull request #2 from giantim/atdd-mission-modify
Browse files Browse the repository at this point in the history
Atdd mission modify
  • Loading branch information
giantim authored May 11, 2020
2 parents f01afd5 + 50b89d3 commit 5c2393b
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LineRequest> view) {
public ResponseEntity<LineResponse> createLines(@RequestBody Request<LineRequest> view) {
Line line = view.getContent().toLine();
Line persistLine = lineService.save(line);

Expand All @@ -39,25 +39,25 @@ public ResponseEntity createLines(@RequestBody Request<LineRequest> view) {
}

@GetMapping
public ResponseEntity showLines() {
List<Line> lines = lineService.showLines();
return ResponseEntity.ok().body(lines);
public ResponseEntity<List<LineResponse>> showLines() {
List<LineResponse> lineResponses = lineService.showLines();
return ResponseEntity.ok().body(lineResponses);
}

@GetMapping("/{id}")
public ResponseEntity getLine(@PathVariable Long id) {
public ResponseEntity<LineResponse> 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> lineRequest) {
public ResponseEntity<LineResponse> updateLine(@PathVariable Long id, @RequestBody Request<LineRequest> lineRequest) {
lineService.updateLine(id, lineRequest.getContent().toLine());
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
public ResponseEntity deleteLine(@PathVariable Long id) {
public ResponseEntity<LineResponse> deleteLine(@PathVariable Long id) {
lineService.deleteLineById(id);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public LineStationController(LineService lineService) {
}

@PostMapping
public ResponseEntity createLineStation(@PathVariable Long lineId,
public ResponseEntity<LineStationResponse> createLineStation(@PathVariable Long lineId,
@RequestBody Request<LineStationCreateRequest> lineStationRequest) {
LineStationCreateRequest lineStationCreateRequest = lineStationRequest.getContent();
lineService.addLineStation(lineId, lineStationCreateRequest);
Expand All @@ -31,13 +31,13 @@ public ResponseEntity createLineStation(@PathVariable Long lineId,
}

@GetMapping
public ResponseEntity getLineStations(@PathVariable Long lineId) {
public ResponseEntity<List<LineStationResponse>> getLineStations(@PathVariable Long lineId) {
List<LineStationResponse> lineStations = lineService.findLineStations(lineId);
return ResponseEntity.ok().body(lineStations);
}

@DeleteMapping("/{stationId}")
public ResponseEntity deleteLine(@PathVariable Long lineId, @PathVariable Long stationId) {
public ResponseEntity<LineStationResponse> deleteLine(@PathVariable Long lineId, @PathVariable Long stationId) {
lineService.removeLineStation(lineId, stationId);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,7 +20,7 @@ public StationController(StationRepository stationRepository) {
}

@PostMapping
public ResponseEntity createStation(@RequestBody Request<StationCreateRequest> view) {
public ResponseEntity<StationResponse> createStation(@RequestBody Request<StationCreateRequest> view) {
Station station = view.getContent().toStation();
Station persistStation = stationRepository.save(station);

Expand All @@ -31,12 +30,12 @@ public ResponseEntity createStation(@RequestBody Request<StationCreateRequest> v
}

@GetMapping
public ResponseEntity showStations() {
public ResponseEntity<Iterable<Station>> showStations() {
return ResponseEntity.ok().body(stationRepository.findAll());
}

@DeleteMapping("/{id}")
public ResponseEntity deleteStation(@PathVariable Long id) {
public ResponseEntity<StationResponse> deleteStation(@PathVariable Long id) {
stationRepository.deleteById(id);
return ResponseEntity.noContent().build();
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/wooteco/subway/admin/domain/LineStation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/wooteco/subway/admin/dto/LineResponse.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,31 +27,26 @@ public LineResponse() {
}

public LineResponse(Long id, String name, LocalTime startTime, LocalTime endTime, int intervalTime,
LocalDateTime createdAt, LocalDateTime updatedAt, Set<Station> stations) {
LocalDateTime createdAt, LocalDateTime updatedAt, String bgColor, Set<Station> stations) {
this.id = id;
this.name = name;
this.startTime = startTime;
this.endTime = 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<Station> stations) {
return new LineResponse(line.getId(), line.getName(), line.getStartTime(), line.getEndTime(),
line.getIntervalTime(), line.getCreatedAt(), line.getUpdatedAt(), stations);
}

public static List<LineResponse> listOf(List<Line> lines) {
return lines.stream()
.map(it -> LineResponse.of(it))
.collect(Collectors.toList());
line.getIntervalTime(), line.getCreatedAt(), line.getUpdatedAt(), line.getBgColor(), stations);
}

public Long getId() {
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/wooteco/subway/admin/service/LineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
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;
import java.util.stream.Collectors;

@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;
Expand All @@ -30,8 +31,16 @@ public Line save(Line line) {
return lineRepository.save(line);
}

public List<Line> showLines() {
return lineRepository.findAll();
public List<LineResponse> showLines() {
List<LineResponse> lineResponses = new ArrayList<>();
List<Line> lines = lineRepository.findAll();
for (Line line : lines) {
List<Long> lineStationsId = line.findLineStationsId();
Set<Station> stations = stationRepository.findAllById(lineStationsId);
lineResponses.add(LineResponse.of(line, stations));
}

return Collections.unmodifiableList(lineResponses);
}

public void updateLine(Long id, Line line) {
Expand Down
44 changes: 44 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -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);
37 changes: 1 addition & 36 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
);
Loading

0 comments on commit 5c2393b

Please sign in to comment.