2024-08-25 13:39:00 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
using Vegasco.WebApi.Persistence;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Vegasco.WebApi.Consumptions;
|
|
|
|
|
|
|
|
|
|
|
|
public static class GetConsumptions
|
|
|
|
|
|
{
|
2024-08-25 13:39:00 +02:00
|
|
|
|
public class ApiResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
public IEnumerable<ResponseDto> Consumptions { get; set; } = [];
|
|
|
|
|
|
}
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2024-08-25 13:39:00 +02:00
|
|
|
|
public record ResponseDto(
|
|
|
|
|
|
Guid Id,
|
|
|
|
|
|
DateTimeOffset DateTime,
|
|
|
|
|
|
double Distance,
|
|
|
|
|
|
double Amount,
|
|
|
|
|
|
bool IgnoreInCalculation,
|
|
|
|
|
|
Guid CarId);
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2024-08-25 13:39:00 +02:00
|
|
|
|
public class Request
|
|
|
|
|
|
{
|
|
|
|
|
|
[FromQuery(Name = "page")] public int? Page { get; set; }
|
|
|
|
|
|
[FromQuery(Name = "pageSize")] public int? PageSize { get; set; }
|
|
|
|
|
|
}
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2024-08-25 13:39:00 +02:00
|
|
|
|
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return builder
|
|
|
|
|
|
.MapGet("consumptions", Endpoint)
|
|
|
|
|
|
.WithDescription("Returns all consumption entries")
|
|
|
|
|
|
.WithTags("Consumptions");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task<Ok<ApiResponse>> Endpoint(
|
|
|
|
|
|
[AsParameters] Request request,
|
|
|
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<ResponseDto> consumptions = await dbContext.Consumptions
|
|
|
|
|
|
.Select(x =>
|
|
|
|
|
|
new ResponseDto(x.Id.Value, x.DateTime, x.Distance, x.Amount, x.IgnoreInCalculation, x.CarId.Value))
|
|
|
|
|
|
.ToListAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
var apiResponse = new ApiResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
Consumptions = consumptions
|
|
|
|
|
|
};
|
|
|
|
|
|
return TypedResults.Ok(apiResponse);
|
|
|
|
|
|
}
|
2024-08-23 18:02:18 +02:00
|
|
|
|
}
|