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:
Khwezi Mngoma
2026-05-05 23:59:31 +02:00
parent 4b822c63b2
commit 83f51c6a23
67 changed files with 3051 additions and 42 deletions
@@ -0,0 +1,63 @@
using LiteCharms.Models;
namespace LiteCharms.Features.Notifications.Commands;
public class CreateNotificationCommand : IRequest<Result<Guid>>
{
public NotificationDirection Direction { get; set; }
public string? Author { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public string? Platform { get; set; }
public string? PlatformAddress { get; set; }
public string? CorrelationId { get; set; }
public string? CorrelationIdType { get; set; }
public bool IsInternal { get; set; }
private CreateNotificationCommand(NotificationDirection direction, string author, string title, string description, string platform, string platformAddress, string correlationId, string correlationIdType, bool isInternal)
{
Direction = direction;
Author = author;
Title = title;
Description = description;
Platform = platform;
PlatformAddress = platformAddress;
CorrelationId = correlationId;
CorrelationIdType = correlationIdType;
IsInternal = isInternal;
}
public static CreateNotificationCommand Create(NotificationDirection direction, string author, string title, string description, string platform, string platformAddress, string correlationId, string correlationIdType, bool isInternal)
{
if (string.IsNullOrWhiteSpace(author))
throw new ArgumentException("Author cannot be null or whitespace.", nameof(author));
if (string.IsNullOrWhiteSpace(title))
throw new ArgumentException("Title cannot be null or whitespace.", nameof(title));
if (string.IsNullOrWhiteSpace(description))
throw new ArgumentException("Description cannot be null or whitespace.", nameof(description));
if (string.IsNullOrWhiteSpace(platform))
throw new ArgumentException("Platform cannot be null or whitespace.", nameof(platform));
if (string.IsNullOrWhiteSpace(platformAddress))
throw new ArgumentException("PlatformAddress cannot be null or whitespace.", nameof(platformAddress));
if (string.IsNullOrWhiteSpace(correlationId))
throw new ArgumentException("CorrelationId cannot be null or whitespace.", nameof(correlationId));
if (string.IsNullOrWhiteSpace(correlationIdType))
throw new ArgumentException("CorrelationIdType cannot be null or whitespace.", nameof(correlationIdType));
return new(direction, author, title, description, platform, platformAddress, correlationId, correlationIdType, isInternal);
}
}
@@ -0,0 +1,35 @@
using LiteCharms.Infrastructure.Database;
namespace LiteCharms.Features.Notifications.Commands.Handlers;
public class CreateNotificationCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<CreateNotificationCommand, Result<Guid>>
{
public async ValueTask<Result<Guid>> Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var newNotification = context.Notifications.Add(new Entities.Notification
{
Direction = request.Direction,
Author = request.Author,
Title = request.Title,
Description = request.Description,
Platform = request.Platform,
PlatformAddress = request.PlatformAddress,
CorrelationId = request.CorrelationId,
CorrelationIdType = request.CorrelationIdType,
IsInternal = request.IsInternal,
});
return newNotification is not null
? Result.Ok(newNotification.Entity.Id)
: Result.Fail(new Error("Failed to create notification"));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,29 @@
using LiteCharms.Infrastructure.Database;
namespace LiteCharms.Features.Notifications.Commands.Handlers;
public class UpdateNotificationCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<UpdateNotificationCommand, Result>
{
public async ValueTask<Result> Handle(UpdateNotificationCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == request.NotificationId, cancellationToken);
if(notification is null)
return Result.Fail(new Error($"Notification with id {request.NotificationId} not found."));
notification.Processed = request.Processed;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error($"Failed to update notification with id {request.NotificationId}."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,22 @@
namespace LiteCharms.Features.Notifications.Commands;
public class UpdateNotificationCommand : IRequest<Result>
{
public Guid NotificationId { get; set; }
public bool Processed { get; set; }
private UpdateNotificationCommand(Guid notificationId, bool processed)
{
NotificationId = notificationId;
Processed = processed;
}
public static UpdateNotificationCommand Create(Guid notificationId, bool processed)
{
if(notificationId == Guid.Empty)
throw new ArgumentException("Notification ID cannot be empty.", nameof(notificationId));
return new(notificationId, processed);
}
}
@@ -0,0 +1,18 @@
using LiteCharms.Models;
namespace LiteCharms.Features.Notifications.Queries;
public class GetNotificationQuery : IRequest<Result<Notification>>
{
public Guid NotificationId { get; set; }
private GetNotificationQuery(Guid notificationId) => NotificationId = notificationId;
public static GetNotificationQuery Create(Guid notificationId)
{
if (notificationId == Guid.Empty)
throw new ArgumentException("Notification ID is required.", nameof(notificationId));
return new(notificationId);
}
}
@@ -0,0 +1,26 @@
using LiteCharms.Extensions;
using LiteCharms.Infrastructure.Database;
using LiteCharms.Models;
namespace LiteCharms.Features.Notifications.Queries.Handlers;
public class GetNotificationQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetNotificationQuery, Result<Notification>>
{
public async ValueTask<Result<Notification>> Handle(GetNotificationQuery request, CancellationToken cancellationToken)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notification = await context.Notifications.FindAsync(new object[] { request.NotificationId }, cancellationToken);
return notification is not null
? Result.Ok(notification.ToModel())
: Result.Fail<Notification>(new Error($"Notification with id {request.NotificationId} not found"));
}
catch (Exception ex)
{
return Result.Fail<Notification>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -15,7 +15,7 @@ public class GetNotificationsQueryHandler(IDbContextFactory<LeadGeneratorDbConte
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notifications = await context.Notifications
var notifications = await context.Notifications.AsNoTracking()
.Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate)
.OrderByDescending(n => n.CreatedAt)
.Take(request.MaxRecords)