-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShipController.java
More file actions
107 lines (93 loc) · 5.83 KB
/
ShipController.java
File metadata and controls
107 lines (93 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.space.controller;
import com.space.model.Ship;
import com.space.model.ShipType;
import com.space.service.ShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class ShipController {
private ShipService shipService;
@Autowired
public ShipController(ShipService shipService) {
this.shipService = shipService;
}
@GetMapping("/rest/ships")
public ResponseEntity<List<Ship>> findAll(
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "planet", required = false) String planet,
@RequestParam(value = "shipType", required = false) ShipType shipType,
@RequestParam(value = "after", required = false) Long after,
@RequestParam(value = "before", required = false) Long before,
@RequestParam(value = "isUsed", required = false) Boolean isUsed,
@RequestParam(value = "minSpeed", required = false) Double minSpeed,
@RequestParam(value = "maxSpeed", required = false) Double maxSpeed,
@RequestParam(value = "minCrewSize", required = false) Integer minCrewSize,
@RequestParam(value = "maxCrewSize", required = false) Integer maxCrewSize,
@RequestParam(value = "minRating", required = false) Double minRating,
@RequestParam(value = "maxRating", required = false) Double maxRating,
@RequestParam(value = "order", required = false, defaultValue = "ID") ShipOrder order,
@RequestParam(value = "pageNumber", required = false, defaultValue = "0") Integer pageNumber,
@RequestParam(value = "pageSize", required = false, defaultValue = "3") Integer pageSize) {
Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(order.getFieldName()));
Specification<Ship> specification = Specification.where(shipService.nameFilter(name)
.and(shipService.planetFilter(planet))
.and(shipService.shipTypeFilter(shipType))
.and(shipService.prodDateFilter(after, before))
.and(shipService.isUseFilter(isUsed))
.and(shipService.speedFilter(minSpeed, maxSpeed))
.and(shipService.crewSizeFilter(minCrewSize, maxCrewSize))
.and(shipService.ratingFilter(minRating, maxRating)));
return new ResponseEntity<>(shipService.getShipsList(specification, pageable).getContent(), HttpStatus.OK);
}
@GetMapping("/rest/ships/count")
public ResponseEntity<Integer> getCount(@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "planet", required = false) String planet,
@RequestParam(value = "shipType", required = false) ShipType shipType,
@RequestParam(value = "after", required = false) Long after,
@RequestParam(value = "before", required = false) Long before,
@RequestParam(value = "isUsed", required = false) Boolean isUsed,
@RequestParam(value = "minSpeed", required = false) Double minSpeed,
@RequestParam(value = "maxSpeed", required = false) Double maxSpeed,
@RequestParam(value = "minCrewSize", required = false) Integer minCrewSize,
@RequestParam(value = "maxCrewSize", required = false) Integer maxCrewSize,
@RequestParam(value = "minRating", required = false) Double minRating,
@RequestParam(value = "maxRating", required = false) Double maxRating) {
Specification<Ship> specification = Specification.where(shipService.nameFilter(name)
.and(shipService.planetFilter(planet))
.and(shipService.shipTypeFilter(shipType))
.and(shipService.prodDateFilter(after, before))
.and(shipService.isUseFilter(isUsed))
.and(shipService.speedFilter(minSpeed, maxSpeed))
.and(shipService.crewSizeFilter(minCrewSize, maxCrewSize))
.and(shipService.ratingFilter(minRating, maxRating)));
return new ResponseEntity<>(shipService.getShipsCount(specification), HttpStatus.OK);
}
@PostMapping("/rest/ships")
public ResponseEntity<Ship> createShip(@RequestBody Ship ship) {
Ship resultShip = shipService.createShip(ship);
return new ResponseEntity<>(resultShip, HttpStatus.OK);
}
@GetMapping("/rest/ships/{id}")
public ResponseEntity<Ship> getShipById(@PathVariable String id) {
Ship resultShip = shipService.getShip(shipService.idToLongWithChecking(id));
return new ResponseEntity<>(resultShip, HttpStatus.OK);
}
@PostMapping("/rest/ships/{id}")
public ResponseEntity<Ship> updateShip(@PathVariable String id,
@RequestBody Ship ship) {
Ship resultShip = this.shipService.updateShip(shipService.idToLongWithChecking(id), ship);
return new ResponseEntity<>(resultShip, HttpStatus.OK);
}
@DeleteMapping("/rest/ships/{id}")
public ResponseEntity<?> deleteShip(@PathVariable String id) {
shipService.deleteShip(shipService.idToLongWithChecking(id));
return new ResponseEntity<>(HttpStatus.OK);
}
}