2025-06-16 20:28:37 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
|
using Vegasco.Server.Api.Persistence;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
|
namespace Vegasco.Server.Api.Cars;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
public static class GetCar
|
|
|
|
|
|
{
|
|
|
|
|
|
public record Response(Guid Id, string Name);
|
|
|
|
|
|
|
|
|
|
|
|
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return builder
|
|
|
|
|
|
.MapGet("cars/{id:guid}", Endpoint)
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.WithDescription("Returns a single car by ID")
|
|
|
|
|
|
.WithTags("Cars")
|
|
|
|
|
|
.Produces<Response>()
|
|
|
|
|
|
.Produces(404);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-23 18:02:18 +02:00
|
|
|
|
private static async Task<IResult> Endpoint(
|
2024-08-17 16:38:40 +02:00
|
|
|
|
Guid id,
|
|
|
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2024-08-23 18:02:18 +02:00
|
|
|
|
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
if (car is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return TypedResults.NotFound();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-17 18:00:23 +02:00
|
|
|
|
var response = new Response(car.Id.Value, car.Name);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return TypedResults.Ok(response);
|
|
|
|
|
|
}
|
2025-06-16 20:28:37 +02:00
|
|
|
|
}
|