Added support for notifications
This commit is contained in:
@@ -8,17 +8,23 @@ public class GetCustomersQuery : IRequest<Result<Customer[]>>
|
||||
|
||||
public DateOnly To { get; set; }
|
||||
|
||||
private GetCustomersQuery(DateOnly from, DateOnly to)
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetCustomersQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
MaxRecords = maxRecords;
|
||||
}
|
||||
|
||||
public static GetCustomersQuery Create(DateOnly from, DateOnly to)
|
||||
public static GetCustomersQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
if (from > to)
|
||||
throw new ArgumentException("From date cannot be greater than To date.");
|
||||
|
||||
return new(from, to);
|
||||
if(maxRecords <= 0)
|
||||
throw new ArgumentException("MaxRecords must be a positive integer.");
|
||||
|
||||
return new(from, to, maxRecords);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ public class GetCustomersQueryHandler(IDbContextFactory<LeadGeneratorDbContext>
|
||||
var customers = await context.Customers.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(c => c.CreatedAt >= fromDate && c.CreatedAt <= toDate)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return customers?.Length > 0
|
||||
|
||||
@@ -8,17 +8,23 @@ public class GetLeadsQuery : IRequest<Result<Lead[]>>
|
||||
|
||||
public DateOnly To { get; set; }
|
||||
|
||||
private GetLeadsQuery(DateOnly from, DateOnly to)
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetLeadsQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
MaxRecords = maxRecords;
|
||||
}
|
||||
|
||||
public static GetLeadsQuery Create(DateOnly from, DateOnly to)
|
||||
public static GetLeadsQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
if (from > to)
|
||||
throw new ArgumentException("From date cannot be greater than To date.");
|
||||
|
||||
return new(from, to);
|
||||
if(maxRecords <= 0)
|
||||
throw new ArgumentException("MaxRecords must be a positive integer.");
|
||||
|
||||
return new(from, to, maxRecords);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ public class GetLeadsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> cont
|
||||
var leads = await context.Leads.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(l => l.CreatedAt.Date >= fromDate && l.CreatedAt.Date <= toDate)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return leads?.Length > 0
|
||||
|
||||
@@ -34,8 +34,8 @@
|
||||
<PackageReference Include="MimeKit" Version="4.16.0" />
|
||||
|
||||
<!-- Global Usings-->
|
||||
<Using Include="MimeKit"/>
|
||||
<Using Include="MailKit.Net.Smtp"/>
|
||||
<Using Include="MimeKit" />
|
||||
<Using Include="MailKit.Net.Smtp" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- CQRS -->
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Queries;
|
||||
|
||||
public class GetNotificationsQuery : IRequest<Result<Notification[]>>
|
||||
{
|
||||
public DateOnly From { get; set; }
|
||||
|
||||
public DateOnly To { get; set; }
|
||||
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetNotificationsQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
MaxRecords = maxRecords;
|
||||
}
|
||||
|
||||
public static GetNotificationsQuery 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.", nameof(maxRecords));
|
||||
|
||||
return new(from, to, maxRecords);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Queries.Handlers;
|
||||
|
||||
public class GetNotificationsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetNotificationsQuery, Result<Notification[]>>
|
||||
{
|
||||
public async ValueTask<Result<Notification[]>> Handle(GetNotificationsQuery 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 notifications = await context.Notifications
|
||||
.Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate)
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return notifications?.Length > 0
|
||||
? Result.Ok(notifications.Select(n => n.ToModel()).ToArray())
|
||||
: Result.Fail(new Error($"No notifications found for the specified date range {request.From} to {request.To}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,17 +8,23 @@ public class GetOrdersQuery : IRequest<Result<Order[]>>
|
||||
|
||||
public DateOnly To { get; set; }
|
||||
|
||||
private GetOrdersQuery(DateOnly from, DateOnly to)
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetOrdersQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
MaxRecords = maxRecords;
|
||||
}
|
||||
|
||||
public static GetOrdersQuery Create(DateOnly from, DateOnly to)
|
||||
public static GetOrdersQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
if (from > to)
|
||||
throw new ArgumentException("From date cannot be greater than To date.");
|
||||
|
||||
return new(from, to);
|
||||
if(maxRecords <= 0)
|
||||
throw new ArgumentException("MaxRecords must be a positive integer.");
|
||||
|
||||
return new(from, to, maxRecords);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ public class GetOrdersQueryHandler(IDbContextFactory<LeadGeneratorDbContext> con
|
||||
var orders = await context.Orders
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return orders?.Length > 0
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Products.Queries;
|
||||
|
||||
public class GetProductPricesQuery : IRequest<Result<ProductPrice[]>>
|
||||
{
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetProductPricesQuery(int maxRecords = 1000) => MaxRecords = maxRecords;
|
||||
|
||||
public static GetProductPricesQuery Create(int maxRecords = 1000)
|
||||
{
|
||||
if (maxRecords <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(maxRecords), "MaxRecords must be greater than zero.");
|
||||
|
||||
return new(maxRecords);
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,15 @@ namespace LiteCharms.Features.Products.Queries;
|
||||
|
||||
public class GetProductsQuery : IRequest<Result<Product[]>>
|
||||
{
|
||||
public static GetProductsQuery Create() => new();
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetProductsQuery(int maxRecords = 1000) => MaxRecords = maxRecords;
|
||||
|
||||
public static GetProductsQuery Create(int maxRecords = 1000)
|
||||
{
|
||||
if (maxRecords <= 0)
|
||||
throw new ArgumentException("MaxRecords must be a positive integer.");
|
||||
|
||||
return new(maxRecords);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Products.Queries.Handlers;
|
||||
|
||||
public class GetProductPricesQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetProductPricesQuery, Result<ProductPrice[]>>
|
||||
{
|
||||
public async ValueTask<Result<ProductPrice[]>> Handle(GetProductPricesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var products = await context.ProductPrices.AsNoTracking()
|
||||
.OrderByDescending(o => o.Id)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return Result.Ok(products.Select(p => p.ToModel()).ToArray());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ProductPrice[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ public class GetProductsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> c
|
||||
|
||||
var products = await context.Products.AsNoTracking()
|
||||
.OrderByDescending(o => o.Id)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return Result.Ok(products.Select(p => p.ToModel()).ToArray());
|
||||
|
||||
Reference in New Issue
Block a user