83f51c6a23
Added shopping cart and items Added quotes Refactored relatinoships Migrated changes Refactored cqrs commands and queries Refactored mappings
32 lines
1.3 KiB
C#
32 lines
1.3 KiB
C#
using LiteCharms.Extensions;
|
|
using LiteCharms.Features.Quotes.Queries;
|
|
using LiteCharms.Infrastructure.Database;
|
|
using LiteCharms.Models;
|
|
|
|
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
|
|
|
public class GetCustomerQuotesQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetCustomerQuotesQuery, Result<Quote[]>>
|
|
{
|
|
public async ValueTask<Result<Quote[]>> Handle(GetCustomerQuotesQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
if (!await context.Customers.AnyAsync(c => c.Id == request.CustomerId, cancellationToken))
|
|
return Result.Fail<Quote[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
|
|
|
|
var quotes = await context.Quotes.AsNoTracking()
|
|
.Where(q => q.CustomerId == request.CustomerId).ToArrayAsync(cancellationToken);
|
|
|
|
return quotes?.Length > 0
|
|
? Result.Ok(quotes.Select(q => q.ToModel()).ToArray())
|
|
: Result.Fail<Quote[]>(new Error($"No quotes found for customer with Id {request.CustomerId}."));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<Quote[]>(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
}
|