fixes for 2nd practical
This commit is contained in:
@@ -9,6 +9,9 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
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.Route;
|
||||
import ua.com.dxrkness.model.Vehicle;
|
||||
@@ -41,7 +44,7 @@ public class ApplicationClient {
|
||||
|
||||
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);
|
||||
HttpPost post = new HttpPost(BASE_URL + "/vehicles");
|
||||
post.setEntity(new StringEntity(vehicleXml, "UTF-8"));
|
||||
@@ -54,11 +57,11 @@ public class ApplicationClient {
|
||||
execute(client, get, "GET vehicle by ID=0");
|
||||
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);
|
||||
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);
|
||||
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");
|
||||
|
||||
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);
|
||||
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/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);
|
||||
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);
|
||||
execute(client, freightPatch, "PATCH update freight ID=0");
|
||||
|
||||
@@ -96,7 +99,7 @@ public class ApplicationClient {
|
||||
private static void testRoutes(CloseableHttpClient client) throws Exception {
|
||||
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);
|
||||
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/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);
|
||||
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);
|
||||
execute(client, routePatch, "PATCH update route ID=0");
|
||||
|
||||
@@ -155,7 +158,7 @@ public class ApplicationClient {
|
||||
|
||||
// Test 1: Create route with valid references (should succeed)
|
||||
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);
|
||||
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 org.jspecify.annotations.Nullable;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.service.FreightService;
|
||||
|
||||
@@ -40,9 +41,9 @@ public class FreightController {
|
||||
@Parameter(description = "Filter freights by status (PENDING, IN_TRANSIT, DELIVERED)")
|
||||
@RequestParam(name = "status", required = false) Freight.@Nullable Status status) {
|
||||
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(
|
||||
@@ -59,7 +60,7 @@ public class FreightController {
|
||||
)
|
||||
@GetMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
||||
public Freight getById(@PathVariable("id") long id) {
|
||||
return service.getById(id);
|
||||
return service.getById(id).toEntity();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -75,8 +76,8 @@ public class FreightController {
|
||||
description = "Invalid freight data (e.g., weight exceeds vehicle capacity)"
|
||||
)
|
||||
@PostMapping
|
||||
public Freight add(@RequestBody Freight newFreight) {
|
||||
return service.add(newFreight);
|
||||
public Freight add(@RequestBody FreightRequest newFreight) {
|
||||
return service.add(newFreight).toEntity();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -92,8 +93,8 @@ public class FreightController {
|
||||
description = "Invalid input"
|
||||
)
|
||||
@PutMapping("/{id}")
|
||||
public Freight update(@PathVariable("id") long id, @RequestBody Freight newFreight) {
|
||||
return service.update(id, newFreight);
|
||||
public Freight update(@PathVariable("id") long id, @RequestBody FreightRequest newFreight) {
|
||||
return service.update(id, newFreight).toEntity();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -109,8 +110,8 @@ public class FreightController {
|
||||
description = "Invalid input"
|
||||
)
|
||||
@PatchMapping("/{id}")
|
||||
public Freight updatePatch(@PathVariable("id") long id, @RequestBody Freight newFreight) {
|
||||
return service.update(id, newFreight);
|
||||
public Freight updatePatch(@PathVariable("id") long id, @RequestBody FreightRequest newFreight) {
|
||||
return service.update(id, newFreight).toEntity();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -127,6 +128,6 @@ public class FreightController {
|
||||
)
|
||||
@DeleteMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
||||
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.springframework.http.MediaType;
|
||||
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.Route;
|
||||
import ua.com.dxrkness.model.Vehicle;
|
||||
@@ -17,6 +19,7 @@ import ua.com.dxrkness.service.VehicleService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(
|
||||
@@ -56,19 +59,19 @@ public class RouteController {
|
||||
@Parameter(description = "Filter routes by status (PLANNED, IN_PROGRESS, COMPLETED, CANCELLED)")
|
||||
@RequestParam(name = "status", required = false) Route.@Nullable Status status) {
|
||||
if (status != null) {
|
||||
return routeService.getByStatus(status);
|
||||
return routeService.getByStatus(status).stream().map(RouteResponse::toEntity).toList();
|
||||
}
|
||||
if (vehicleId != null) {
|
||||
return routeService.getByVehicleId(vehicleId);
|
||||
return routeService.getByVehicleId(vehicleId).stream().map(RouteResponse::toEntity).toList();
|
||||
}
|
||||
if (freightId != null) {
|
||||
var route = routeService.getByFreightId(freightId);
|
||||
if (route == null) {
|
||||
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(
|
||||
@@ -87,7 +90,7 @@ public class RouteController {
|
||||
public Route getById(
|
||||
@Parameter(description = "ID of the route to retrieve", required = true)
|
||||
@PathVariable("id") long id) {
|
||||
return routeService.getById(id);
|
||||
return routeService.getById(id).toEntity();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -105,7 +108,7 @@ public class RouteController {
|
||||
public Vehicle getVehicleById(
|
||||
@Parameter(description = "ID of the route to retrieve", required = true)
|
||||
@PathVariable("id") long id) {
|
||||
return vehicleService.getById(routeService.getById(id).vehicleId());
|
||||
return vehicleService.getById(routeService.getById(id).vehicleId()).toVehicle();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -126,7 +129,7 @@ public class RouteController {
|
||||
var route = routeService.getById(id);
|
||||
var freights = new ArrayList<Freight>();
|
||||
for (var freightId : route.freightId()) {
|
||||
freights.add(freightService.getById(freightId));
|
||||
freights.add(freightService.getById(freightId).toEntity());
|
||||
}
|
||||
return freights;
|
||||
}
|
||||
@@ -153,8 +156,8 @@ public class RouteController {
|
||||
description = "Route object to be created. Must include valid vehicleId and freightId list.",
|
||||
required = true
|
||||
)
|
||||
@RequestBody Route newRoute) {
|
||||
return routeService.add(newRoute);
|
||||
@RequestBody RouteRequest newRoute) {
|
||||
return routeService.add(newRoute).toEntity();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -177,8 +180,8 @@ public class RouteController {
|
||||
description = "Updated route object",
|
||||
required = true
|
||||
)
|
||||
@RequestBody Route newRoute) {
|
||||
return routeService.update(id, newRoute);
|
||||
@RequestBody RouteRequest newRoute) {
|
||||
return routeService.update(id, newRoute).toEntity();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -201,8 +204,8 @@ public class RouteController {
|
||||
description = "Route object with fields to update",
|
||||
required = true
|
||||
)
|
||||
@RequestBody Route newRoute) {
|
||||
return routeService.update(id, newRoute);
|
||||
@RequestBody RouteRequest newRoute) {
|
||||
return routeService.update(id, newRoute).toEntity();
|
||||
}
|
||||
|
||||
|
||||
@@ -222,6 +225,6 @@ public class RouteController {
|
||||
public Route delete(
|
||||
@Parameter(description = "ID of the route to delete", required = true)
|
||||
@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.springframework.http.MediaType;
|
||||
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.service.VehicleService;
|
||||
|
||||
@@ -38,9 +40,9 @@ public class VehicleController {
|
||||
@Parameter(description = "Filter vehicles by status (AVAILABLE, IN_TRANSIT, MAINTENANCE)")
|
||||
@RequestParam(name = "status", required = false) Vehicle.@Nullable Status status) {
|
||||
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(
|
||||
@@ -53,7 +55,7 @@ public class VehicleController {
|
||||
public Vehicle getById(
|
||||
@Parameter(description = "ID of the vehicle to retrieve", required = true)
|
||||
@PathVariable("id") long id) {
|
||||
return service.getById(id);
|
||||
return service.getById(id).toVehicle();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -68,8 +70,8 @@ public class VehicleController {
|
||||
description = "Vehicle object to be created",
|
||||
required = true
|
||||
)
|
||||
@RequestBody Vehicle newVehicle) {
|
||||
return service.add(newVehicle);
|
||||
@RequestBody VehicleRequest newVehicle) {
|
||||
return service.add(newVehicle).toVehicle();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -86,8 +88,8 @@ public class VehicleController {
|
||||
description = "Updated vehicle object",
|
||||
required = true
|
||||
)
|
||||
@RequestBody Vehicle newVehicle) {
|
||||
return service.update(id, newVehicle);
|
||||
@RequestBody VehicleRequest newVehicle) {
|
||||
return service.update(id, newVehicle).toVehicle();
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -104,8 +106,8 @@ public class VehicleController {
|
||||
description = "Vehicle object with fields to update",
|
||||
required = true
|
||||
)
|
||||
@RequestBody Vehicle newVehicle) {
|
||||
return service.update(id, newVehicle);
|
||||
@RequestBody VehicleRequest newVehicle) {
|
||||
return service.update(id, newVehicle).toVehicle();
|
||||
}
|
||||
|
||||
|
||||
@@ -119,6 +121,6 @@ public class VehicleController {
|
||||
public Vehicle delete(
|
||||
@Parameter(description = "ID of the vehicle to delete", required = true)
|
||||
@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)
|
||||
public record FreightDto(
|
||||
long id,
|
||||
String name,
|
||||
String description,
|
||||
int weightKg,
|
||||
@@ -15,7 +14,16 @@ public record FreightDto(
|
||||
) {
|
||||
public static FreightDto fromFreight(Freight freight) {
|
||||
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.description(),
|
||||
freight.weightKg(),
|
||||
|
||||
19
src/main/java/ua/com/dxrkness/dto/FreightRequest.java
Normal file
19
src/main/java/ua/com/dxrkness/dto/FreightRequest.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
23
src/main/java/ua/com/dxrkness/dto/FreightResponse.java
Normal file
23
src/main/java/ua/com/dxrkness/dto/FreightResponse.java
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
21
src/main/java/ua/com/dxrkness/dto/RouteRequest.java
Normal file
21
src/main/java/ua/com/dxrkness/dto/RouteRequest.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
25
src/main/java/ua/com/dxrkness/dto/RouteResponse.java
Normal file
25
src/main/java/ua/com/dxrkness/dto/RouteResponse.java
Normal file
@@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
20
src/main/java/ua/com/dxrkness/dto/VehicleRequest.java
Normal file
20
src/main/java/ua/com/dxrkness/dto/VehicleRequest.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
40
src/main/java/ua/com/dxrkness/dto/VehicleResponse.java
Normal file
40
src/main/java/ua/com/dxrkness/dto/VehicleResponse.java
Normal file
@@ -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.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.model.Freight;
|
||||
import ua.com.dxrkness.repository.FreightRepository;
|
||||
@@ -16,39 +18,40 @@ public final class FreightService {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
public List<Freight> getAll() {
|
||||
return repo.getAll();
|
||||
public List<FreightResponse> getAll() {
|
||||
return repo.getAll().stream().map(FreightResponse::fromEntity).toList();
|
||||
}
|
||||
|
||||
public Freight add(Freight freight) {
|
||||
int id = repo.add(freight);
|
||||
return new Freight(id, freight.name(), freight.description(), freight.weightKg(), freight.dimensions(), freight.status());
|
||||
public FreightResponse add(FreightRequest freight) {
|
||||
int id = repo.add(freight.toEntity());
|
||||
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);
|
||||
if (freight == null) {
|
||||
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()
|
||||
.filter(f -> f.status() == status)
|
||||
.map(FreightResponse::fromEntity)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Freight update(long id, Freight newFreight) {
|
||||
newFreight = new Freight(id, newFreight.name(), newFreight.description(), newFreight.weightKg(), newFreight.dimensions(), newFreight.status());
|
||||
return repo.update(id, newFreight);
|
||||
public FreightResponse update(long id, FreightRequest newFreight) {
|
||||
var newFreightEntity = new Freight(id, newFreight.name(), newFreight.description(), newFreight.weightKg(), newFreight.dimensions(), newFreight.status());
|
||||
return FreightResponse.fromEntity(repo.update(id, newFreightEntity));
|
||||
}
|
||||
|
||||
public Freight delete(long id) {
|
||||
public FreightResponse delete(long id) {
|
||||
var deleted = repo.delete(id);
|
||||
if (deleted == null) {
|
||||
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.springframework.stereotype.Service;
|
||||
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.exception.FreightNotFoundException;
|
||||
import ua.com.dxrkness.exception.RouteNotFoundException;
|
||||
@@ -26,37 +28,37 @@ public final class RouteService {
|
||||
this.freightService = freightService;
|
||||
}
|
||||
|
||||
public List<Route> getAll() {
|
||||
return repo.getAll();
|
||||
public List<RouteResponse> getAll() {
|
||||
return repo.getAll().stream().map(RouteResponse::fromRoute).toList();
|
||||
}
|
||||
|
||||
public Route getById(long id) {
|
||||
public RouteResponse getById(long id) {
|
||||
Route route = repo.getById(id);
|
||||
if (route == null) {
|
||||
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());
|
||||
List<FreightDto> freightDtos = validateAndGetFreights(route.freightId());
|
||||
|
||||
int id = repo.add(route);
|
||||
return new Route(id,
|
||||
int id = repo.add(route.toEntity());
|
||||
return RouteResponse.fromRoute(new Route(id,
|
||||
route.vehicleId(),
|
||||
route.freightId(),
|
||||
route.startLocation(),
|
||||
route.endLocation(),
|
||||
route.distanceKm(),
|
||||
route.estimatedDurationHours(),
|
||||
route.status());
|
||||
route.status()));
|
||||
}
|
||||
|
||||
private VehicleDto validateAndGetVehicle(long vehicleId) {
|
||||
try {
|
||||
var vehicle = vehicleService.getById(vehicleId);
|
||||
return VehicleDto.fromVehicle(vehicle);
|
||||
return VehicleDto.fromResponse(vehicle);
|
||||
} catch (VehicleNotFoundException e) {
|
||||
throw new VehicleNotFoundException(
|
||||
"Cannot create or update route: Referenced vehicle with ID " + vehicleId + " not found.");
|
||||
@@ -68,7 +70,7 @@ public final class RouteService {
|
||||
for (long freightId : freightIds) {
|
||||
try {
|
||||
var freight = freightService.getById(freightId);
|
||||
freightDtos.add(FreightDto.fromFreight(freight));
|
||||
freightDtos.add(FreightDto.fromResponse(freight));
|
||||
} catch (FreightNotFoundException e) {
|
||||
throw new FreightNotFoundException(
|
||||
"Cannot create or update route: Referenced freight with ID " + freightId + " not found.");
|
||||
@@ -77,25 +79,26 @@ public final class RouteService {
|
||||
return freightDtos;
|
||||
}
|
||||
|
||||
public @Nullable Route getByFreightId(long freightId) {
|
||||
return repo.getByFreightId(freightId);
|
||||
public @Nullable RouteResponse getByFreightId(long freightId) {
|
||||
return RouteResponse.fromRoute(repo.getByFreightId(freightId));
|
||||
}
|
||||
|
||||
public List<Route> getByVehicleId(long vehicleId) {
|
||||
return repo.getByVehicleId(vehicleId);
|
||||
public List<RouteResponse> getByVehicleId(long 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()
|
||||
.filter(r -> r.status() == status)
|
||||
.map(RouteResponse::fromRoute)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Route update(long id, Route newRoute) {
|
||||
public RouteResponse update(long id, RouteRequest newRoute) {
|
||||
VehicleDto vehicleDto = validateAndGetVehicle(newRoute.vehicleId());
|
||||
List<FreightDto> freightDtos = validateAndGetFreights(newRoute.freightId());
|
||||
|
||||
newRoute = new Route(id,
|
||||
var newRouteEntity = new Route(id,
|
||||
newRoute.vehicleId(),
|
||||
newRoute.freightId(),
|
||||
newRoute.startLocation(),
|
||||
@@ -103,14 +106,14 @@ public final class RouteService {
|
||||
newRoute.distanceKm(),
|
||||
newRoute.estimatedDurationHours(),
|
||||
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);
|
||||
if (deleted == null) {
|
||||
throw new RouteNotFoundException("Route with ID " + id + " not found.");
|
||||
}
|
||||
return deleted;
|
||||
return RouteResponse.fromRoute(deleted);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package ua.com.dxrkness.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.model.Vehicle;
|
||||
import ua.com.dxrkness.repository.VehicleRepository;
|
||||
@@ -15,45 +17,46 @@ public final class VehicleService {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
public List<Vehicle> getAll() {
|
||||
return repo.getAll();
|
||||
public List<VehicleResponse> 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()
|
||||
.filter(v -> v.status() == status)
|
||||
.map(VehicleResponse::fromVehicle)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Vehicle add(Vehicle veh) {
|
||||
int id = repo.add(veh);
|
||||
return new Vehicle(id, veh.brand(), veh.model(), veh.licensePlate(), veh.year(), veh.capacityKg(), veh.status());
|
||||
public VehicleResponse add(VehicleRequest veh) {
|
||||
int id = repo.add(veh.toEntity());
|
||||
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);
|
||||
if (vehicle == null) {
|
||||
throw new VehicleNotFoundException("Vehicle with ID " + id + " not found.");
|
||||
}
|
||||
return vehicle;
|
||||
return VehicleResponse.fromVehicle(vehicle);
|
||||
}
|
||||
|
||||
public Vehicle update(long id, Vehicle newVehicle) {
|
||||
newVehicle = new Vehicle(id,
|
||||
public VehicleResponse update(long id, VehicleRequest newVehicle) {
|
||||
var newVehicleEntity = new Vehicle(id,
|
||||
newVehicle.brand(),
|
||||
newVehicle.model(),
|
||||
newVehicle.licensePlate(),
|
||||
newVehicle.year(),
|
||||
newVehicle.capacityKg(),
|
||||
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);
|
||||
if (deleted == null) {
|
||||
throw new VehicleNotFoundException("Vehicle with ID " + id + " not found.");
|
||||
}
|
||||
return deleted;
|
||||
return VehicleResponse.fromVehicle(deleted);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user