Added notifications
Added shopping cart and items Added quotes Refactored relatinoships Migrated changes Refactored cqrs commands and queries Refactored mappings
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries;
|
||||
|
||||
public class GetCustomerQuotesQuery : IRequest<Result<Quote[]>>
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
|
||||
private GetCustomerQuotesQuery(Guid customerId) => CustomerId = customerId;
|
||||
|
||||
public static GetCustomerQuotesQuery Create(Guid customerId)
|
||||
{
|
||||
if (customerId == Guid.Empty)
|
||||
throw new ArgumentException("CustomerId is required.");
|
||||
|
||||
return new(customerId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries;
|
||||
|
||||
public class GetQuoteQuery : IRequest<Result<Quote>>
|
||||
{
|
||||
public Guid QuoteId { get; set; }
|
||||
|
||||
private GetQuoteQuery(Guid quoteId) => QuoteId = quoteId;
|
||||
|
||||
public static GetQuoteQuery Create(Guid quoteId)
|
||||
{
|
||||
if(quoteId == Guid.Empty)
|
||||
throw new ArgumentException("Quote ID is required.", nameof(quoteId));
|
||||
|
||||
return new(quoteId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries;
|
||||
|
||||
public class GetQuotesQuery : IRequest<Result<Quote[]>>
|
||||
{
|
||||
public DateOnly From { get; set; }
|
||||
|
||||
public DateOnly To { get; set; }
|
||||
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetQuotesQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
MaxRecords = maxRecords;
|
||||
}
|
||||
|
||||
public static GetQuotesQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
if (from > to)
|
||||
throw new ArgumentException("From date cannot be greater than To date.");
|
||||
|
||||
if (maxRecords <= 0)
|
||||
throw new ArgumentException("MaxRecords must be a positive integer.");
|
||||
|
||||
return new(from, to, maxRecords);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetQuoteQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetQuoteQuery, Result<Quote>>
|
||||
{
|
||||
public async ValueTask<Result<Quote>> Handle(GetQuoteQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var quote = await context.Quotes.AsNoTracking().FirstOrDefaultAsync(q => q.Id == request.QuoteId, cancellationToken);
|
||||
|
||||
return quote is not null
|
||||
? Result.Ok(quote.ToModel())
|
||||
: Result.Fail<Quote>(new Error($"Quote with ID {request.QuoteId} not found."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Quote>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetQuotesHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetQuotesQuery, Result<Quote[]>>
|
||||
{
|
||||
public async ValueTask<Result<Quote[]>> Handle(GetQuotesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fromDate = request.From.ToDateTime(TimeOnly.MinValue);
|
||||
var toDate = request.To.ToDateTime(TimeOnly.MaxValue);
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var quotes = await context.Quotes.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return quotes?.Length > 0
|
||||
? Result.Ok(quotes.Select(o => o.ToModel()).ToArray())
|
||||
: Result.Fail<Quote[]>(new Error($"No quotes found for the specified date range {request.From} - {request.To}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Quote[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user