155 lines
5.4 KiB
C#
155 lines
5.4 KiB
C#
using Distribution.Infra.Authorization;
|
|
using Distribution.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.Ride;
|
|
|
|
[ApiController]
|
|
[Route("api/rides")]
|
|
public class RideController(RideService service, ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAll(
|
|
[FromQuery] RideStatus? status,
|
|
[FromQuery] RideType? type,
|
|
[FromQuery] Guid? driverProfileId,
|
|
[FromQuery] Guid? vehicleId,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20)
|
|
{
|
|
var result = await service.GetAllAsync(status, type, driverProfileId, vehicleId, page, pageSize);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(CreateRideRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}/driver")]
|
|
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}/open")]
|
|
public async Task<IActionResult> Open(Guid id, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.OpenAsync(id, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPatch("{id:guid}/activate")]
|
|
[HttpPost("{id:guid}/activate")]
|
|
public async Task<IActionResult> Activate(Guid id, ActivateRideRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.ActivateAsync(id, request, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPatch("{id:guid}/close")]
|
|
[HttpPost("{id:guid}/close")]
|
|
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-request")]
|
|
public async Task<IActionResult> RaiseCancelRequest(Guid id, RaiseCancelRequest request, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
try
|
|
{
|
|
var result = await service.RaiseCancelRequestAsync(id, request, userId, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpGet("cancel-requests")]
|
|
public async Task<IActionResult> GetCancelRequests(
|
|
[FromQuery] CancelRequestStatus? status,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20)
|
|
{
|
|
var result = await service.GetCancelRequestsAsync(status, page, pageSize);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("cancel-requests/{id:guid}/approve")]
|
|
public async Task<IActionResult> ApproveCancelRequest(Guid id, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
try
|
|
{
|
|
var result = await service.ApproveCancelAsync(id, userId, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("cancel-requests/{id:guid}/reject")]
|
|
public async Task<IActionResult> RejectCancelRequest(Guid id, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
try
|
|
{
|
|
var result = await service.RejectCancelAsync(id, userId, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpGet("{id:guid}/cancel-request")]
|
|
public async Task<IActionResult> GetCancelRequest(Guid id)
|
|
{
|
|
var result = await service.GetCancelRequestAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("breakdown-assist/{rideId:guid}")]
|
|
public async Task<IActionResult> GetBreakdownAssist(Guid rideId)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.GetBreakdownAssistAsync(rideId);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
}
|
|
}
|