Files
components/LiteCharms.Features/Customers/Queries/Handlers/GetCustomerQueryHandler.cs
T
Khwezi Mngoma 83f51c6a23 Added notifications
Added shopping cart and items
Added quotes
Refactored relatinoships
Migrated changes
Refactored cqrs commands and queries
Refactored mappings
2026-05-05 23:59:31 +02:00

27 lines
975 B
C#

using LiteCharms.Extensions;
using LiteCharms.Infrastructure.Database;
using LiteCharms.Models;
namespace LiteCharms.Features.Customers.Queries.Handlers;
public class GetCustomerQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetCustomerQuery, Result<Customer>>
{
public async ValueTask<Result<Customer>> Handle(GetCustomerQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == request.CustomerId, cancellationToken);
return customer is not null
? Result.Ok(customer.ToModel())
: Result.Fail<Customer>($"Customer not found with id {request.CustomerId}");
}
catch (Exception ex)
{
return Result.Fail<Customer>(new Error(ex.Message).CausedBy(ex));
}
}
}