44 lines
1.8 KiB
C#
44 lines
1.8 KiB
C#
using LiteCharms.Infrastructure.Database;
|
|
|
|
namespace LiteCharms.Features.Customers.Commands.Handlers;
|
|
|
|
public class UpdateCustomerCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateCustomerCommand, Result>
|
|
{
|
|
public async ValueTask<Result> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == request.CustomerId, cancellationToken);
|
|
|
|
if (customer is null)
|
|
return Result.Fail(new Error($"Customer with ID {request.CustomerId} not found."));
|
|
|
|
customer.Name = request.Name;
|
|
customer.LastName = request.LastName;
|
|
customer.Email = request.Email;
|
|
customer.Company = request.Company;
|
|
customer.Address = request.Address;
|
|
customer.City = request.City;
|
|
customer.Region = request.Region;
|
|
customer.Country = request.Country;
|
|
customer.PostalCode = request.PostalCode;
|
|
customer.Phone = request.Phone;
|
|
customer.Tax = request.Tax;
|
|
customer.City = request.City;
|
|
customer.Discord = request.Discord;
|
|
customer.Slack = request.Slack;
|
|
customer.LinkedIn = request.LinkedIn;
|
|
customer.Whatsapp = request.Whatsapp;
|
|
|
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
|
? Result.Ok()
|
|
: Result.Fail(new Error($"Failed to update the customer {request.CustomerId}."));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
} |