Better debug date time error when creating a consumptions #11
@@ -11,6 +11,7 @@ namespace Vegasco.Server.Api.Cars;
|
||||
public static class CreateCar
|
||||
{
|
||||
public record Request(string Name);
|
||||
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
@@ -44,16 +45,20 @@ public static class CreateCar
|
||||
{
|
||||
ILogger logger = loggerFactory.CreateLogger(typeof(CreateCar));
|
||||
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken: cancellationToken);
|
||||
List<ValidationResult> failedValidations =
|
||||
await validators.ValidateAllAsync(request, cancellationToken: cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
string[] errors = failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage)
|
||||
.ToArray();
|
||||
|
||||
logger.LogDebug(
|
||||
"Validation failed for request {@Request} with errors {@Errors}",
|
||||
request,
|
||||
failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage));
|
||||
errors);
|
||||
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
@@ -74,18 +79,11 @@ public static class CreateCar
|
||||
{
|
||||
logger.LogDebug("User with ID '{UserId}' not found, creating new user", userId);
|
||||
|
||||
user = new User
|
||||
{
|
||||
Id = userId
|
||||
};
|
||||
user = new User { Id = userId };
|
||||
await dbContext.Users.AddAsync(user, cancellationToken);
|
||||
}
|
||||
|
||||
Car car = new()
|
||||
{
|
||||
Name = request.Name.Trim(),
|
||||
UserId = userId
|
||||
};
|
||||
Car car = new() { Name = request.Name.Trim(), UserId = userId };
|
||||
|
||||
await dbContext.Cars.AddAsync(car, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
@@ -49,13 +49,16 @@ public static class UpdateCar
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
string[] errors = failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage)
|
||||
.ToArray();
|
||||
|
||||
logger.LogDebug(
|
||||
"Validation failed for request {@Request} with errors {@Errors}",
|
||||
request,
|
||||
failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage));
|
||||
errors);
|
||||
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
@@ -58,13 +58,16 @@ public static class CreateConsumption
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
string[] errors = failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage)
|
||||
.ToArray();
|
||||
|
||||
logger.LogDebug(
|
||||
"Validation failed for request {@Request} with errors {@Errors}",
|
||||
request,
|
||||
failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage));
|
||||
errors);
|
||||
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
@@ -56,13 +56,16 @@ public static class UpdateConsumption
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
string[] errors = failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage)
|
||||
.ToArray();
|
||||
|
||||
logger.LogDebug(
|
||||
"Validation failed for request {@Request} with errors {@Errors}",
|
||||
request,
|
||||
failedValidations
|
||||
.Where(x => !x.IsValid)
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage));
|
||||
errors);
|
||||
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
@@ -81,6 +84,7 @@ public static class UpdateConsumption
|
||||
|
||||
logger.LogTrace("Updated consumption: {@Consumption}", consumption);
|
||||
|
||||
return TypedResults.Ok(new Response(consumption.Id.Value, consumption.DateTime, consumption.Distance, consumption.Amount, consumption.CarId.Value));
|
||||
return TypedResults.Ok(new Response(consumption.Id.Value, consumption.DateTime, consumption.Distance,
|
||||
consumption.Amount, consumption.CarId.Value));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user