83 lines
2.7 KiB
C#
83 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Distribution.Models;
|
|
|
|
namespace Distribution.Features.Ride;
|
|
|
|
[ApiController]
|
|
[Route("api/rides")]
|
|
public class RideController(RideService service) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize("ride.view")]
|
|
public async Task<IActionResult> GetAll(
|
|
[FromQuery] RideStatus? status,
|
|
[FromQuery] Guid? driverProfileId,
|
|
[FromQuery] Guid? vehicleId,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20)
|
|
{
|
|
var result = await service.GetAllAsync(status, driverProfileId, vehicleId, page, pageSize);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize("ride.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize("ride.manage")]
|
|
public async Task<IActionResult> Create(CreateRideRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPut("{id:guid}/driver")]
|
|
[Authorize("ride.manage")]
|
|
public async Task<IActionResult> AssignDriver(Guid id, AssignRideDriverRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.AssignDriverAsync(id, request.DriverProfileId, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("{id:guid}/close")]
|
|
[Authorize("ride.manage")]
|
|
public async Task<IActionResult> Close(Guid id, CloseRideRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.CloseAsync(id, request, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("{id:guid}/cancel")]
|
|
[Authorize("ride.manage")]
|
|
public async Task<IActionResult> Cancel(Guid id, CancelRideRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.CancelAsync(id, request, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
}
|