2024-08-25 13:39:00 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-06-22 11:51:38 +02:00
|
|
|
|
using Vegasco.Server.Api.Cars;
|
2025-06-12 18:22:37 +02:00
|
|
|
|
using Vegasco.Server.Api.Persistence;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
|
namespace Vegasco.Server.Api.Consumptions;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
|
|
|
|
|
public static class GetConsumptions
|
|
|
|
|
|
{
|
2025-06-15 09:36:52 +02:00
|
|
|
|
public class ApiResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
public IEnumerable<ResponseDto> Consumptions { get; set; } = [];
|
|
|
|
|
|
}
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2025-06-15 09:36:52 +02:00
|
|
|
|
public record ResponseDto(
|
|
|
|
|
|
Guid Id,
|
|
|
|
|
|
DateTimeOffset DateTime,
|
|
|
|
|
|
double Distance,
|
|
|
|
|
|
double Amount,
|
2025-06-22 11:51:38 +02:00
|
|
|
|
CarDto Car,
|
|
|
|
|
|
double? LiterPer100Km);
|
|
|
|
|
|
|
|
|
|
|
|
public record CarDto(
|
|
|
|
|
|
Guid Id,
|
|
|
|
|
|
string Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
public static CarDto FromCar(Car car)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new CarDto(car.Id.Value, car.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2025-06-15 09:36:52 +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
|
|
|
|
|
2025-06-15 09:36:52 +02:00
|
|
|
|
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return builder
|
|
|
|
|
|
.MapGet("consumptions", Endpoint)
|
|
|
|
|
|
.WithDescription("Returns all consumption entries")
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.WithTags("Consumptions")
|
|
|
|
|
|
.Produces<ApiResponse>();
|
2025-06-15 09:36:52 +02:00
|
|
|
|
}
|
2024-08-25 13:39:00 +02:00
|
|
|
|
|
2025-06-15 09:36:52 +02:00
|
|
|
|
private static async Task<Ok<ApiResponse>> Endpoint(
|
|
|
|
|
|
[AsParameters] Request request,
|
|
|
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2025-06-23 16:50:07 +02:00
|
|
|
|
Dictionary<CarId, List<Consumption>> consumptionsByCar = await dbContext.Consumptions
|
2025-06-19 18:56:40 +02:00
|
|
|
|
.OrderByDescending(x => x.DateTime)
|
2025-06-22 11:51:38 +02:00
|
|
|
|
.Include(x => x.Car)
|
2025-06-23 16:50:07 +02:00
|
|
|
|
.GroupBy(x => x.CarId)
|
|
|
|
|
|
.ToDictionaryAsync(x => x.Key, x => x.ToList(), cancellationToken);
|
2024-08-25 13:39:00 +02:00
|
|
|
|
|
2025-06-22 11:51:38 +02:00
|
|
|
|
List<ResponseDto> responses = [];
|
|
|
|
|
|
|
2025-06-23 16:50:07 +02:00
|
|
|
|
foreach (var consumptions in consumptionsByCar.Select(x => x.Value))
|
2025-06-15 09:36:52 +02:00
|
|
|
|
{
|
2025-06-23 16:50:07 +02:00
|
|
|
|
for (int i = 0; i < consumptions.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Consumption consumption = consumptions[i];
|
2025-06-22 11:51:38 +02:00
|
|
|
|
|
2025-06-23 16:50:07 +02:00
|
|
|
|
double? literPer100Km = null;
|
2025-06-22 11:51:38 +02:00
|
|
|
|
|
2025-06-23 16:50:07 +02:00
|
|
|
|
bool isLast = i == consumptions.Count - 1;
|
|
|
|
|
|
if (!isLast)
|
|
|
|
|
|
{
|
|
|
|
|
|
Consumption previousConsumption = consumptions[i + 1];
|
|
|
|
|
|
double distanceDiff = consumption.Distance - previousConsumption.Distance;
|
|
|
|
|
|
literPer100Km = consumption.Amount / (distanceDiff / 100);
|
|
|
|
|
|
}
|
2025-06-22 11:51:38 +02:00
|
|
|
|
|
2025-06-23 16:50:07 +02:00
|
|
|
|
responses.Add(new ResponseDto(
|
|
|
|
|
|
consumption.Id.Value,
|
|
|
|
|
|
consumption.DateTime,
|
|
|
|
|
|
consumption.Distance,
|
|
|
|
|
|
consumption.Amount,
|
|
|
|
|
|
CarDto.FromCar(consumption.Car),
|
|
|
|
|
|
literPer100Km));
|
|
|
|
|
|
}
|
2025-06-22 11:51:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ApiResponse apiResponse = new() { Consumptions = responses };
|
2025-06-15 09:36:52 +02:00
|
|
|
|
return TypedResults.Ok(apiResponse);
|
|
|
|
|
|
}
|
2024-08-23 18:02:18 +02:00
|
|
|
|
}
|