83 lines
2.6 KiB
C#
83 lines
2.6 KiB
C#
using Distribution.Features.DriverComplianceConfig;
|
|
using Distribution.Infra.Authorization;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.DriverProfile;
|
|
|
|
[ApiController]
|
|
[Route("api/driver-profiles")]
|
|
public class DriverProfileController(
|
|
DriverProfileService service,
|
|
DriverComplianceConfigService complianceService,
|
|
ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize("driver.view")]
|
|
public async Task<IActionResult> GetAll([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
|
|
{
|
|
var result = await service.GetAllAsync(page, pageSize);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize("driver.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize("driver.manage")]
|
|
public async Task<IActionResult> Create(CreateDriverProfileRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return NotFound(new { error = ex.Message });
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return Conflict(new { error = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
[Authorize("driver.manage")]
|
|
public async Task<IActionResult> Update(Guid id, UpdateDriverProfileRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
[Authorize("driver.manage")]
|
|
public async Task<IActionResult> Deactivate(Guid id, CancellationToken ct)
|
|
{
|
|
var success = await service.DeactivateAsync(id, ct);
|
|
return success ? NoContent() : NotFound();
|
|
}
|
|
|
|
[HttpGet("compliance")]
|
|
[Authorize("driver.view")]
|
|
public async Task<IActionResult> GetCompliance()
|
|
{
|
|
var result = await complianceService.GetAsync();
|
|
return result is null ? NoContent() : Ok(result);
|
|
}
|
|
|
|
[HttpPut("compliance")]
|
|
[Authorize("driver.manage")]
|
|
public async Task<IActionResult> UpsertCompliance(UpdateDriverComplianceConfigRequest request, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
var result = await complianceService.UpsertAsync(request, userId, ct);
|
|
return Ok(result);
|
|
}
|
|
}
|