fixes for 2nd practical
This commit is contained in:
@@ -9,6 +9,9 @@ import org.springframework.http.HttpMethod;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import tools.jackson.databind.ObjectMapper;
|
import tools.jackson.databind.ObjectMapper;
|
||||||
import tools.jackson.dataformat.xml.XmlMapper;
|
import tools.jackson.dataformat.xml.XmlMapper;
|
||||||
|
import ua.com.dxrkness.dto.FreightRequest;
|
||||||
|
import ua.com.dxrkness.dto.RouteRequest;
|
||||||
|
import ua.com.dxrkness.dto.VehicleRequest;
|
||||||
import ua.com.dxrkness.model.Freight;
|
import ua.com.dxrkness.model.Freight;
|
||||||
import ua.com.dxrkness.model.Route;
|
import ua.com.dxrkness.model.Route;
|
||||||
import ua.com.dxrkness.model.Vehicle;
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
@@ -41,7 +44,7 @@ public class ApplicationClient {
|
|||||||
|
|
||||||
execute(client, new HttpGet(BASE_URL + "/vehicles"), "GET all vehicles");
|
execute(client, new HttpGet(BASE_URL + "/vehicles"), "GET all vehicles");
|
||||||
|
|
||||||
Vehicle newVehicle = new Vehicle(0, "Mercedes", "Actros", "AA1234BB", 2023, 18000, Vehicle.Status.AVAILABLE);
|
var newVehicle = new VehicleRequest("Mercedes", "Actros", "AA1234BB", 2023, 18000, Vehicle.Status.AVAILABLE);
|
||||||
String vehicleXml = xmlMapper.writeValueAsString(newVehicle);
|
String vehicleXml = xmlMapper.writeValueAsString(newVehicle);
|
||||||
HttpPost post = new HttpPost(BASE_URL + "/vehicles");
|
HttpPost post = new HttpPost(BASE_URL + "/vehicles");
|
||||||
post.setEntity(new StringEntity(vehicleXml, "UTF-8"));
|
post.setEntity(new StringEntity(vehicleXml, "UTF-8"));
|
||||||
@@ -54,11 +57,11 @@ public class ApplicationClient {
|
|||||||
execute(client, get, "GET vehicle by ID=0");
|
execute(client, get, "GET vehicle by ID=0");
|
||||||
execute(client, new HttpGet(BASE_URL + "/vehicles/999"), "GET vehicle by ID=999 (not found)");
|
execute(client, new HttpGet(BASE_URL + "/vehicles/999"), "GET vehicle by ID=999 (not found)");
|
||||||
|
|
||||||
Vehicle updateVehicle = new Vehicle(1, "Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.MAINTENANCE);
|
var updateVehicle = new VehicleRequest("Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.MAINTENANCE);
|
||||||
var vehiclePut = createHttpRequest(updateVehicle, "/vehicles/0", HttpMethod.PUT);
|
var vehiclePut = createHttpRequest(updateVehicle, "/vehicles/0", HttpMethod.PUT);
|
||||||
execute(client, vehiclePut, "PUT update vehicle ID=0");
|
execute(client, vehiclePut, "PUT update vehicle ID=0");
|
||||||
|
|
||||||
Vehicle patchVehicle = new Vehicle(1, "Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.IN_TRANSIT);
|
var patchVehicle = new VehicleRequest("Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.IN_TRANSIT);
|
||||||
var vehiclePatch = createHttpRequest(patchVehicle, "/vehicles/0", HttpMethod.PATCH);
|
var vehiclePatch = createHttpRequest(patchVehicle, "/vehicles/0", HttpMethod.PATCH);
|
||||||
execute(client, vehiclePatch, "PATCH update vehicle ID=0");
|
execute(client, vehiclePatch, "PATCH update vehicle ID=0");
|
||||||
|
|
||||||
@@ -73,18 +76,18 @@ public class ApplicationClient {
|
|||||||
execute(client, new HttpGet(BASE_URL + "/freights"), "GET all freights");
|
execute(client, new HttpGet(BASE_URL + "/freights"), "GET all freights");
|
||||||
|
|
||||||
Freight.Dimensions dims = new Freight.Dimensions(120, 100, 200);
|
Freight.Dimensions dims = new Freight.Dimensions(120, 100, 200);
|
||||||
Freight newFreight = new Freight(0, "Electronics", "Laptops and monitors", 500, dims, Freight.Status.PENDING);
|
var newFreight = new FreightRequest("Electronics", "Laptops and monitors", 500, dims, Freight.Status.PENDING);
|
||||||
var freightPost = createHttpRequest(newFreight, "/freights", HttpMethod.POST);
|
var freightPost = createHttpRequest(newFreight, "/freights", HttpMethod.POST);
|
||||||
execute(client, freightPost, "POST new freight");
|
execute(client, freightPost, "POST new freight");
|
||||||
|
|
||||||
execute(client, new HttpGet(BASE_URL + "/freights/0"), "GET freight by ID=0");
|
execute(client, new HttpGet(BASE_URL + "/freights/0"), "GET freight by ID=0");
|
||||||
execute(client, new HttpGet(BASE_URL + "/freights/999"), "GET freight by ID=999 (not found)");
|
execute(client, new HttpGet(BASE_URL + "/freights/999"), "GET freight by ID=999 (not found)");
|
||||||
|
|
||||||
Freight updateFreight = new Freight(1, "Furniture", "Office desks", 800, dims, Freight.Status.IN_TRANSIT);
|
var updateFreight = new FreightRequest("Furniture", "Office desks", 800, dims, Freight.Status.IN_TRANSIT);
|
||||||
var freightPut = createHttpRequest(updateFreight, "/freights/0", HttpMethod.PUT);
|
var freightPut = createHttpRequest(updateFreight, "/freights/0", HttpMethod.PUT);
|
||||||
execute(client, freightPut, "PUT update freight ID=0");
|
execute(client, freightPut, "PUT update freight ID=0");
|
||||||
|
|
||||||
Freight patchFreight = new Freight(1, "Furniture", "Office desks", 800, dims, Freight.Status.DELIVERED);
|
var patchFreight = new FreightRequest("Furniture", "Office desks", 800, dims, Freight.Status.DELIVERED);
|
||||||
var freightPatch = createHttpRequest(patchFreight, "/freights/0", HttpMethod.PATCH);
|
var freightPatch = createHttpRequest(patchFreight, "/freights/0", HttpMethod.PATCH);
|
||||||
execute(client, freightPatch, "PATCH update freight ID=0");
|
execute(client, freightPatch, "PATCH update freight ID=0");
|
||||||
|
|
||||||
@@ -96,7 +99,7 @@ public class ApplicationClient {
|
|||||||
private static void testRoutes(CloseableHttpClient client) throws Exception {
|
private static void testRoutes(CloseableHttpClient client) throws Exception {
|
||||||
System.out.println("--- ROUTES ---");
|
System.out.println("--- ROUTES ---");
|
||||||
|
|
||||||
Route newRoute = new Route(0, 0, List.of(0L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
var newRoute = new RouteRequest(0, List.of(0L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
||||||
var routePost = createHttpRequest(newRoute, "/routes", HttpMethod.POST);
|
var routePost = createHttpRequest(newRoute, "/routes", HttpMethod.POST);
|
||||||
execute(client, routePost, "POST new route");
|
execute(client, routePost, "POST new route");
|
||||||
|
|
||||||
@@ -105,11 +108,11 @@ public class ApplicationClient {
|
|||||||
execute(client, new HttpGet(BASE_URL + "/routes/0"), "GET route by ID=0");
|
execute(client, new HttpGet(BASE_URL + "/routes/0"), "GET route by ID=0");
|
||||||
execute(client, new HttpGet(BASE_URL + "/routes/999"), "GET route by ID=999 (not found)");
|
execute(client, new HttpGet(BASE_URL + "/routes/999"), "GET route by ID=999 (not found)");
|
||||||
|
|
||||||
Route updateRoute = new Route(0, 0, List.of(0L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.IN_PROGRESS);
|
var updateRoute = new RouteRequest(0, List.of(0L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.IN_PROGRESS);
|
||||||
var routePut = createHttpRequest(updateRoute, "/routes/0", HttpMethod.PUT);
|
var routePut = createHttpRequest(updateRoute, "/routes/0", HttpMethod.PUT);
|
||||||
execute(client, routePut, "PUT update route ID=0");
|
execute(client, routePut, "PUT update route ID=0");
|
||||||
|
|
||||||
Route patchRoute = new Route(0, 0, List.of(0L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.COMPLETED);
|
var patchRoute = new RouteRequest(0, List.of(0L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.COMPLETED);
|
||||||
var routePatch = createHttpRequest(patchRoute, "/routes/0", HttpMethod.PATCH);
|
var routePatch = createHttpRequest(patchRoute, "/routes/0", HttpMethod.PATCH);
|
||||||
execute(client, routePatch, "PATCH update route ID=0");
|
execute(client, routePatch, "PATCH update route ID=0");
|
||||||
|
|
||||||
@@ -155,7 +158,7 @@ public class ApplicationClient {
|
|||||||
|
|
||||||
// Test 1: Create route with valid references (should succeed)
|
// Test 1: Create route with valid references (should succeed)
|
||||||
System.out.println("[INTER-SERVICE TEST 1] Creating route with valid vehicle and freight references:");
|
System.out.println("[INTER-SERVICE TEST 1] Creating route with valid vehicle and freight references:");
|
||||||
Route validRoute = new Route(0, 0, List.of(0L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
var validRoute = new RouteRequest(0, List.of(0L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
||||||
var validRoutePost = createHttpRequest(validRoute, "/routes", HttpMethod.POST);
|
var validRoutePost = createHttpRequest(validRoute, "/routes", HttpMethod.POST);
|
||||||
execute(client, validRoutePost, "POST route with valid vehicle and freights (should succeed)");
|
execute(client, validRoutePost, "POST route with valid vehicle and freights (should succeed)");
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.jspecify.annotations.Nullable;
|
import org.jspecify.annotations.Nullable;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ua.com.dxrkness.dto.FreightRequest;
|
||||||
|
import ua.com.dxrkness.dto.FreightResponse;
|
||||||
import ua.com.dxrkness.model.Freight;
|
import ua.com.dxrkness.model.Freight;
|
||||||
import ua.com.dxrkness.service.FreightService;
|
import ua.com.dxrkness.service.FreightService;
|
||||||
|
|
||||||
@@ -40,9 +41,9 @@ public class FreightController {
|
|||||||
@Parameter(description = "Filter freights by status (PENDING, IN_TRANSIT, DELIVERED)")
|
@Parameter(description = "Filter freights by status (PENDING, IN_TRANSIT, DELIVERED)")
|
||||||
@RequestParam(name = "status", required = false) Freight.@Nullable Status status) {
|
@RequestParam(name = "status", required = false) Freight.@Nullable Status status) {
|
||||||
if (status != null) {
|
if (status != null) {
|
||||||
return service.getByStatus(status);
|
return service.getByStatus(status).stream().map(FreightResponse::toEntity).toList();
|
||||||
}
|
}
|
||||||
return service.getAll();
|
return service.getAll().stream().map(FreightResponse::toEntity).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -59,7 +60,7 @@ public class FreightController {
|
|||||||
)
|
)
|
||||||
@GetMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
@GetMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
||||||
public Freight getById(@PathVariable("id") long id) {
|
public Freight getById(@PathVariable("id") long id) {
|
||||||
return service.getById(id);
|
return service.getById(id).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -75,8 +76,8 @@ public class FreightController {
|
|||||||
description = "Invalid freight data (e.g., weight exceeds vehicle capacity)"
|
description = "Invalid freight data (e.g., weight exceeds vehicle capacity)"
|
||||||
)
|
)
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Freight add(@RequestBody Freight newFreight) {
|
public Freight add(@RequestBody FreightRequest newFreight) {
|
||||||
return service.add(newFreight);
|
return service.add(newFreight).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -92,8 +93,8 @@ public class FreightController {
|
|||||||
description = "Invalid input"
|
description = "Invalid input"
|
||||||
)
|
)
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public Freight update(@PathVariable("id") long id, @RequestBody Freight newFreight) {
|
public Freight update(@PathVariable("id") long id, @RequestBody FreightRequest newFreight) {
|
||||||
return service.update(id, newFreight);
|
return service.update(id, newFreight).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -109,8 +110,8 @@ public class FreightController {
|
|||||||
description = "Invalid input"
|
description = "Invalid input"
|
||||||
)
|
)
|
||||||
@PatchMapping("/{id}")
|
@PatchMapping("/{id}")
|
||||||
public Freight updatePatch(@PathVariable("id") long id, @RequestBody Freight newFreight) {
|
public Freight updatePatch(@PathVariable("id") long id, @RequestBody FreightRequest newFreight) {
|
||||||
return service.update(id, newFreight);
|
return service.update(id, newFreight).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -127,6 +128,6 @@ public class FreightController {
|
|||||||
)
|
)
|
||||||
@DeleteMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
@DeleteMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
||||||
public Freight delete(@PathVariable("id") long id) {
|
public Freight delete(@PathVariable("id") long id) {
|
||||||
return service.delete(id);
|
return service.delete(id).toEntity();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import org.jspecify.annotations.NullMarked;
|
|||||||
import org.jspecify.annotations.Nullable;
|
import org.jspecify.annotations.Nullable;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ua.com.dxrkness.dto.RouteRequest;
|
||||||
|
import ua.com.dxrkness.dto.RouteResponse;
|
||||||
import ua.com.dxrkness.model.Freight;
|
import ua.com.dxrkness.model.Freight;
|
||||||
import ua.com.dxrkness.model.Route;
|
import ua.com.dxrkness.model.Route;
|
||||||
import ua.com.dxrkness.model.Vehicle;
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
@@ -17,6 +19,7 @@ import ua.com.dxrkness.service.VehicleService;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(
|
@RequestMapping(
|
||||||
@@ -56,19 +59,19 @@ public class RouteController {
|
|||||||
@Parameter(description = "Filter routes by status (PLANNED, IN_PROGRESS, COMPLETED, CANCELLED)")
|
@Parameter(description = "Filter routes by status (PLANNED, IN_PROGRESS, COMPLETED, CANCELLED)")
|
||||||
@RequestParam(name = "status", required = false) Route.@Nullable Status status) {
|
@RequestParam(name = "status", required = false) Route.@Nullable Status status) {
|
||||||
if (status != null) {
|
if (status != null) {
|
||||||
return routeService.getByStatus(status);
|
return routeService.getByStatus(status).stream().map(RouteResponse::toEntity).toList();
|
||||||
}
|
}
|
||||||
if (vehicleId != null) {
|
if (vehicleId != null) {
|
||||||
return routeService.getByVehicleId(vehicleId);
|
return routeService.getByVehicleId(vehicleId).stream().map(RouteResponse::toEntity).toList();
|
||||||
}
|
}
|
||||||
if (freightId != null) {
|
if (freightId != null) {
|
||||||
var route = routeService.getByFreightId(freightId);
|
var route = routeService.getByFreightId(freightId);
|
||||||
if (route == null) {
|
if (route == null) {
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
return List.of(route);
|
return Stream.of(route).map(RouteResponse::toEntity).toList();
|
||||||
}
|
}
|
||||||
return routeService.getAll();
|
return routeService.getAll().stream().map(RouteResponse::toEntity).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -87,7 +90,7 @@ public class RouteController {
|
|||||||
public Route getById(
|
public Route getById(
|
||||||
@Parameter(description = "ID of the route to retrieve", required = true)
|
@Parameter(description = "ID of the route to retrieve", required = true)
|
||||||
@PathVariable("id") long id) {
|
@PathVariable("id") long id) {
|
||||||
return routeService.getById(id);
|
return routeService.getById(id).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -105,7 +108,7 @@ public class RouteController {
|
|||||||
public Vehicle getVehicleById(
|
public Vehicle getVehicleById(
|
||||||
@Parameter(description = "ID of the route to retrieve", required = true)
|
@Parameter(description = "ID of the route to retrieve", required = true)
|
||||||
@PathVariable("id") long id) {
|
@PathVariable("id") long id) {
|
||||||
return vehicleService.getById(routeService.getById(id).vehicleId());
|
return vehicleService.getById(routeService.getById(id).vehicleId()).toVehicle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -126,7 +129,7 @@ public class RouteController {
|
|||||||
var route = routeService.getById(id);
|
var route = routeService.getById(id);
|
||||||
var freights = new ArrayList<Freight>();
|
var freights = new ArrayList<Freight>();
|
||||||
for (var freightId : route.freightId()) {
|
for (var freightId : route.freightId()) {
|
||||||
freights.add(freightService.getById(freightId));
|
freights.add(freightService.getById(freightId).toEntity());
|
||||||
}
|
}
|
||||||
return freights;
|
return freights;
|
||||||
}
|
}
|
||||||
@@ -153,8 +156,8 @@ public class RouteController {
|
|||||||
description = "Route object to be created. Must include valid vehicleId and freightId list.",
|
description = "Route object to be created. Must include valid vehicleId and freightId list.",
|
||||||
required = true
|
required = true
|
||||||
)
|
)
|
||||||
@RequestBody Route newRoute) {
|
@RequestBody RouteRequest newRoute) {
|
||||||
return routeService.add(newRoute);
|
return routeService.add(newRoute).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -177,8 +180,8 @@ public class RouteController {
|
|||||||
description = "Updated route object",
|
description = "Updated route object",
|
||||||
required = true
|
required = true
|
||||||
)
|
)
|
||||||
@RequestBody Route newRoute) {
|
@RequestBody RouteRequest newRoute) {
|
||||||
return routeService.update(id, newRoute);
|
return routeService.update(id, newRoute).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -201,8 +204,8 @@ public class RouteController {
|
|||||||
description = "Route object with fields to update",
|
description = "Route object with fields to update",
|
||||||
required = true
|
required = true
|
||||||
)
|
)
|
||||||
@RequestBody Route newRoute) {
|
@RequestBody RouteRequest newRoute) {
|
||||||
return routeService.update(id, newRoute);
|
return routeService.update(id, newRoute).toEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -222,6 +225,6 @@ public class RouteController {
|
|||||||
public Route delete(
|
public Route delete(
|
||||||
@Parameter(description = "ID of the route to delete", required = true)
|
@Parameter(description = "ID of the route to delete", required = true)
|
||||||
@PathVariable("id") long id) {
|
@PathVariable("id") long id) {
|
||||||
return routeService.delete(id);
|
return routeService.delete(id).toEntity();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import org.jspecify.annotations.NullMarked;
|
|||||||
import org.jspecify.annotations.Nullable;
|
import org.jspecify.annotations.Nullable;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ua.com.dxrkness.dto.VehicleRequest;
|
||||||
|
import ua.com.dxrkness.dto.VehicleResponse;
|
||||||
import ua.com.dxrkness.model.Vehicle;
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
import ua.com.dxrkness.service.VehicleService;
|
import ua.com.dxrkness.service.VehicleService;
|
||||||
|
|
||||||
@@ -38,9 +40,9 @@ public class VehicleController {
|
|||||||
@Parameter(description = "Filter vehicles by status (AVAILABLE, IN_TRANSIT, MAINTENANCE)")
|
@Parameter(description = "Filter vehicles by status (AVAILABLE, IN_TRANSIT, MAINTENANCE)")
|
||||||
@RequestParam(name = "status", required = false) Vehicle.@Nullable Status status) {
|
@RequestParam(name = "status", required = false) Vehicle.@Nullable Status status) {
|
||||||
if (status != null) {
|
if (status != null) {
|
||||||
return service.getByStatus(status);
|
return service.getByStatus(status).stream().map(VehicleResponse::toVehicle).toList();
|
||||||
}
|
}
|
||||||
return service.getAll();
|
return service.getAll().stream().map(VehicleResponse::toVehicle).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -53,7 +55,7 @@ public class VehicleController {
|
|||||||
public Vehicle getById(
|
public Vehicle getById(
|
||||||
@Parameter(description = "ID of the vehicle to retrieve", required = true)
|
@Parameter(description = "ID of the vehicle to retrieve", required = true)
|
||||||
@PathVariable("id") long id) {
|
@PathVariable("id") long id) {
|
||||||
return service.getById(id);
|
return service.getById(id).toVehicle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -68,8 +70,8 @@ public class VehicleController {
|
|||||||
description = "Vehicle object to be created",
|
description = "Vehicle object to be created",
|
||||||
required = true
|
required = true
|
||||||
)
|
)
|
||||||
@RequestBody Vehicle newVehicle) {
|
@RequestBody VehicleRequest newVehicle) {
|
||||||
return service.add(newVehicle);
|
return service.add(newVehicle).toVehicle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -86,8 +88,8 @@ public class VehicleController {
|
|||||||
description = "Updated vehicle object",
|
description = "Updated vehicle object",
|
||||||
required = true
|
required = true
|
||||||
)
|
)
|
||||||
@RequestBody Vehicle newVehicle) {
|
@RequestBody VehicleRequest newVehicle) {
|
||||||
return service.update(id, newVehicle);
|
return service.update(id, newVehicle).toVehicle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -104,8 +106,8 @@ public class VehicleController {
|
|||||||
description = "Vehicle object with fields to update",
|
description = "Vehicle object with fields to update",
|
||||||
required = true
|
required = true
|
||||||
)
|
)
|
||||||
@RequestBody Vehicle newVehicle) {
|
@RequestBody VehicleRequest newVehicle) {
|
||||||
return service.update(id, newVehicle);
|
return service.update(id, newVehicle).toVehicle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -119,6 +121,6 @@ public class VehicleController {
|
|||||||
public Vehicle delete(
|
public Vehicle delete(
|
||||||
@Parameter(description = "ID of the vehicle to delete", required = true)
|
@Parameter(description = "ID of the vehicle to delete", required = true)
|
||||||
@PathVariable("id") long id) {
|
@PathVariable("id") long id) {
|
||||||
return service.delete(id);
|
return service.delete(id).toVehicle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import ua.com.dxrkness.model.Freight;
|
|||||||
|
|
||||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
public record FreightDto(
|
public record FreightDto(
|
||||||
long id,
|
|
||||||
String name,
|
String name,
|
||||||
String description,
|
String description,
|
||||||
int weightKg,
|
int weightKg,
|
||||||
@@ -15,7 +14,16 @@ public record FreightDto(
|
|||||||
) {
|
) {
|
||||||
public static FreightDto fromFreight(Freight freight) {
|
public static FreightDto fromFreight(Freight freight) {
|
||||||
return new FreightDto(
|
return new FreightDto(
|
||||||
freight.id(),
|
freight.name(),
|
||||||
|
freight.description(),
|
||||||
|
freight.weightKg(),
|
||||||
|
freight.dimensions(),
|
||||||
|
freight.status()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FreightDto fromResponse(FreightResponse freight) {
|
||||||
|
return new FreightDto(
|
||||||
freight.name(),
|
freight.name(),
|
||||||
freight.description(),
|
freight.description(),
|
||||||
freight.weightKg(),
|
freight.weightKg(),
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package ua.com.dxrkness.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
|
import ua.com.dxrkness.model.Freight;
|
||||||
|
|
||||||
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
public record FreightRequest(
|
||||||
|
String name,
|
||||||
|
String description,
|
||||||
|
int weightKg,
|
||||||
|
Freight.Dimensions dimensions,
|
||||||
|
Freight.Status status
|
||||||
|
) {
|
||||||
|
public Freight toEntity() {
|
||||||
|
return new Freight(0, name, description, weightKg, dimensions, status);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package ua.com.dxrkness.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
|
import ua.com.dxrkness.model.Freight;
|
||||||
|
|
||||||
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
public record FreightResponse(
|
||||||
|
String name,
|
||||||
|
String description,
|
||||||
|
int weightKg,
|
||||||
|
Freight.Dimensions dimensions,
|
||||||
|
Freight.Status status
|
||||||
|
) {
|
||||||
|
public Freight toEntity() {
|
||||||
|
return new Freight(0, name, description, weightKg, dimensions, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FreightResponse fromEntity(Freight freight) {
|
||||||
|
return new FreightResponse(freight.name(), freight.description(), freight.weightKg(), freight.dimensions(), freight.status());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package ua.com.dxrkness.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
|
import ua.com.dxrkness.model.Route;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
public record RouteRequest(long vehicleId,
|
||||||
|
List<Long> freightId,
|
||||||
|
String startLocation,
|
||||||
|
String endLocation,
|
||||||
|
Double distanceKm,
|
||||||
|
Double estimatedDurationHours,
|
||||||
|
Route.Status status) {
|
||||||
|
public Route toEntity() {
|
||||||
|
return new Route(0, vehicleId, freightId, startLocation, endLocation, distanceKm, estimatedDurationHours, status);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package ua.com.dxrkness.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
|
import ua.com.dxrkness.model.Route;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
public record RouteResponse(long vehicleId,
|
||||||
|
List<Long> freightId,
|
||||||
|
String startLocation,
|
||||||
|
String endLocation,
|
||||||
|
Double distanceKm,
|
||||||
|
Double estimatedDurationHours,
|
||||||
|
Route.Status status) {
|
||||||
|
public Route toEntity() {
|
||||||
|
return new Route(0, vehicleId, freightId, startLocation, endLocation, distanceKm, estimatedDurationHours, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RouteResponse fromRoute(Route route) {
|
||||||
|
return new RouteResponse(route.vehicleId(), route.freightId(), route.startLocation(), route.endLocation(), route.distanceKm(), route.estimatedDurationHours(), route.status());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,4 +25,28 @@ public record VehicleDto(
|
|||||||
vehicle.status()
|
vehicle.status()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static VehicleDto fromResponse(VehicleResponse vehicle) {
|
||||||
|
return new VehicleDto(
|
||||||
|
vehicle.id(),
|
||||||
|
vehicle.brand(),
|
||||||
|
vehicle.model(),
|
||||||
|
vehicle.licensePlate(),
|
||||||
|
vehicle.year(),
|
||||||
|
vehicle.capacityKg(),
|
||||||
|
vehicle.status()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vehicle toVehicle() {
|
||||||
|
return new Vehicle(
|
||||||
|
this.id(),
|
||||||
|
this.brand(),
|
||||||
|
this.model(),
|
||||||
|
this.licensePlate(),
|
||||||
|
this.year(),
|
||||||
|
this.capacityKg(),
|
||||||
|
this.status()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package ua.com.dxrkness.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
|
|
||||||
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
public record VehicleRequest(
|
||||||
|
String brand,
|
||||||
|
String model,
|
||||||
|
String licensePlate,
|
||||||
|
int year,
|
||||||
|
int capacityKg,
|
||||||
|
Vehicle.Status status
|
||||||
|
) {
|
||||||
|
public Vehicle toEntity() {
|
||||||
|
return new Vehicle(0, brand, model, licensePlate, year, capacityKg, status);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package ua.com.dxrkness.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
|
|
||||||
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
|
public record VehicleResponse(
|
||||||
|
long id,
|
||||||
|
String brand,
|
||||||
|
String model,
|
||||||
|
String licensePlate,
|
||||||
|
int year,
|
||||||
|
int capacityKg,
|
||||||
|
Vehicle.Status status
|
||||||
|
) {
|
||||||
|
public static VehicleResponse fromVehicle(Vehicle vehicle) {
|
||||||
|
return new VehicleResponse(
|
||||||
|
vehicle.id(),
|
||||||
|
vehicle.brand(),
|
||||||
|
vehicle.model(),
|
||||||
|
vehicle.licensePlate(),
|
||||||
|
vehicle.year(),
|
||||||
|
vehicle.capacityKg(),
|
||||||
|
vehicle.status()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vehicle toVehicle() {
|
||||||
|
return new Vehicle(
|
||||||
|
this.id(),
|
||||||
|
this.brand(),
|
||||||
|
this.model(),
|
||||||
|
this.licensePlate(),
|
||||||
|
this.year(),
|
||||||
|
this.capacityKg(),
|
||||||
|
this.status()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ package ua.com.dxrkness.service;
|
|||||||
|
|
||||||
import org.jspecify.annotations.Nullable;
|
import org.jspecify.annotations.Nullable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import ua.com.dxrkness.dto.FreightRequest;
|
||||||
|
import ua.com.dxrkness.dto.FreightResponse;
|
||||||
import ua.com.dxrkness.exception.FreightNotFoundException;
|
import ua.com.dxrkness.exception.FreightNotFoundException;
|
||||||
import ua.com.dxrkness.model.Freight;
|
import ua.com.dxrkness.model.Freight;
|
||||||
import ua.com.dxrkness.repository.FreightRepository;
|
import ua.com.dxrkness.repository.FreightRepository;
|
||||||
@@ -16,39 +18,40 @@ public final class FreightService {
|
|||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Freight> getAll() {
|
public List<FreightResponse> getAll() {
|
||||||
return repo.getAll();
|
return repo.getAll().stream().map(FreightResponse::fromEntity).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Freight add(Freight freight) {
|
public FreightResponse add(FreightRequest freight) {
|
||||||
int id = repo.add(freight);
|
int id = repo.add(freight.toEntity());
|
||||||
return new Freight(id, freight.name(), freight.description(), freight.weightKg(), freight.dimensions(), freight.status());
|
return FreightResponse.fromEntity(new Freight(id, freight.name(), freight.description(), freight.weightKg(), freight.dimensions(), freight.status()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Freight getById(long id) {
|
public FreightResponse getById(long id) {
|
||||||
Freight freight = repo.getById(id);
|
Freight freight = repo.getById(id);
|
||||||
if (freight == null) {
|
if (freight == null) {
|
||||||
throw new FreightNotFoundException("Freight with ID " + id + " not found.");
|
throw new FreightNotFoundException("Freight with ID " + id + " not found.");
|
||||||
}
|
}
|
||||||
return freight;
|
return FreightResponse.fromEntity(freight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Freight> getByStatus(Freight.Status status) {
|
public List<FreightResponse> getByStatus(Freight.Status status) {
|
||||||
return repo.getAll().stream()
|
return repo.getAll().stream()
|
||||||
.filter(f -> f.status() == status)
|
.filter(f -> f.status() == status)
|
||||||
|
.map(FreightResponse::fromEntity)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Freight update(long id, Freight newFreight) {
|
public FreightResponse update(long id, FreightRequest newFreight) {
|
||||||
newFreight = new Freight(id, newFreight.name(), newFreight.description(), newFreight.weightKg(), newFreight.dimensions(), newFreight.status());
|
var newFreightEntity = new Freight(id, newFreight.name(), newFreight.description(), newFreight.weightKg(), newFreight.dimensions(), newFreight.status());
|
||||||
return repo.update(id, newFreight);
|
return FreightResponse.fromEntity(repo.update(id, newFreightEntity));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Freight delete(long id) {
|
public FreightResponse delete(long id) {
|
||||||
var deleted = repo.delete(id);
|
var deleted = repo.delete(id);
|
||||||
if (deleted == null) {
|
if (deleted == null) {
|
||||||
throw new FreightNotFoundException("Freight with ID " + id + " not found.");
|
throw new FreightNotFoundException("Freight with ID " + id + " not found.");
|
||||||
}
|
}
|
||||||
return deleted;
|
return FreightResponse.fromEntity(deleted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package ua.com.dxrkness.service;
|
|||||||
import org.jspecify.annotations.Nullable;
|
import org.jspecify.annotations.Nullable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ua.com.dxrkness.dto.FreightDto;
|
import ua.com.dxrkness.dto.FreightDto;
|
||||||
|
import ua.com.dxrkness.dto.RouteRequest;
|
||||||
|
import ua.com.dxrkness.dto.RouteResponse;
|
||||||
import ua.com.dxrkness.dto.VehicleDto;
|
import ua.com.dxrkness.dto.VehicleDto;
|
||||||
import ua.com.dxrkness.exception.FreightNotFoundException;
|
import ua.com.dxrkness.exception.FreightNotFoundException;
|
||||||
import ua.com.dxrkness.exception.RouteNotFoundException;
|
import ua.com.dxrkness.exception.RouteNotFoundException;
|
||||||
@@ -26,37 +28,37 @@ public final class RouteService {
|
|||||||
this.freightService = freightService;
|
this.freightService = freightService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Route> getAll() {
|
public List<RouteResponse> getAll() {
|
||||||
return repo.getAll();
|
return repo.getAll().stream().map(RouteResponse::fromRoute).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route getById(long id) {
|
public RouteResponse getById(long id) {
|
||||||
Route route = repo.getById(id);
|
Route route = repo.getById(id);
|
||||||
if (route == null) {
|
if (route == null) {
|
||||||
throw new RouteNotFoundException("Route with ID " + id + " not found.");
|
throw new RouteNotFoundException("Route with ID " + id + " not found.");
|
||||||
}
|
}
|
||||||
return route;
|
return RouteResponse.fromRoute(route);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route add(Route route) {
|
public RouteResponse add(RouteRequest route) {
|
||||||
VehicleDto vehicleDto = validateAndGetVehicle(route.vehicleId());
|
VehicleDto vehicleDto = validateAndGetVehicle(route.vehicleId());
|
||||||
List<FreightDto> freightDtos = validateAndGetFreights(route.freightId());
|
List<FreightDto> freightDtos = validateAndGetFreights(route.freightId());
|
||||||
|
|
||||||
int id = repo.add(route);
|
int id = repo.add(route.toEntity());
|
||||||
return new Route(id,
|
return RouteResponse.fromRoute(new Route(id,
|
||||||
route.vehicleId(),
|
route.vehicleId(),
|
||||||
route.freightId(),
|
route.freightId(),
|
||||||
route.startLocation(),
|
route.startLocation(),
|
||||||
route.endLocation(),
|
route.endLocation(),
|
||||||
route.distanceKm(),
|
route.distanceKm(),
|
||||||
route.estimatedDurationHours(),
|
route.estimatedDurationHours(),
|
||||||
route.status());
|
route.status()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private VehicleDto validateAndGetVehicle(long vehicleId) {
|
private VehicleDto validateAndGetVehicle(long vehicleId) {
|
||||||
try {
|
try {
|
||||||
var vehicle = vehicleService.getById(vehicleId);
|
var vehicle = vehicleService.getById(vehicleId);
|
||||||
return VehicleDto.fromVehicle(vehicle);
|
return VehicleDto.fromResponse(vehicle);
|
||||||
} catch (VehicleNotFoundException e) {
|
} catch (VehicleNotFoundException e) {
|
||||||
throw new VehicleNotFoundException(
|
throw new VehicleNotFoundException(
|
||||||
"Cannot create or update route: Referenced vehicle with ID " + vehicleId + " not found.");
|
"Cannot create or update route: Referenced vehicle with ID " + vehicleId + " not found.");
|
||||||
@@ -68,7 +70,7 @@ public final class RouteService {
|
|||||||
for (long freightId : freightIds) {
|
for (long freightId : freightIds) {
|
||||||
try {
|
try {
|
||||||
var freight = freightService.getById(freightId);
|
var freight = freightService.getById(freightId);
|
||||||
freightDtos.add(FreightDto.fromFreight(freight));
|
freightDtos.add(FreightDto.fromResponse(freight));
|
||||||
} catch (FreightNotFoundException e) {
|
} catch (FreightNotFoundException e) {
|
||||||
throw new FreightNotFoundException(
|
throw new FreightNotFoundException(
|
||||||
"Cannot create or update route: Referenced freight with ID " + freightId + " not found.");
|
"Cannot create or update route: Referenced freight with ID " + freightId + " not found.");
|
||||||
@@ -77,25 +79,26 @@ public final class RouteService {
|
|||||||
return freightDtos;
|
return freightDtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable Route getByFreightId(long freightId) {
|
public @Nullable RouteResponse getByFreightId(long freightId) {
|
||||||
return repo.getByFreightId(freightId);
|
return RouteResponse.fromRoute(repo.getByFreightId(freightId));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Route> getByVehicleId(long vehicleId) {
|
public List<RouteResponse> getByVehicleId(long vehicleId) {
|
||||||
return repo.getByVehicleId(vehicleId);
|
return repo.getByVehicleId(vehicleId).stream().map(RouteResponse::fromRoute).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Route> getByStatus(Route.Status status) {
|
public List<RouteResponse> getByStatus(Route.Status status) {
|
||||||
return repo.getAll().stream()
|
return repo.getAll().stream()
|
||||||
.filter(r -> r.status() == status)
|
.filter(r -> r.status() == status)
|
||||||
|
.map(RouteResponse::fromRoute)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route update(long id, Route newRoute) {
|
public RouteResponse update(long id, RouteRequest newRoute) {
|
||||||
VehicleDto vehicleDto = validateAndGetVehicle(newRoute.vehicleId());
|
VehicleDto vehicleDto = validateAndGetVehicle(newRoute.vehicleId());
|
||||||
List<FreightDto> freightDtos = validateAndGetFreights(newRoute.freightId());
|
List<FreightDto> freightDtos = validateAndGetFreights(newRoute.freightId());
|
||||||
|
|
||||||
newRoute = new Route(id,
|
var newRouteEntity = new Route(id,
|
||||||
newRoute.vehicleId(),
|
newRoute.vehicleId(),
|
||||||
newRoute.freightId(),
|
newRoute.freightId(),
|
||||||
newRoute.startLocation(),
|
newRoute.startLocation(),
|
||||||
@@ -103,14 +106,14 @@ public final class RouteService {
|
|||||||
newRoute.distanceKm(),
|
newRoute.distanceKm(),
|
||||||
newRoute.estimatedDurationHours(),
|
newRoute.estimatedDurationHours(),
|
||||||
newRoute.status());
|
newRoute.status());
|
||||||
return repo.update(id, newRoute);
|
return RouteResponse.fromRoute(repo.update(id, newRouteEntity));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route delete(long id) {
|
public RouteResponse delete(long id) {
|
||||||
var deleted = repo.delete(id);
|
var deleted = repo.delete(id);
|
||||||
if (deleted == null) {
|
if (deleted == null) {
|
||||||
throw new RouteNotFoundException("Route with ID " + id + " not found.");
|
throw new RouteNotFoundException("Route with ID " + id + " not found.");
|
||||||
}
|
}
|
||||||
return deleted;
|
return RouteResponse.fromRoute(deleted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package ua.com.dxrkness.service;
|
package ua.com.dxrkness.service;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import ua.com.dxrkness.dto.VehicleRequest;
|
||||||
|
import ua.com.dxrkness.dto.VehicleResponse;
|
||||||
import ua.com.dxrkness.exception.VehicleNotFoundException;
|
import ua.com.dxrkness.exception.VehicleNotFoundException;
|
||||||
import ua.com.dxrkness.model.Vehicle;
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
import ua.com.dxrkness.repository.VehicleRepository;
|
import ua.com.dxrkness.repository.VehicleRepository;
|
||||||
@@ -15,45 +17,46 @@ public final class VehicleService {
|
|||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Vehicle> getAll() {
|
public List<VehicleResponse> getAll() {
|
||||||
return repo.getAll();
|
return repo.getAll().stream().map(VehicleResponse::fromVehicle).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Vehicle> getByStatus(Vehicle.Status status) {
|
public List<VehicleResponse> getByStatus(Vehicle.Status status) {
|
||||||
return repo.getAll().stream()
|
return repo.getAll().stream()
|
||||||
.filter(v -> v.status() == status)
|
.filter(v -> v.status() == status)
|
||||||
|
.map(VehicleResponse::fromVehicle)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vehicle add(Vehicle veh) {
|
public VehicleResponse add(VehicleRequest veh) {
|
||||||
int id = repo.add(veh);
|
int id = repo.add(veh.toEntity());
|
||||||
return new Vehicle(id, veh.brand(), veh.model(), veh.licensePlate(), veh.year(), veh.capacityKg(), veh.status());
|
return new VehicleResponse(id, veh.brand(), veh.model(), veh.licensePlate(), veh.year(), veh.capacityKg(), veh.status());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vehicle getById(long id) {
|
public VehicleResponse getById(long id) {
|
||||||
Vehicle vehicle = repo.getById(id);
|
Vehicle vehicle = repo.getById(id);
|
||||||
if (vehicle == null) {
|
if (vehicle == null) {
|
||||||
throw new VehicleNotFoundException("Vehicle with ID " + id + " not found.");
|
throw new VehicleNotFoundException("Vehicle with ID " + id + " not found.");
|
||||||
}
|
}
|
||||||
return vehicle;
|
return VehicleResponse.fromVehicle(vehicle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vehicle update(long id, Vehicle newVehicle) {
|
public VehicleResponse update(long id, VehicleRequest newVehicle) {
|
||||||
newVehicle = new Vehicle(id,
|
var newVehicleEntity = new Vehicle(id,
|
||||||
newVehicle.brand(),
|
newVehicle.brand(),
|
||||||
newVehicle.model(),
|
newVehicle.model(),
|
||||||
newVehicle.licensePlate(),
|
newVehicle.licensePlate(),
|
||||||
newVehicle.year(),
|
newVehicle.year(),
|
||||||
newVehicle.capacityKg(),
|
newVehicle.capacityKg(),
|
||||||
newVehicle.status());
|
newVehicle.status());
|
||||||
return repo.update(id, newVehicle);
|
return VehicleResponse.fromVehicle(repo.update(id, newVehicleEntity));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vehicle delete(long id) {
|
public VehicleResponse delete(long id) {
|
||||||
var deleted = repo.delete(id);
|
var deleted = repo.delete(id);
|
||||||
if (deleted == null) {
|
if (deleted == null) {
|
||||||
throw new VehicleNotFoundException("Vehicle with ID " + id + " not found.");
|
throw new VehicleNotFoundException("Vehicle with ID " + id + " not found.");
|
||||||
}
|
}
|
||||||
return deleted;
|
return VehicleResponse.fromVehicle(deleted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user