65 lines
3.0 KiB
C#
65 lines
3.0 KiB
C#
using Heimdall.Application.DTOs;
|
|||
|
|
using Heimdall.Application.Vehicles.Commands.CreateVehicle;
|
||
|
|
using Heimdall.Application.Vehicles.Commands.UpdateVehicle;
|
||
|
|
using Heimdall.Application.Vehicles.Commands.DeleteVehicle;
|
||
|
|
using Heimdall.Application.Vehicles.Queries.GetVehicle;
|
||
|
|
using Heimdall.Application.Vehicles.Queries.GetVehicles;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using MediatR;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/vehicles")]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class VehiclesController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IMediator _mediator;
|
||
|
|
|
||
|
|
public VehiclesController(IMediator mediator) => _mediator = mediator;
|
||
|
|
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.VehicleRead)]
|
||
|
|
[HttpGet]
|
||
|
|
public async Task<IActionResult> GetAll([FromQuery] string? search, [FromQuery] int page = 1, [FromQuery] int pageSize = 50, CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new GetVehiclesQuery(search, page, pageSize), ct);
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.VehicleRead)]
|
||
|
|
[HttpGet("{id:guid}")]
|
||
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new GetVehicleQuery(id), ct);
|
||
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.VehicleWrite)]
|
||
|
|
[HttpPost]
|
||
|
|
public async Task<IActionResult> Create([FromBody] CreateVehicleRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var command = new CreateVehicleCommand(request.LicensePlate, request.PersonId, request.Make, request.Model, request.Color);
|
||
|
|
var result = await _mediator.Send(command, ct);
|
||
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.VehicleWrite)]
|
||
|
|
[HttpPut("{id:guid}")]
|
||
|
|
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateVehicleRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var command = new UpdateVehicleCommand(id, request.LicensePlate, request.Make, request.Model, request.Color, request.PersonId);
|
||
|
|
var result = await _mediator.Send(command, ct);
|
||
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.VehicleWrite)]
|
||
|
|
[HttpDelete("{id:guid}")]
|
||
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new DeleteVehicleCommand(id), ct);
|
||
|
|
return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
}
|