|
@@ -1,21 +1,27 @@
|
|
|
package eu.fibane.parkingtoll.api;
|
|
package eu.fibane.parkingtoll.api;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import eu.fibane.parkingtoll.core.PersistenceManager;
|
|
|
import eu.fibane.parkingtoll.model.CarSlot;
|
|
import eu.fibane.parkingtoll.model.CarSlot;
|
|
|
import eu.fibane.parkingtoll.model.ParkingLot;
|
|
import eu.fibane.parkingtoll.model.ParkingLot;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.validation.Valid;
|
|
import javax.validation.Valid;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
+@RestController
|
|
|
public class ParkingLotApiController implements ParkingLotApi {
|
|
public class ParkingLotApiController implements ParkingLotApi {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ParkingLotApiController.class);
|
|
private static final Logger log = LoggerFactory.getLogger(ParkingLotApiController.class);
|
|
@@ -24,37 +30,37 @@ public class ParkingLotApiController implements ParkingLotApi {
|
|
|
|
|
|
|
|
private final HttpServletRequest request;
|
|
private final HttpServletRequest request;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private PersistenceManager persistenceManager;
|
|
|
|
|
+
|
|
|
@org.springframework.beans.factory.annotation.Autowired
|
|
@org.springframework.beans.factory.annotation.Autowired
|
|
|
public ParkingLotApiController(ObjectMapper objectMapper, HttpServletRequest request) {
|
|
public ParkingLotApiController(ObjectMapper objectMapper, HttpServletRequest request) {
|
|
|
this.objectMapper = objectMapper;
|
|
this.objectMapper = objectMapper;
|
|
|
this.request = request;
|
|
this.request = request;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ResponseEntity<Void> addParkingLot(@Valid @RequestBody ParkingLot parkingLotItem) {
|
|
|
|
|
- String accept = request.getHeader("Accept");
|
|
|
|
|
- return new ResponseEntity<Void>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
|
|
+ public ResponseEntity<ParkingLot> addParkingLot(@Valid @RequestBody ParkingLot parkingLotItem) {
|
|
|
|
|
+ persistenceManager.addParkingLot(parkingLotItem);
|
|
|
|
|
+ return ResponseEntity.created(URI.create("/parking_lot/" + parkingLotItem.getId())).body(parkingLotItem);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ResponseEntity<Void> parkingLotParkingLotIdDelete(@PathVariable("parkingLotId") Long parkingLotId) {
|
|
|
|
|
- String accept = request.getHeader("Accept");
|
|
|
|
|
- return new ResponseEntity<Void>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
|
|
+ public ResponseEntity<ParkingLot> parkingLotDeleteById(@PathVariable("parkingLotId") Long parkingLotId) {
|
|
|
|
|
+ ParkingLot parkingLot = persistenceManager.deleteParkingLotById(parkingLotId);
|
|
|
|
|
+ if(parkingLot == null){
|
|
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
|
|
+ }
|
|
|
|
|
+ return ResponseEntity.ok(parkingLot);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ResponseEntity<ParkingLot> parkingLotParkingLotIdGet(@PathVariable("parkingLotId") Long parkingLotId) {
|
|
|
|
|
- String accept = request.getHeader("Accept");
|
|
|
|
|
- if (accept != null && accept.contains("application/json")) {
|
|
|
|
|
- try {
|
|
|
|
|
- return new ResponseEntity<ParkingLot>(objectMapper.readValue("{ \"layout\" : [ { \"name\" : \"name\", \"available\" : 0 }, { \"name\" : \"name\", \"available\" : 0 } ], \"pricing_policy\" : { \"per_hour_fare\" : 2.1, \"flat_fee\" : 1.1 }, \"name\" : \"Victoria 1 Parking Lot\", \"id\" : 5}", ParkingLot.class), HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("Couldn't serialize response for content type application/json", e);
|
|
|
|
|
- return new ResponseEntity<ParkingLot>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public ResponseEntity<ParkingLot> parkingLotGetById(@PathVariable("parkingLotId") Long parkingLotId) {
|
|
|
|
|
+ ParkingLot parkingLot = persistenceManager.getParkingLotById(parkingLotId);
|
|
|
|
|
+ if(parkingLot == null){
|
|
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return new ResponseEntity<ParkingLot>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
|
|
+ return ResponseEntity.ok(parkingLot);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ResponseEntity<CarSlot> parkingLotParkingLotIdParkDelete(@PathVariable("parkingLotId") Long parkingLotId, @Valid @RequestBody CarSlot carSlotItem) {
|
|
|
|
|
|
|
+ public ResponseEntity<CarSlot> leaveParkingLot(@PathVariable("parkingLotId") Long parkingLotId, @Valid @RequestBody CarSlot carSlotItem) {
|
|
|
String accept = request.getHeader("Accept");
|
|
String accept = request.getHeader("Accept");
|
|
|
if (accept != null && accept.contains("application/json")) {
|
|
if (accept != null && accept.contains("application/json")) {
|
|
|
try {
|
|
try {
|
|
@@ -68,7 +74,7 @@ public class ParkingLotApiController implements ParkingLotApi {
|
|
|
return new ResponseEntity<CarSlot>(HttpStatus.NOT_IMPLEMENTED);
|
|
return new ResponseEntity<CarSlot>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ResponseEntity<CarSlot> parkingLotParkingLotIdParkPost(@PathVariable("parkingLotId") Long parkingLotId, @Valid @RequestBody CarSlot carSlotItem) {
|
|
|
|
|
|
|
+ public ResponseEntity<CarSlot> parkParkingLot(@PathVariable("parkingLotId") Long parkingLotId, @Valid @RequestBody CarSlot carSlotItem) {
|
|
|
String accept = request.getHeader("Accept");
|
|
String accept = request.getHeader("Accept");
|
|
|
if (accept != null && accept.contains("application/json")) {
|
|
if (accept != null && accept.contains("application/json")) {
|
|
|
try {
|
|
try {
|
|
@@ -82,32 +88,17 @@ public class ParkingLotApiController implements ParkingLotApi {
|
|
|
return new ResponseEntity<CarSlot>(HttpStatus.NOT_IMPLEMENTED);
|
|
return new ResponseEntity<CarSlot>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ResponseEntity<ParkingLot> parkingLotPut(@Valid @RequestBody ParkingLot parkingLotItem) {
|
|
|
|
|
- String accept = request.getHeader("Accept");
|
|
|
|
|
- if (accept != null && accept.contains("application/json")) {
|
|
|
|
|
- try {
|
|
|
|
|
- return new ResponseEntity<ParkingLot>(objectMapper.readValue("{ \"layout\" : [ { \"name\" : \"name\", \"available\" : 0 }, { \"name\" : \"name\", \"available\" : 0 } ], \"pricing_policy\" : { \"per_hour_fare\" : 2.1, \"flat_fee\" : 1.1 }, \"name\" : \"Victoria 1 Parking Lot\", \"id\" : 5}", ParkingLot.class), HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("Couldn't serialize response for content type application/json", e);
|
|
|
|
|
- return new ResponseEntity<ParkingLot>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return new ResponseEntity<ParkingLot>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
|
|
+ public ResponseEntity<ParkingLot> updateParkingLot(@PathVariable("parkingLotId") Long parkingLotId, @Valid @RequestBody ParkingLot parkingLotItem) {
|
|
|
|
|
+ ParkingLot parkingLot = persistenceManager.updateParkingLot(parkingLotId, parkingLotItem);
|
|
|
|
|
+ return ResponseEntity.ok(parkingLot);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ResponseEntity<List<ParkingLot>> searchParkingLot(@Valid @RequestParam(value = "searchString", required = false) String searchString) {
|
|
|
|
|
- String accept = request.getHeader("Accept");
|
|
|
|
|
- if (accept != null && accept.contains("application/json")) {
|
|
|
|
|
- try {
|
|
|
|
|
- return new ResponseEntity<List<ParkingLot>>(objectMapper.readValue("[ { \"layout\" : [ { \"name\" : \"name\", \"available\" : 0 }, { \"name\" : \"name\", \"available\" : 0 } ], \"pricing_policy\" : { \"per_hour_fare\" : 2.1, \"flat_fee\" : 1.1 }, \"name\" : \"Victoria 1 Parking Lot\", \"id\" : 5}, { \"layout\" : [ { \"name\" : \"name\", \"available\" : 0 }, { \"name\" : \"name\", \"available\" : 0 } ], \"pricing_policy\" : { \"per_hour_fare\" : 2.1, \"flat_fee\" : 1.1 }, \"name\" : \"Victoria 1 Parking Lot\", \"id\" : 5} ]", List.class), HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("Couldn't serialize response for content type application/json", e);
|
|
|
|
|
- return new ResponseEntity<List<ParkingLot>>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public ResponseEntity<Collection<ParkingLot>> searchParkingLot(@Valid @RequestParam(value = "searchString", required = false) String searchString) {
|
|
|
|
|
+ Collection<ParkingLot> parkingLots = persistenceManager.getAllParkingLots();
|
|
|
|
|
+ if(searchString != null && !searchString.isBlank()) {
|
|
|
|
|
+ parkingLots = parkingLots.stream().filter(a -> a.getName().toUpperCase().contains(searchString.toUpperCase())).collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return new ResponseEntity<List<ParkingLot>>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
|
|
+ return ResponseEntity.ok(parkingLots);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|