Wrote tests for most services, applied EF core optimisations
This commit is contained in:
@@ -13,7 +13,7 @@ public sealed class CustomerService(IDbContextFactory<MidrandBooksDbContext> con
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (await context.Customers.AnyAsync(c => c.Email!.Equals(request.Email, StringComparison.OrdinalIgnoreCase), cancellationToken))
|
||||
if (await context.Customers.AnyAsync(c => EF.Functions.ILike(c.Email!, $"%{request.Email}%"), cancellationToken))
|
||||
return Result.Fail<long>(new Error($"Customer with email '{request.Email}' already exists."));
|
||||
|
||||
var customer = context.Customers.Add(new Entities.Customer
|
||||
@@ -46,7 +46,7 @@ public sealed class CustomerService(IDbContextFactory<MidrandBooksDbContext> con
|
||||
if (!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken))
|
||||
return Result.Fail<long>(new Error($"Customer with ID '{customerId}' does not exist."));
|
||||
|
||||
if (await context.Contacts.AnyAsync(cc => cc.CustomerId == customerId && cc.Email!.Equals(request.Email, StringComparison.OrdinalIgnoreCase), cancellationToken))
|
||||
if (await context.Contacts.AnyAsync(cc => cc.CustomerId == customerId && EF.Functions.ILike(cc.Email!, $"%{request.Email}%"), cancellationToken))
|
||||
return Result.Fail<long>(new Error($"Contact with email '{request.Email}' already exists for this customer."));
|
||||
|
||||
var contact = context.Contacts.Add(new Entities.Contact
|
||||
@@ -139,21 +139,19 @@ public sealed class CustomerService(IDbContextFactory<MidrandBooksDbContext> con
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var contact = await context.Contacts.FirstOrDefaultAsync(cc => cc.Id == contactId, cancellationToken);
|
||||
var rowsUpdated = await context.Contacts
|
||||
.Where(cc => cc.Id == contactId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(cc => cc.Name, request.Name)
|
||||
.SetProperty(cc => cc.LastName, request.LastName)
|
||||
.SetProperty(cc => cc.Email, request.Email)
|
||||
.SetProperty(cc => cc.Phone, request.Phone)
|
||||
.SetProperty(cc => cc.Type, request.Type)
|
||||
.SetProperty(cc => cc.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (contact is null)
|
||||
return Result.Fail(new Error($"Contact with ID '{contactId}' does not exist."));
|
||||
|
||||
contact.UpdatedAt = DateTime.UtcNow;
|
||||
contact.Name = request.Name;
|
||||
contact.LastName = request.LastName;
|
||||
contact.Email = request.Email;
|
||||
contact.Phone = request.Phone;
|
||||
contact.Type = request.Type;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error("Failed to update customer contact."));
|
||||
: Result.Fail(new Error($"Contact with ID '{contactId}' does not exist."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -167,25 +165,23 @@ public sealed class CustomerService(IDbContextFactory<MidrandBooksDbContext> con
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var address = await context.Addresses.FirstOrDefaultAsync(a => a.Id == addressId, cancellationToken);
|
||||
var rowsUpdated = await context.Addresses
|
||||
.Where(a => a.Id == addressId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(a => a.Street, request.Street)
|
||||
.SetProperty(a => a.City, request.City)
|
||||
.SetProperty(a => a.State, request.State)
|
||||
.SetProperty(a => a.PostalCode, request.PostalCode)
|
||||
.SetProperty(a => a.Country, request.Country)
|
||||
.SetProperty(a => a.Type, request.Type)
|
||||
.SetProperty(a => a.BuildingType, request.BuildingType)
|
||||
.SetProperty(a => a.IsPrimary, request.IsPrimary)
|
||||
.SetProperty(a => a.Name, request.Name)
|
||||
.SetProperty(a => a.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (address is null)
|
||||
return Result.Fail(new Error($"Address with ID '{addressId}' does not exist."));
|
||||
|
||||
address.UpdatedAt = DateTime.UtcNow;
|
||||
address.Street = request.Street;
|
||||
address.City = request.City;
|
||||
address.State = request.State;
|
||||
address.PostalCode = request.PostalCode;
|
||||
address.Country = request.Country;
|
||||
address.Type = request.Type;
|
||||
address.BuildingType = request.BuildingType;
|
||||
address.IsPrimary = request.IsPrimary;
|
||||
address.Name = request.Name;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error("Failed to update customer address."));
|
||||
: Result.Fail(new Error($"Address with ID '{addressId}' does not exist."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -199,17 +195,15 @@ public sealed class CustomerService(IDbContextFactory<MidrandBooksDbContext> con
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == customerId, cancellationToken);
|
||||
var rowsUpdated = await context.Customers
|
||||
.Where(c => c.Id == customerId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(c => c.Enabled, enabled)
|
||||
.SetProperty(c => c.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (customer is null)
|
||||
return Result.Fail(new Error($"Customer with ID '{customerId}' does not exist."));
|
||||
|
||||
customer.Enabled = enabled;
|
||||
customer.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error("Failed to update customer status."));
|
||||
: Result.Fail(new Error($"Customer with ID '{customerId}' does not exist."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -223,18 +217,16 @@ public sealed class CustomerService(IDbContextFactory<MidrandBooksDbContext> con
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var contact = await context.Contacts.FirstOrDefaultAsync(cc => cc.Id == contactId, cancellationToken);
|
||||
var rowsUpdated = await context.Contacts
|
||||
.Where(cc => cc.Id == contactId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(cc => cc.Enabled, enabled)
|
||||
.SetProperty(cc => cc.IsPrimary, isPrimary)
|
||||
.SetProperty(cc => cc.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (contact is null)
|
||||
return Result.Fail(new Error($"Contact with ID '{contactId}' does not exist."));
|
||||
|
||||
contact.Enabled = enabled;
|
||||
contact.IsPrimary = isPrimary;
|
||||
contact.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error("Failed to update customer contact status."));
|
||||
: Result.Fail(new Error($"Contact with ID '{contactId}' does not exist."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -248,18 +240,16 @@ public sealed class CustomerService(IDbContextFactory<MidrandBooksDbContext> con
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var address = await context.Addresses.FirstOrDefaultAsync(a => a.Id == addressId, cancellationToken);
|
||||
var rowsUpdated = await context.Addresses
|
||||
.Where(a => a.Id == addressId)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(a => a.Enabled, enabled)
|
||||
.SetProperty(a => a.IsPrimary, isPrimary)
|
||||
.SetProperty(a => a.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||
|
||||
if (address is null)
|
||||
return Result.Fail(new Error($"Address with ID '{addressId}' does not exist."));
|
||||
|
||||
address.Enabled = enabled;
|
||||
address.IsPrimary = isPrimary;
|
||||
address.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
return rowsUpdated > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error("Failed to update customer address status."));
|
||||
: Result.Fail(new Error($"Address with ID '{addressId}' does not exist."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user