264 lines
11 KiB
C#
264 lines
11 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using cuqmbr.TravelGuide.Domain.Enums;
|
|
using cuqmbr.TravelGuide.Application.Common.Models;
|
|
using cuqmbr.TravelGuide.Application.Common.ViewModels;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments
|
|
.Commands.AddVehicleEnrollment;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments
|
|
.Queries.GetVehicleEnrollmentsPage;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments
|
|
.Queries.GetVehicleEnrollment;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments
|
|
.Commands.UpdateVehicleEnrollment;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments
|
|
.Commands.DeleteVehicleEnrollment;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments.Models;
|
|
using cuqmbr.TravelGuide.Application.VehicleEnrollments.ViewModels;
|
|
|
|
namespace cuqmbr.TravelGuide.HttpApi.Controllers;
|
|
|
|
[Route("vehicleEnrollments")]
|
|
public class VehicleEnrollmentsController : ControllerBase
|
|
{
|
|
[HttpPost]
|
|
[SwaggerOperation("Add a vehicle enrollment")]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status201Created, "Object successfuly created",
|
|
typeof(VehicleEnrollmentDto))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status400BadRequest, "Input data validation error",
|
|
typeof(HttpValidationProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status400BadRequest,
|
|
"Enrollment travel time overlapping with " +
|
|
"other enrollment time of the vehicle",
|
|
typeof(HttpValidationProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status401Unauthorized, "Unauthorized to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status403Forbidden,
|
|
"Not enough privileges to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status404NotFound, "Given route not found",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status404NotFound, "Given vehicle not found",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status404NotFound, "At least one route address not found",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status500InternalServerError, "Internal server error",
|
|
typeof(ProblemDetails))]
|
|
public async Task<ActionResult<VehicleEnrollmentDto>> Add(
|
|
[FromBody] AddVehicleEnrollmentViewModel viewModel,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return StatusCode(
|
|
StatusCodes.Status201Created,
|
|
await Mediator.Send(
|
|
new AddVehicleEnrollmentCommand()
|
|
{
|
|
DepartureTime = viewModel.DepartureTime,
|
|
Currency = Currency.FromName(viewModel.Currency),
|
|
VehicleGuid = viewModel.VehicleUuid,
|
|
RouteGuid = viewModel.RouteUuid,
|
|
RouteAddressDetails = viewModel.RouteAddressDetails.Select(
|
|
rad => new RouteAddressDetailModel()
|
|
{
|
|
TimeToNextAddress = rad.TimeToNextAddress,
|
|
CostToNextAddress = rad.CostToNextAddress,
|
|
CurrentAddressStopTime = rad.CurrentAddressStopTime,
|
|
RouteAddressGuid = rad.RouteAddressUuid
|
|
})
|
|
.ToArray(),
|
|
EmployeeGuids = viewModel.EmployeeUuids
|
|
},
|
|
cancellationToken));
|
|
}
|
|
|
|
[HttpGet]
|
|
[SwaggerOperation("Get a list of all vehicle enrollments")]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status200OK, "Request successful",
|
|
typeof(PaginatedList<VehicleEnrollmentDto>))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status400BadRequest, "Input data validation error",
|
|
typeof(HttpValidationProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status401Unauthorized, "Unauthorized to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status403Forbidden,
|
|
"Not enough privileges to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status500InternalServerError, "Internal server error",
|
|
typeof(ProblemDetails))]
|
|
public async Task<PaginatedList<VehicleEnrollmentDto>> GetPage(
|
|
[FromQuery] PageQuery pageQuery, [FromQuery] SearchQuery searchQuery,
|
|
[FromQuery] SortQuery sortQuery,
|
|
[FromQuery] GetVehicleEnrollmentsPageFilterViewModel filterQuery,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await Mediator.Send(
|
|
new GetVehicleEnrollmentsPageQuery()
|
|
{
|
|
PageNumber = pageQuery.PageNumber,
|
|
PageSize = pageQuery.PageSize,
|
|
// Search = searchQuery.Search,
|
|
Sort = sortQuery.Sort,
|
|
RouteGuid = filterQuery.RouteGuid,
|
|
VehicleGuid = filterQuery.VehicleGuid,
|
|
NumberOfAddressesGreaterThanOrEqual =
|
|
filterQuery.NumberOfAddressesGreaterThanOrEqual,
|
|
NumberOfAddressesLessThanOrEqual =
|
|
filterQuery.NumberOfAddressesLessThanOrEqual,
|
|
DepartureTimeGreaterThanOrEqual =
|
|
filterQuery.DepartureTimeGreaterThanOrEqual,
|
|
DepartureTimeLessThanOrEqual =
|
|
filterQuery.DepartureTimeLessThanOrEqual,
|
|
ArrivalTimeGreaterThanOrEqual =
|
|
filterQuery.ArrivalTimeGreaterThanOrEqual,
|
|
ArrivalTimeLessThanOrEqual =
|
|
filterQuery.ArrivalTimeLessThanOrEqual,
|
|
TravelTimeGreaterThanOrEqual =
|
|
filterQuery.TravelTimeGreaterThanOrEqual,
|
|
TravelTimeLessThanOrEqual =
|
|
filterQuery.TravelTimeLessThanOrEqual,
|
|
TimeMovingGreaterThanOrEqual =
|
|
filterQuery.TimeMovingGreaterThanOrEqual,
|
|
TimeMovingLessThanOrEqual =
|
|
filterQuery.TimeMovingLessThanOrEqual,
|
|
TimeInStopsGreaterThanOrEqual =
|
|
filterQuery.TimeInStopsGreaterThanOrEqual,
|
|
TimeInStopsLessThanOrEqual =
|
|
filterQuery.TimeInStopsLessThanOrEqual,
|
|
CostGreaterThanOrEqual =
|
|
filterQuery.CostGreaterThanOrEqual,
|
|
CostLessThanOrEqual =
|
|
filterQuery.CostLessThanOrEqual,
|
|
Currency = Currency.FromName(filterQuery.Currency),
|
|
EmployeeGuids = filterQuery.EmployeeUuids
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
[HttpGet("{uuid:guid}")]
|
|
[SwaggerOperation("Get a vehicle enrollment by uuid")]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status200OK, "Request successful",
|
|
typeof(VehicleEnrollmentDto))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status400BadRequest, "Input data validation error",
|
|
typeof(HttpValidationProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status401Unauthorized, "Unauthorized to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status403Forbidden,
|
|
"Not enough privileges to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status404NotFound, "Object not found",
|
|
typeof(VehicleEnrollmentDto))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status500InternalServerError, "Internal server error",
|
|
typeof(ProblemDetails))]
|
|
public async Task<VehicleEnrollmentDto> Get(
|
|
[FromRoute] Guid uuid,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await Mediator.Send(
|
|
new GetVehicleEnrollmentQuery() { Guid = uuid },
|
|
cancellationToken);
|
|
}
|
|
|
|
[HttpPut("{uuid:guid}")]
|
|
[SwaggerOperation("Update a vehicle enrollment")]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status200OK, "Request successful",
|
|
typeof(VehicleEnrollmentDto))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status400BadRequest, "Input data validation error",
|
|
typeof(HttpValidationProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status400BadRequest,
|
|
"Enrollment travel time overlapping with " +
|
|
"other enrollment time of the vehicle",
|
|
typeof(HttpValidationProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status401Unauthorized, "Unauthorized to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status403Forbidden,
|
|
"Not enough privileges to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status404NotFound, "Object not found",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status404NotFound, "At least one route address not found",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status500InternalServerError, "Internal server error",
|
|
typeof(ProblemDetails))]
|
|
public async Task<VehicleEnrollmentDto> Update(
|
|
[FromRoute] Guid uuid,
|
|
[FromBody] UpdateVehicleEnrollmentViewModel viewModel,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await Mediator.Send(
|
|
new UpdateVehicleEnrollmentCommand()
|
|
{
|
|
Guid = uuid,
|
|
DepartureTime = viewModel.DepartureTime,
|
|
Currency = Currency.FromName(viewModel.Currency),
|
|
RouteAddressDetails = viewModel.RouteAddressDetails.Select(
|
|
rad => new RouteAddressDetailModel()
|
|
{
|
|
TimeToNextAddress = rad.TimeToNextAddress,
|
|
CostToNextAddress = rad.CostToNextAddress,
|
|
CurrentAddressStopTime = rad.CurrentAddressStopTime,
|
|
RouteAddressGuid = rad.RouteAddressUuid
|
|
})
|
|
.ToArray(),
|
|
EmployeeGuids = viewModel.EmployeeUuids
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
[HttpDelete("{uuid:guid}")]
|
|
[SwaggerOperation("Delete a vehicle enrollment")]
|
|
[SwaggerResponse(StatusCodes.Status204NoContent, "Request successful")]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status400BadRequest, "Input data validation error",
|
|
typeof(HttpValidationProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status401Unauthorized, "Unauthorized to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status403Forbidden,
|
|
"Not enough privileges to perform an action",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status404NotFound, "Object not found",
|
|
typeof(ProblemDetails))]
|
|
[SwaggerResponse(
|
|
StatusCodes.Status500InternalServerError, "Internal server error",
|
|
typeof(ProblemDetails))]
|
|
public async Task<IActionResult> Delete(
|
|
[FromRoute] Guid uuid,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await Mediator.Send(
|
|
new DeleteVehicleEnrollmentCommand() { Guid = uuid },
|
|
cancellationToken);
|
|
return StatusCode(StatusCodes.Status204NoContent);
|
|
}
|
|
}
|