|
1 | 1 | package train.controller; |
2 | 2 |
|
| 3 | +import edu.fudan.common.client.dto.train.TrainTypeDto; |
3 | 4 | import edu.fudan.common.util.Response; |
4 | | -import org.slf4j.Logger; |
5 | | -import org.slf4j.LoggerFactory; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
6 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
7 | 7 | import org.springframework.http.HttpEntity; |
8 | 8 | import org.springframework.http.HttpHeaders; |
9 | | -import org.springframework.web.bind.annotation.*; |
| 9 | +import org.springframework.web.bind.annotation.GetMapping; |
| 10 | +import org.springframework.web.bind.annotation.PathVariable; |
| 11 | +import org.springframework.web.bind.annotation.PostMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestBody; |
| 13 | +import org.springframework.web.bind.annotation.RequestHeader; |
| 14 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 15 | +import org.springframework.web.bind.annotation.RestController; |
10 | 16 | import train.entity.TrainType; |
| 17 | +import train.mapper.TrainTypeMapper; |
11 | 18 | import train.service.TrainService; |
12 | 19 |
|
13 | 20 | import java.util.List; |
14 | 21 |
|
15 | 22 | import static org.springframework.http.ResponseEntity.ok; |
16 | 23 |
|
17 | | - |
| 24 | +@Slf4j |
18 | 25 | @RestController |
19 | 26 | @RequestMapping("/api/v1/train") |
20 | 27 | public class TrainController { |
21 | 28 |
|
22 | | - |
23 | 29 | @Autowired |
24 | 30 | private TrainService trainService; |
25 | 31 |
|
26 | | - private static final Logger LOGGER = LoggerFactory.getLogger(TrainController.class); |
| 32 | + @Autowired |
| 33 | + private TrainTypeMapper trainTypeMapper; |
27 | 34 |
|
28 | | - @GetMapping(path = "/trains/welcome") |
| 35 | + @GetMapping(path = "/welcome") |
29 | 36 | public String home(@RequestHeader HttpHeaders headers) { |
30 | 37 | return "Welcome to [ Train Service ] !"; |
31 | 38 | } |
32 | 39 |
|
33 | | - @CrossOrigin(origins = "*") |
34 | | - @PostMapping(value = "/trains") |
35 | | - public HttpEntity create(@RequestBody TrainType trainType, @RequestHeader HttpHeaders headers) { |
36 | | - TrainController.LOGGER.info("[create][Create train][TrainTypeId: {}]",trainType.getId()); |
37 | | - boolean isCreateSuccess = trainService.create(trainType, headers); |
38 | | - if (isCreateSuccess) { |
39 | | - return ok(new Response(1, "create success", null)); |
40 | | - } else { |
41 | | - return ok(new Response(0, "train type already exist", trainType)); |
42 | | - } |
| 40 | + @GetMapping(value = "/trainTypes") |
| 41 | + public HttpEntity<Response<List<TrainTypeDto>>> query(@RequestHeader HttpHeaders headers) { |
| 42 | + log.info("[query][Query train]"); |
| 43 | + return ok(toListResponse(trainService.query(headers), "no content")); |
43 | 44 | } |
44 | 45 |
|
45 | | - @CrossOrigin(origins = "*") |
46 | | - @GetMapping(value = "/trains/{id}") |
47 | | - public HttpEntity retrieve(@PathVariable String id, @RequestHeader HttpHeaders headers) { |
48 | | - TrainController.LOGGER.info("[retrieve][Retrieve train][TrainTypeId: {}]",id); |
49 | | - TrainType trainType = trainService.retrieve(id, headers); |
50 | | - if (trainType == null) { |
51 | | - return ok(new Response(0, "here is no TrainType with the trainType id: " + id, null)); |
52 | | - } else { |
53 | | - return ok(new Response(1, "success", trainType)); |
54 | | - } |
| 46 | + @GetMapping(value = "/trainTypes/{id}") |
| 47 | + public HttpEntity<Response<TrainTypeDto>> retrieve(@PathVariable String id, @RequestHeader HttpHeaders headers) { |
| 48 | + log.info("[retrieve][Retrieve train][TrainTypeId: {}]", id); |
| 49 | + String msg = "here is no TrainType with the trainType id: " + id; |
| 50 | + return ok(toSingleResponse(trainService.retrieve(id, headers), msg)); |
55 | 51 | } |
56 | 52 |
|
57 | | - @CrossOrigin(origins = "*") |
58 | | - @GetMapping(value = "/trains/byName/{name}") |
59 | | - public HttpEntity retrieveByName(@PathVariable String name, @RequestHeader HttpHeaders headers) { |
60 | | - TrainController.LOGGER.info("[retrieveByName][Retrieve train][TrainTypeName: {}]", name); |
61 | | - TrainType trainType = trainService.retrieveByName(name, headers); |
62 | | - if (trainType == null) { |
63 | | - return ok(new Response(0, "here is no TrainType with the trainType name: " + name, null)); |
64 | | - } else { |
65 | | - return ok(new Response(1, "success", trainType)); |
66 | | - } |
| 53 | + @GetMapping(value = "/trainTypes/byName/{name}") |
| 54 | + public HttpEntity<Response<TrainTypeDto>> retrieveByName(@PathVariable String name, @RequestHeader HttpHeaders headers) { |
| 55 | + log.info("[retrieveByName][Retrieve train][TrainTypeName: {}]", name); |
| 56 | + String msg = "here is no TrainType with the trainType name: " + name; |
| 57 | + return ok(toSingleResponse(trainService.retrieveByName(name, headers), msg)); |
67 | 58 | } |
68 | 59 |
|
69 | | - @CrossOrigin(origins = "*") |
70 | 60 | @PostMapping(value = "/trains/byNames") |
71 | | - public HttpEntity retrieveByName(@RequestBody List<String> names, @RequestHeader HttpHeaders headers) { |
72 | | - TrainController.LOGGER.info("[retrieveByNames][Retrieve train][TrainTypeNames: {}]", names); |
73 | | - List<TrainType> trainTypes = trainService.retrieveByNames(names, headers); |
74 | | - if (trainTypes == null) { |
75 | | - return ok(new Response(0, "here is no TrainTypes with the trainType names: " + names, null)); |
76 | | - } else { |
77 | | - return ok(new Response(1, "success", trainTypes)); |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - @CrossOrigin(origins = "*") |
82 | | - @PutMapping(value = "/trains") |
83 | | - public HttpEntity update(@RequestBody TrainType trainType, @RequestHeader HttpHeaders headers) { |
84 | | - TrainController.LOGGER.info("[update][Update train][TrainTypeId: {}]",trainType.getId()); |
85 | | - boolean isUpdateSuccess = trainService.update(trainType, headers); |
86 | | - if (isUpdateSuccess) { |
87 | | - return ok(new Response(1, "update success", isUpdateSuccess)); |
88 | | - } else { |
89 | | - return ok(new Response(0, "there is no trainType with the trainType id", isUpdateSuccess)); |
90 | | - } |
| 61 | + public HttpEntity<Response<List<TrainTypeDto>>> retrieveByName(@RequestBody List<String> names, @RequestHeader HttpHeaders headers) { |
| 62 | + log.info("[retrieveByNames][Retrieve train][TrainTypeNames: {}]", names); |
| 63 | + String msg = "here is no TrainTypes with the trainType names: " + names; |
| 64 | + return ok(toListResponse(trainService.retrieveByNames(names, headers), msg)); |
91 | 65 | } |
92 | 66 |
|
93 | | - @CrossOrigin(origins = "*") |
94 | | - @DeleteMapping(value = "/trains/{id}") |
95 | | - public HttpEntity delete(@PathVariable String id, @RequestHeader HttpHeaders headers) { |
96 | | - TrainController.LOGGER.info("[delete][Delete train][TrainTypeId: {}]",id); |
97 | | - boolean isDeleteSuccess = trainService.delete(id, headers); |
98 | | - if (isDeleteSuccess) { |
99 | | - return ok(new Response(1, "delete success", isDeleteSuccess)); |
100 | | - } else { |
101 | | - return ok(new Response(0, "there is no train according to id", null)); |
102 | | - } |
| 67 | + private Response<List<TrainTypeDto>> toListResponse(List<TrainType> entities, String emptyMsg) { |
| 68 | + List<TrainTypeDto> payload = entities == null ? null : trainTypeMapper.toDtoList(entities); |
| 69 | + boolean hasData = payload != null && !payload.isEmpty(); |
| 70 | + return new Response<>(hasData ? 1 : 0, hasData ? "success" : emptyMsg, payload); |
103 | 71 | } |
104 | 72 |
|
105 | | - @CrossOrigin(origins = "*") |
106 | | - @GetMapping(value = "/trains") |
107 | | - public HttpEntity query(@RequestHeader HttpHeaders headers) { |
108 | | - TrainController.LOGGER.info("[query][Query train]"); |
109 | | - List<TrainType> trainTypes = trainService.query(headers); |
110 | | - if (trainTypes != null && !trainTypes.isEmpty()) { |
111 | | - return ok(new Response(1, "success", trainTypes)); |
112 | | - } else { |
113 | | - return ok(new Response(0, "no content", trainTypes)); |
114 | | - } |
| 73 | + private Response<TrainTypeDto> toSingleResponse(TrainType entity, String notFoundMsg) { |
| 74 | + TrainTypeDto payload = entity == null ? null : trainTypeMapper.toDto(entity); |
| 75 | + boolean hasData = payload != null; |
| 76 | + return new Response<>(hasData ? 1 : 0, hasData ? "success" : notFoundMsg, payload); |
115 | 77 | } |
116 | 78 | } |
0 commit comments