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

[디디] 지하철 경로 조회 - TDD 미션 제출합니다. #29

Merged
merged 19 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing;

@SpringBootApplication
public class SubwayAdminApplication {
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/wooteco/subway/admin/config/ETagHeaderFilter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package wooteco.subway.admin.config;

// TODO: ETag 관련 설정하기
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

@Configuration
public class ETagHeaderFilter {

@Bean
public FilterRegistrationBean<ShallowEtagHeaderFilter>
shallowEtagHeaderFilter() {
FilterRegistrationBean<ShallowEtagHeaderFilter> filter
= new FilterRegistrationBean<>(new ShallowEtagHeaderFilter());
filter.addUrlPatterns("/lines/detail");
filter.setName("etagFilter");
return filter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package wooteco.subway.admin.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing;

@Configuration
@EnableJdbcAuditing
public class JdbcAuditConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package wooteco.subway.admin.controller;

import java.util.stream.Collectors;

import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import wooteco.subway.admin.dto.ErrorResponse;
import wooteco.subway.admin.exception.BusinessException;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> validExceptionHandler(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
String errorMessage = bindingResult.getAllErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining("\n"));
return ResponseEntity.badRequest().body(new ErrorResponse(errorMessage));
}

@ExceptionHandler(value = BusinessException.class)
public ResponseEntity<ErrorResponse> bindingErrorHandler(BusinessException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(value = {DataAccessException.class})
public ResponseEntity<ErrorResponse> bindingErrorHandler(DataAccessException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(value = Exception.class)
public ResponseEntity<ErrorResponse> defaultExceptionHandler(Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(e.getMessage()));
}
}
82 changes: 55 additions & 27 deletions src/main/java/wooteco/subway/admin/controller/LineController.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,93 @@
package wooteco.subway.admin.controller;

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

import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import wooteco.subway.admin.domain.Line;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import wooteco.subway.admin.dto.LineDetailResponse;
import wooteco.subway.admin.dto.LineRequest;
import wooteco.subway.admin.dto.LineResponse;
import wooteco.subway.admin.dto.LineStationCreateRequest;
import wooteco.subway.admin.dto.WholeSubwayResponse;
import wooteco.subway.admin.service.LineService;

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

@RestController
@RequestMapping("/lines")
public class LineController {
private LineService lineService;
private final LineService lineService;

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

@PostMapping(value = "/lines")
public ResponseEntity<LineResponse> createLine(@RequestBody LineRequest view) {
Line persistLine = lineService.save(view.toLine());
@PostMapping
public ResponseEntity<LineResponse> create(@Valid @RequestBody LineRequest request) {
LineResponse lineResponse = lineService.save(request.toLine());

return ResponseEntity
.created(URI.create("/lines/" + persistLine.getId()))
.body(LineResponse.of(persistLine));
.created(URI.create("/lines/" + lineResponse.getId()))
.body(lineResponse);
}

@GetMapping("/lines")
public ResponseEntity<List<LineResponse>> showLine() {
return ResponseEntity.ok().body(LineResponse.listOf(lineService.showLines()));
@GetMapping
public ResponseEntity<List<LineResponse>> retrieveLines() {
return ResponseEntity.ok().body(lineService.findLines());
}

@GetMapping("/lines/{id}")
public ResponseEntity<LineDetailResponse> retrieveLine(@PathVariable Long id) {
return ResponseEntity.ok().body(lineService.findLineWithStationsById(id));
@GetMapping("/{id}")
public ResponseEntity<LineResponse> retrieveLine(@PathVariable Long id) {
return ResponseEntity.ok().body(lineService.findLine(id));
}

@PutMapping("/lines/{id}")
public ResponseEntity updateLine(@PathVariable Long id, @RequestBody LineRequest view) {
lineService.updateLine(id, view);
@PutMapping("/{id}")
public ResponseEntity<Void> update(@PathVariable Long id,
@RequestBody LineRequest request) {
lineService.updateLine(id, request);
return ResponseEntity.ok().build();
}

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

@PostMapping("/lines/{lineId}/stations")
public ResponseEntity addLineStation(@PathVariable Long lineId, @RequestBody LineStationCreateRequest view) {
lineService.addLineStation(lineId, view);
@PostMapping("/{lineId}/stations")
public ResponseEntity<Void> addLineStation(@PathVariable Long lineId,
@Valid @RequestBody LineStationCreateRequest request) {
lineService.addLineStation(lineId, request);
return ResponseEntity.ok().build();
}

@DeleteMapping("/lines/{lineId}/stations/{stationId}")
public ResponseEntity removeLineStation(@PathVariable Long lineId, @PathVariable Long stationId) {
@DeleteMapping("/{lineId}/stations/{stationId}")
public ResponseEntity<Void> removeLineStation(@PathVariable Long lineId,
@PathVariable Long stationId) {
lineService.removeLineStation(lineId, stationId);
return ResponseEntity.noContent().build();
}

@GetMapping("/detail")
public ResponseEntity<WholeSubwayResponse> retrieveDetailLines() {
WholeSubwayResponse response = lineService.findDetailLines();
return ResponseEntity.ok().body(response);
}

@GetMapping("/detail/{id}")
public ResponseEntity<LineDetailResponse> retrieveDetailLine(@PathVariable Long id) {
LineDetailResponse response = lineService.findDetailLine(id);

return ResponseEntity.status(HttpStatus.OK).body(response);
}
}
46 changes: 28 additions & 18 deletions src/main/java/wooteco/subway/admin/controller/PageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,50 @@

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import wooteco.subway.admin.repository.StationRepository;
import wooteco.subway.admin.service.LineService;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public class PageController {
private LineService lineService;
private StationRepository stationRepository;

public PageController(LineService lineService, StationRepository stationRepository) {
this.lineService = lineService;
this.stationRepository = stationRepository;
@GetMapping
public String index() {
return "index";
}

@GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
public String index() {
@GetMapping(value = "/service")
public String serviceIndex() {
return "service/index";
}

@GetMapping(value = "/service/map")
public String map() {
return "service/map";
}

@GetMapping(value = "/service/search")
public String search() {
return "service/search";
}

@GetMapping(value = "/admin")
public String adminIndex() {
return "admin/index";
}

@GetMapping(value = "/stations", produces = MediaType.TEXT_HTML_VALUE)
public String stationPage(Model model) {
model.addAttribute("stations", stationRepository.findAll());
@GetMapping(value = "/admin/station")
public String stationPage() {
return "admin/admin-station";
}

@GetMapping(value = "/lines", produces = MediaType.TEXT_HTML_VALUE)
public String linePage(Model model) {
model.addAttribute("lines", lineService.showLines());
@GetMapping(value = "/admin/line")
public String linePage() {
return "admin/admin-line";
}

@GetMapping(value = "/edges", produces = MediaType.TEXT_HTML_VALUE)
public String edgePage(Model model) {
@GetMapping(value = "/admin/edge")
public String edgePage() {
return "admin/admin-edge";
}
}
30 changes: 30 additions & 0 deletions src/main/java/wooteco/subway/admin/controller/PathController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package wooteco.subway.admin.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import wooteco.subway.admin.dto.PathRequest;
import wooteco.subway.admin.dto.PathResponse;
import wooteco.subway.admin.service.PathService;

@RestController
@RequestMapping("/path")
public class PathController {

private final PathService pathService;

public PathController(PathService pathService) {
this.pathService = pathService;
}

@GetMapping
ResponseEntity<PathResponse> findPath(@RequestParam String sourceName,
@RequestParam String targetName, @RequestParam String type) {
PathResponse response = pathService.findPath(new PathRequest(sourceName, targetName, type));

return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
package wooteco.subway.admin.controller;

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

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import wooteco.subway.admin.domain.Station;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import wooteco.subway.admin.dto.StationCreateRequest;
import wooteco.subway.admin.dto.StationResponse;
import wooteco.subway.admin.repository.StationRepository;

import java.net.URI;
import java.util.List;
import wooteco.subway.admin.service.StationService;

@RestController
@RequestMapping("/stations")
public class StationController {
private final StationRepository stationRepository;
private final StationService stationService;

public StationController(StationRepository stationRepository) {
this.stationRepository = stationRepository;
public StationController(StationService stationService) {
this.stationService = stationService;
}

@PostMapping("/stations")
public ResponseEntity<StationResponse> createStation(@RequestBody StationCreateRequest view) {
Station station = view.toStation();
Station persistStation = stationRepository.save(station);
@PostMapping
public ResponseEntity<StationResponse> create(
@Valid @RequestBody StationCreateRequest request) {
StationResponse response = stationService.save(request);

return ResponseEntity
.created(URI.create("/stations/" + persistStation.getId()))
.body(StationResponse.of(persistStation));
.created(URI.create("/stations/" + response.getId()))
.body(response);
}

@GetMapping("/stations")
public ResponseEntity<List<StationResponse>> showStations() {
return ResponseEntity.ok().body(StationResponse.listOf(stationRepository.findAll()));
@GetMapping
public ResponseEntity<List<StationResponse>> get() {
List<StationResponse> responses = stationService.findAll();

return ResponseEntity.ok(responses);
}

@DeleteMapping("/stations/{id}")
public ResponseEntity deleteStation(@PathVariable Long id) {
stationRepository.deleteById(id);
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
stationService.deleteById(id);

return ResponseEntity.noContent().build();
}
}
Loading