214 lines
7.6 KiB
C#
214 lines
7.6 KiB
C#
|
|
using LogisticsApp.Server.Data;
|
|
using LogisticsApp.Server.DTOs;
|
|
using LogisticsApp.Server.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LogisticsApp.Server.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class VehiclesController : ControllerBase
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public VehiclesController(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/vehicles
|
|
[Authorize]
|
|
[HttpGet]
|
|
public async Task<ActionResult<ApiResponse<PagedResult<Vehicle>>>> GetVehicles([FromQuery] VehicleFilters filters)
|
|
{
|
|
try
|
|
{
|
|
var query = _context.Vehicles.AsQueryable();
|
|
|
|
|
|
if (!string.IsNullOrEmpty(filters.DriverName))
|
|
{
|
|
query = query.Where(v => v.DriverName.ToLower().Contains(filters.DriverName.ToLower()));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(filters.VehicleType))
|
|
{
|
|
query = query.Where(v => v.VehicleType.ToLower().Contains(filters.VehicleType.ToLower()));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(filters.LicensePlate))
|
|
{
|
|
query = query.Where(v => v.LicensePlate.ToLower().Contains(filters.LicensePlate.ToLower()));
|
|
}
|
|
|
|
var totalCount = await query.CountAsync();
|
|
|
|
var vehicles = await query
|
|
.OrderBy(v => v.DriverName)
|
|
.Skip((filters.Page - 1) * filters.PageSize)
|
|
.Take(filters.PageSize)
|
|
.ToListAsync();
|
|
|
|
var result = new PagedResult<Vehicle>
|
|
{
|
|
Items = vehicles,
|
|
TotalCount = totalCount,
|
|
Page = filters.Page,
|
|
PageSize = filters.PageSize,
|
|
TotalPages = (int)Math.Ceiling(totalCount / (double)filters.PageSize)
|
|
};
|
|
|
|
return Ok(new ApiResponse<PagedResult<Vehicle>>(true, "Vehicles retrieved successfully", result));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new ApiResponse<string>(false, $"Internal server error: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
// GET: api/vehicles/5
|
|
[Authorize]
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<ApiResponse<Vehicle>>> GetVehicle(int id)
|
|
{
|
|
try
|
|
{
|
|
var vehicle = await _context.Vehicles
|
|
.FirstOrDefaultAsync(v => v.Id == id);
|
|
|
|
if (vehicle == null)
|
|
{
|
|
return NotFound(new ApiResponse<string>(false, "Vehicle not found"));
|
|
}
|
|
|
|
return Ok(new ApiResponse<Vehicle>(true, "Vehicle retrieved successfully", vehicle));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new ApiResponse<string>(false, $"Internal server error: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
// POST: api/vehicles
|
|
[Authorize]
|
|
[HttpPost]
|
|
public async Task<ActionResult<ApiResponse<Vehicle>>> PostVehicle(Vehicle vehicle)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(vehicle.DriverName) || vehicle.DriverName.Length > 100)
|
|
{
|
|
return BadRequest(new ApiResponse<string>(false, "Driver name is required and must be less than 100 characters"));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(vehicle.VehicleType) || vehicle.VehicleType.Length > 50)
|
|
{
|
|
return BadRequest(new ApiResponse<string>(false, "Vehicle type is required and must be less than 50 characters"));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(vehicle.LicensePlate) || vehicle.LicensePlate.Length > 20)
|
|
{
|
|
return BadRequest(new ApiResponse<string>(false, "License plate is required and must be less than 20 characters"));
|
|
}
|
|
|
|
vehicle.CreatedAt = DateTime.UtcNow;
|
|
_context.Vehicles.Add(vehicle);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetVehicle", new { id = vehicle.Id },
|
|
new ApiResponse<Vehicle>(true, "Vehicle created successfully", vehicle));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new ApiResponse<string>(false, $"Internal server error: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
// PUT: api/vehicles/5
|
|
[Authorize]
|
|
[HttpPut("{id}")]
|
|
public async Task<ActionResult<ApiResponse<Vehicle>>> PutVehicle(int id, Vehicle vehicle)
|
|
{
|
|
try
|
|
{
|
|
if (id != vehicle.Id)
|
|
{
|
|
return BadRequest(new ApiResponse<string>(false, "ID mismatch"));
|
|
}
|
|
|
|
|
|
if (string.IsNullOrEmpty(vehicle.DriverName) || vehicle.DriverName.Length > 100)
|
|
{
|
|
return BadRequest(new ApiResponse<string>(false, "Driver name is required and must be less than 100 characters"));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(vehicle.VehicleType) || vehicle.VehicleType.Length > 50)
|
|
{
|
|
return BadRequest(new ApiResponse<string>(false, "Vehicle type is required and must be less than 50 characters"));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(vehicle.LicensePlate) || vehicle.LicensePlate.Length > 20)
|
|
{
|
|
return BadRequest(new ApiResponse<string>(false, "License plate is required and must be less than 20 characters"));
|
|
}
|
|
|
|
_context.Entry(vehicle).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!VehicleExists(id))
|
|
{
|
|
return NotFound(new ApiResponse<string>(false, "Vehicle not found"));
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return Ok(new ApiResponse<Vehicle>(true, "Vehicle updated successfully", vehicle));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new ApiResponse<string>(false, $"Internal server error: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
// DELETE: api/vehicles/5
|
|
[Authorize]
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<ApiResponse<string>>> DeleteVehicle(int id)
|
|
{
|
|
try
|
|
{
|
|
var vehicle = await _context.Vehicles.FindAsync(id);
|
|
if (vehicle == null)
|
|
{
|
|
return NotFound(new ApiResponse<string>(false, "Vehicle not found"));
|
|
}
|
|
|
|
_context.Vehicles.Remove(vehicle);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Ok(new ApiResponse<string>(true, "Vehicle deleted successfully"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new ApiResponse<string>(false, $"Internal server error: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
private bool VehicleExists(int id)
|
|
{
|
|
return _context.Vehicles.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
} |