Wrote tests for most services, applied EF core optimisations
This commit is contained in:
@@ -14,17 +14,15 @@ public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> cont
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var productPrice = await context.Prices.FirstOrDefaultAsync(p => p.Id == productPriceId, cancellationToken);
|
||||
var rowsUpdated = await context.Prices
|
||||
.Where(p => p.Id == productPriceId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(p => p.Enabled, isEnabled)
|
||||
.SetProperty(p => p.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (productPrice is null)
|
||||
return Result.Fail(new Error($"Product price with ID {productPriceId} not found"));
|
||||
|
||||
productPrice.UpdatedAt = DateTime.UtcNow;
|
||||
productPrice.Enabled = isEnabled;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to change status of product price with ID {productPriceId}"));
|
||||
: Result.Fail(new Error($"Product price with ID {productPriceId} not found"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -38,17 +36,15 @@ public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> cont
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var product = await context.Products.FirstOrDefaultAsync(p => p.Id == productId, cancellationToken);
|
||||
var rowsUpdated = await context.Products
|
||||
.Where(p => p.Id == productId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(p => p.Enabled, isEnabled)
|
||||
.SetProperty(p => p.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (product is null)
|
||||
return Result.Fail(new Error($"Product with ID {productId} not found"));
|
||||
|
||||
product.UpdatedAt = DateTime.UtcNow;
|
||||
product.Enabled = isEnabled;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to change status of product with ID {productId}"));
|
||||
: Result.Fail(new Error($"Product with ID {productId} not found"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user