|
@@ -16,7 +16,7 @@ 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.math.BigDecimal;
|
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -27,19 +27,18 @@ public class ParkingLotApiController implements ParkingLotApi {
|
|
|
private static final Logger log = LoggerFactory.getLogger(ParkingLotApiController.class);
|
|
private static final Logger log = LoggerFactory.getLogger(ParkingLotApiController.class);
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
private final ObjectMapper objectMapper;
|
|
|
-
|
|
|
|
|
private final HttpServletRequest request;
|
|
private final HttpServletRequest request;
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
private PersistenceManager persistenceManager;
|
|
private PersistenceManager persistenceManager;
|
|
|
|
|
|
|
|
- @org.springframework.beans.factory.annotation.Autowired
|
|
|
|
|
- public ParkingLotApiController(ObjectMapper objectMapper, HttpServletRequest request) {
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public ParkingLotApiController(ObjectMapper objectMapper, HttpServletRequest request, PersistenceManager persistenceManager) {
|
|
|
this.objectMapper = objectMapper;
|
|
this.objectMapper = objectMapper;
|
|
|
this.request = request;
|
|
this.request = request;
|
|
|
|
|
+ this.persistenceManager = persistenceManager;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ResponseEntity<ParkingLot> addParkingLot(@Valid @RequestBody ParkingLot parkingLotItem) {
|
|
public ResponseEntity<ParkingLot> addParkingLot(@Valid @RequestBody ParkingLot parkingLotItem) {
|
|
|
|
|
+
|
|
|
persistenceManager.addParkingLot(parkingLotItem);
|
|
persistenceManager.addParkingLot(parkingLotItem);
|
|
|
return ResponseEntity.created(URI.create("/parking_lot/" + parkingLotItem.getId())).body(parkingLotItem);
|
|
return ResponseEntity.created(URI.create("/parking_lot/" + parkingLotItem.getId())).body(parkingLotItem);
|
|
|
}
|
|
}
|
|
@@ -61,31 +60,21 @@ public class ParkingLotApiController implements ParkingLotApi {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ResponseEntity<CarSlot> leaveParkingLot(@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");
|
|
|
|
|
- if (accept != null && accept.contains("application/json")) {
|
|
|
|
|
- try {
|
|
|
|
|
- return new ResponseEntity<CarSlot>(objectMapper.readValue("{ \"arrival_time\" : \"2000-01-23T04:56:07.000+00:00\", \"slot\" : 15, \"type\" : \"25kW charging point\", \"parking_lot_id\" : 0}", CarSlot.class), HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("Couldn't serialize response for content type application/json", e);
|
|
|
|
|
- return new ResponseEntity<CarSlot>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- return new ResponseEntity<CarSlot>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
|
|
+ ParkingLot parkingLot = persistenceManager.getParkingLotById(parkingLotId);
|
|
|
|
|
+ CarSlot oldCarSlot = parkingLot.removeCar(carSlotItem);
|
|
|
|
|
+ BigDecimal fare = parkingLot.getPricingPolicy().computeFare(oldCarSlot.getArrivalTime(), oldCarSlot.getDepartureTime());
|
|
|
|
|
+ oldCarSlot.setPrice(fare);
|
|
|
|
|
+ return ResponseEntity.ok(oldCarSlot);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ResponseEntity<CarSlot> parkParkingLot(@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");
|
|
|
|
|
- if (accept != null && accept.contains("application/json")) {
|
|
|
|
|
- try {
|
|
|
|
|
- return new ResponseEntity<CarSlot>(objectMapper.readValue("{ \"arrival_time\" : \"2000-01-23T04:56:07.000+00:00\", \"slot\" : 15, \"type\" : \"25kW charging point\", \"parking_lot_id\" : 0}", CarSlot.class), HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("Couldn't serialize response for content type application/json", e);
|
|
|
|
|
- return new ResponseEntity<CarSlot>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //check if the asked parking exists
|
|
|
|
|
+ ParkingLot parkingLot = persistenceManager.getParkingLotById(parkingLotId);
|
|
|
|
|
+ carSlotItem.setParkingLotId(parkingLotId);
|
|
|
|
|
+ parkingLot.parkCar(carSlotItem);
|
|
|
|
|
|
|
|
- return new ResponseEntity<CarSlot>(HttpStatus.NOT_IMPLEMENTED);
|
|
|
|
|
|
|
+ return ResponseEntity.ok(carSlotItem);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ResponseEntity<ParkingLot> updateParkingLot(@PathVariable("parkingLotId") Long parkingLotId, @Valid @RequestBody ParkingLot parkingLotItem) {
|
|
public ResponseEntity<ParkingLot> updateParkingLot(@PathVariable("parkingLotId") Long parkingLotId, @Valid @RequestBody ParkingLot parkingLotItem) {
|