126 lines
4.9 KiB
Java
126 lines
4.9 KiB
Java
package ua.com.dxrkness.controller;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
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.model.Vehicle;
|
|
import ua.com.dxrkness.service.VehicleService;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping(
|
|
value = "/vehicles",
|
|
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
|
|
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}
|
|
)
|
|
@Tag(name = "Vehicle", description = "Vehicles management")
|
|
@NullMarked
|
|
public class VehicleController {
|
|
private final VehicleService service;
|
|
|
|
public VehicleController(VehicleService service) {
|
|
this.service = service;
|
|
}
|
|
|
|
@Operation(
|
|
summary = "Retrieve all vehicles",
|
|
description = "Get a list of all vehicles or filter by status"
|
|
)
|
|
@ApiResponse(responseCode = "200", description = "Successfully retrieved list of vehicles")
|
|
@GetMapping(consumes = MediaType.ALL_VALUE)
|
|
public List<Vehicle> getAll(
|
|
@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.getAll();
|
|
}
|
|
|
|
@Operation(
|
|
summary = "Retrieve a vehicle by ID",
|
|
description = "Get a single vehicle by its ID"
|
|
)
|
|
@ApiResponse(responseCode = "200", description = "Vehicle found")
|
|
@ApiResponse(responseCode = "404", description = "Vehicle not found")
|
|
@GetMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
|
public List<Vehicle> getById(
|
|
@Parameter(description = "ID of the vehicle to retrieve", required = true)
|
|
@PathVariable("id") long id) {
|
|
return List.of(service.getById(id));
|
|
}
|
|
|
|
@Operation(
|
|
summary = "Create a new vehicle",
|
|
description = "Add a new vehicle to the system"
|
|
)
|
|
@ApiResponse(responseCode = "200", description = "Vehicle successfully created")
|
|
@ApiResponse(responseCode = "400", description = "Invalid vehicle data provided")
|
|
@PostMapping
|
|
public Vehicle add(
|
|
@io.swagger.v3.oas.annotations.parameters.RequestBody(
|
|
description = "Vehicle object to be created",
|
|
required = true
|
|
)
|
|
@RequestBody VehicleRequest newVehicle) {
|
|
return service.add(newVehicle.toEntity());
|
|
}
|
|
|
|
@Operation(
|
|
summary = "Update a vehicle (full)",
|
|
description = "Update all fields of an existing vehicle by ID"
|
|
)
|
|
@ApiResponse(responseCode = "200", description = "Vehicle successfully updated")
|
|
@ApiResponse(responseCode = "400", description = "Invalid vehicle data provided")
|
|
@PutMapping("/{id}")
|
|
public Vehicle update(
|
|
@Parameter(description = "ID of the vehicle to update", required = true)
|
|
@PathVariable("id") long id,
|
|
@io.swagger.v3.oas.annotations.parameters.RequestBody(
|
|
description = "Updated vehicle object",
|
|
required = true
|
|
)
|
|
@RequestBody VehicleRequest newVehicle) {
|
|
return service.update(id, newVehicle.toEntity());
|
|
}
|
|
|
|
@Operation(
|
|
summary = "Update a vehicle (partial)",
|
|
description = "Update specific fields of an existing vehicle by ID"
|
|
)
|
|
@ApiResponse(responseCode = "200", description = "Vehicle successfully updated")
|
|
@ApiResponse(responseCode = "400", description = "Invalid vehicle data provided")
|
|
@PatchMapping("/{id}")
|
|
public Vehicle updatePatch(
|
|
@Parameter(description = "ID of the vehicle to update", required = true)
|
|
@PathVariable("id") long id,
|
|
@io.swagger.v3.oas.annotations.parameters.RequestBody(
|
|
description = "Vehicle object with fields to update",
|
|
required = true
|
|
)
|
|
@RequestBody VehicleRequest newVehicle) {
|
|
return service.update(id, newVehicle.toEntity());
|
|
}
|
|
|
|
|
|
@Operation(
|
|
summary = "Delete a vehicle",
|
|
description = "Delete a vehicle by its ID"
|
|
)
|
|
@ApiResponse(responseCode = "200", description = "Vehicle successfully deleted")
|
|
@ApiResponse(responseCode = "404", description = "Vehicle not found")
|
|
@DeleteMapping(value = "/{id}", consumes = MediaType.ALL_VALUE)
|
|
public Vehicle delete(
|
|
@Parameter(description = "ID of the vehicle to delete", required = true)
|
|
@PathVariable("id") long id) {
|
|
return service.delete(id);
|
|
}
|
|
}
|