diff --git a/src/Vegasco.Server.Api/Cars/DeleteCar.cs b/src/Vegasco.Server.Api/Cars/DeleteCar.cs index 271b1a2..a86fb04 100644 --- a/src/Vegasco.Server.Api/Cars/DeleteCar.cs +++ b/src/Vegasco.Server.Api/Cars/DeleteCar.cs @@ -1,4 +1,5 @@ -using Vegasco.Server.Api.Persistence; +using Microsoft.EntityFrameworkCore; +using Vegasco.Server.Api.Persistence; namespace Vegasco.Server.Api.Cars; @@ -17,18 +18,21 @@ public static class DeleteCar public static async Task Endpoint( Guid id, ApplicationDbContext dbContext, + ILoggerFactory loggerFactory, CancellationToken cancellationToken) { - Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken); + var rows = await dbContext.Cars + .Where(x => x.Id == new CarId(id)) + .ExecuteDeleteAsync(cancellationToken); - if (car is null) + if (rows > 1) { - return TypedResults.NotFound(); + var logger = loggerFactory.CreateLogger(nameof(DeleteCar)); + logger.LogWarning("Deleted '{DeletedRowCount}' rows for id '{CarId}'", rows, id); } - dbContext.Cars.Remove(car); - await dbContext.SaveChangesAsync(cancellationToken); - - return TypedResults.NoContent(); + return rows > 0 + ? TypedResults.NoContent() + : TypedResults.NotFound(); } } \ No newline at end of file diff --git a/src/Vegasco.Server.Api/Consumptions/DeleteConsumptions.cs b/src/Vegasco.Server.Api/Consumptions/DeleteConsumptions.cs index bedc45c..1cbcb46 100644 --- a/src/Vegasco.Server.Api/Consumptions/DeleteConsumptions.cs +++ b/src/Vegasco.Server.Api/Consumptions/DeleteConsumptions.cs @@ -1,4 +1,5 @@ -using Vegasco.Server.Api.Persistence; +using Microsoft.EntityFrameworkCore; +using Vegasco.Server.Api.Persistence; namespace Vegasco.Server.Api.Consumptions; @@ -15,19 +16,23 @@ public static class DeleteConsumption } private static async Task Endpoint( - ApplicationDbContext dbContext, Guid id, + ApplicationDbContext dbContext, + ILoggerFactory loggerFactory, CancellationToken cancellationToken) { - Consumption? consumption = await dbContext.Consumptions.FindAsync([new ConsumptionId(id)], cancellationToken); - if (consumption is null) + var rows = await dbContext.Consumptions + .Where(x => x.Id == new ConsumptionId(id)) + .ExecuteDeleteAsync(cancellationToken); + + if (rows > 1) { - return TypedResults.NotFound(); + var logger = loggerFactory.CreateLogger(nameof(DeleteConsumption)); + logger.LogWarning("Deleted '{DeletedRowCount}' rows for id '{ConsumptionId}'", rows, id); } - dbContext.Consumptions.Remove(consumption); - await dbContext.SaveChangesAsync(cancellationToken); - - return TypedResults.NoContent(); + return rows > 0 + ? TypedResults.NoContent() + : TypedResults.NotFound(); } } \ No newline at end of file