Files
vegasco/src/Vegasco.Server.Api/Cars/GetCars.cs

47 lines
1.1 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Vegasco.Server.Api.Persistence;
2024-08-17 16:38:40 +02:00
namespace Vegasco.Server.Api.Cars;
2024-08-17 16:38:40 +02:00
public static class GetCars
{
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)
.WithDescription("Returns all cars")
2024-08-17 16:38:40 +02:00
.WithTags("Cars");
}
private static async Task<Ok<ApiResponse>> Endpoint(
[AsParameters] Request request,
2024-08-17 16:38:40 +02:00
ApplicationDbContext dbContext,
CancellationToken cancellationToken)
{
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);
var response = new ApiResponse
{
Cars = cars
};
return TypedResults.Ok(response);
2024-08-17 16:38:40 +02:00
}
}