2024-08-25 13:39:00 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
using Vegasco.WebApi.Persistence;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Vegasco.WebApi.Cars;
|
|
|
|
|
|
|
|
|
|
|
|
public static class GetCars
|
|
|
|
|
|
{
|
2024-08-25 13:39:00 +02:00
|
|
|
|
public class ApiResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
public IEnumerable<ResponseDto> Cars { get; set; } = [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public record ResponseDto(Guid Id, string Name);
|
|
|
|
|
|
|
|
|
|
|
|
public class Request
|
|
|
|
|
|
{
|
|
|
|
|
|
[FromQuery(Name = "page")] public int? Page { get; set; }
|
|
|
|
|
|
[FromQuery(Name = "pageSize")] public int? PageSize { get; set; }
|
|
|
|
|
|
}
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return builder
|
|
|
|
|
|
.MapGet("cars", Endpoint)
|
2024-08-25 13:39:00 +02:00
|
|
|
|
.WithDescription("Returns all cars")
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.WithTags("Cars");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-25 13:39:00 +02:00
|
|
|
|
private static async Task<Ok<ApiResponse>> Endpoint(
|
|
|
|
|
|
[AsParameters] Request request,
|
2024-08-17 16:38:40 +02:00
|
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2024-08-25 13:39:00 +02:00
|
|
|
|
List<ResponseDto> cars = await dbContext.Cars
|
|
|
|
|
|
.Select(x => new ResponseDto(x.Id.Value, x.Name))
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.ToListAsync(cancellationToken);
|
|
|
|
|
|
|
2024-08-25 13:39:00 +02:00
|
|
|
|
var response = new ApiResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
Cars = cars
|
|
|
|
|
|
};
|
|
|
|
|
|
return TypedResults.Ok(response);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|