Added package management
This commit is contained in:
@@ -12,4 +12,6 @@ public class ShoppingCart : Models.ShoppingCart
|
|||||||
public virtual Quote? Quote { get; set; }
|
public virtual Quote? Quote { get; set; }
|
||||||
|
|
||||||
public virtual ICollection<ShoppingCartItem>? ShoppingCartItems { get; set; }
|
public virtual ICollection<ShoppingCartItem>? ShoppingCartItems { get; set; }
|
||||||
|
|
||||||
|
public virtual ICollection<Package>? Packages { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,36 @@ namespace LiteCharms.Extensions;
|
|||||||
|
|
||||||
public static class EntityModeMappers
|
public static class EntityModeMappers
|
||||||
{
|
{
|
||||||
|
public static ShoppingCartPackage ToModel(this Entities.ShoppingCartPackage entity) =>
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = entity.Id,
|
||||||
|
CreatedAt = entity.CreatedAt,
|
||||||
|
PackageId = entity.PackageId,
|
||||||
|
ShoppingCartId = entity.ShoppingCartId
|
||||||
|
};
|
||||||
|
|
||||||
|
public static PackageItem ToModel(this Entities.PackageItem entity) =>
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = entity.Id,
|
||||||
|
Active = entity.Active,
|
||||||
|
CreatedAt = entity.CreatedAt,
|
||||||
|
PackageId = entity.PackageId,
|
||||||
|
ProductPriceId = entity.ProductPriceId
|
||||||
|
};
|
||||||
|
|
||||||
|
public static Package ToModel(this Entities.Package entity) =>
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = entity.Id,
|
||||||
|
CreatedAt = entity.CreatedAt,
|
||||||
|
Active = entity.Active,
|
||||||
|
Description = entity.Description,
|
||||||
|
Name = entity.Name,
|
||||||
|
UpdatedAt = entity.UpdatedAt
|
||||||
|
};
|
||||||
|
|
||||||
public static ShoppingCartItem ToModel(this Entities.ShoppingCartItem entity) =>
|
public static ShoppingCartItem ToModel(this Entities.ShoppingCartItem entity) =>
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
@@ -36,7 +66,7 @@ public static class EntityModeMappers
|
|||||||
ExpiredAt = entity.ExpiredAt,
|
ExpiredAt = entity.ExpiredAt,
|
||||||
Reason = entity.Reason,
|
Reason = entity.Reason,
|
||||||
ShoppingCartId = entity.ShoppingCartId,
|
ShoppingCartId = entity.ShoppingCartId,
|
||||||
Status = entity.Status
|
Status = entity.Status
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Notification ToModel(this Entities.Notification entity) =>
|
public static Notification ToModel(this Entities.Notification entity) =>
|
||||||
@@ -53,7 +83,12 @@ public static class EntityModeMappers
|
|||||||
Platform = entity.Platform,
|
Platform = entity.Platform,
|
||||||
Recipient = entity.Recipient,
|
Recipient = entity.Recipient,
|
||||||
Subject = entity.Subject,
|
Subject = entity.Subject,
|
||||||
Processed = entity.Processed
|
Processed = entity.Processed,
|
||||||
|
SenderName = entity.SenderName,
|
||||||
|
RecipientAddress = entity.RecipientAddress,
|
||||||
|
Priority = entity.Priority,
|
||||||
|
UpdatedAt = entity?.UpdatedAt,
|
||||||
|
IsHtml = entity!.IsHtml
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Customer ToModel(this Entities.Customer entity) =>
|
public static Customer ToModel(this Entities.Customer entity) =>
|
||||||
@@ -78,7 +113,7 @@ public static class EntityModeMappers
|
|||||||
Slack = entity.Slack,
|
Slack = entity.Slack,
|
||||||
Tax = entity.Tax,
|
Tax = entity.Tax,
|
||||||
Website = entity.Website,
|
Website = entity.Website,
|
||||||
Whatsapp = entity.Whatsapp
|
Whatsapp = entity.Whatsapp
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Lead ToModel(this Entities.Lead entity) =>
|
public static Lead ToModel(this Entities.Lead entity) =>
|
||||||
@@ -113,7 +148,10 @@ public static class EntityModeMappers
|
|||||||
RefundId = entity.RefundId,
|
RefundId = entity.RefundId,
|
||||||
QuoteId = entity.QuoteId,
|
QuoteId = entity.QuoteId,
|
||||||
Status = entity.Status,
|
Status = entity.Status,
|
||||||
ShoppingCartId = entity.ShoppingCartId
|
ShoppingCartId = entity.ShoppingCartId,
|
||||||
|
DepositRequired = entity.DepositRequired,
|
||||||
|
Requirements = entity.Requirements,
|
||||||
|
Terms = entity.Terms
|
||||||
};
|
};
|
||||||
|
|
||||||
public static OrderRefund ToModel(this Entities.OrderRefund entity) =>
|
public static OrderRefund ToModel(this Entities.OrderRefund entity) =>
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace LiteCharms.Features.CartPackages.Commands;
|
||||||
|
|
||||||
|
public class AddPackageItemCommand : IRequest<Result<Guid>>
|
||||||
|
{
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
public Guid ProductPriceId { get; set; }
|
||||||
|
|
||||||
|
private AddPackageItemCommand(Guid packageId, Guid productPriceId)
|
||||||
|
{
|
||||||
|
PackageId = packageId;
|
||||||
|
ProductPriceId = productPriceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AddPackageItemCommand Create(Guid packageId, Guid productPriceId)
|
||||||
|
{
|
||||||
|
if (packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Package id is required", nameof(packageId));
|
||||||
|
|
||||||
|
if (productPriceId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Product price id is required", nameof(productPriceId));
|
||||||
|
|
||||||
|
return new(packageId, productPriceId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
namespace LiteCharms.Features.CartPackages.Commands;
|
||||||
|
|
||||||
|
public class CreatePackageCommand : IRequest<Result<Guid>>
|
||||||
|
{
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
public string? Description { get; set; }
|
||||||
|
|
||||||
|
private CreatePackageCommand(string? name, string? description)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
Description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CreatePackageCommand Create(string? name, string? description)
|
||||||
|
{
|
||||||
|
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
|
||||||
|
|
||||||
|
ArgumentException.ThrowIfNullOrWhiteSpace(description, nameof(description));
|
||||||
|
|
||||||
|
return new(name, description);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace LiteCharms.Features.CartPackages.Commands;
|
||||||
|
|
||||||
|
public class DeletePackageItemCommand : IRequest<Result>
|
||||||
|
{
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
public Guid PackageItemId { get; set; }
|
||||||
|
|
||||||
|
private DeletePackageItemCommand(Guid packageId, Guid packageItemId)
|
||||||
|
{
|
||||||
|
PackageId = packageId;
|
||||||
|
PackageItemId = packageItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DeletePackageItemCommand Create(Guid packageId, Guid packageItemId)
|
||||||
|
{
|
||||||
|
if (packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Package id is required", nameof(packageId));
|
||||||
|
|
||||||
|
if (packageItemId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Product price id is required", nameof(packageItemId));
|
||||||
|
|
||||||
|
return new(packageId, packageItemId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
namespace LiteCharms.Features.CartPackages.Commands;
|
||||||
|
|
||||||
|
public class DeletePackageItemsCommand : IRequest<Result>
|
||||||
|
{
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
private DeletePackageItemsCommand(Guid packageId) => PackageId = packageId;
|
||||||
|
|
||||||
|
public static DeletePackageItemsCommand Create(Guid packageId)
|
||||||
|
{
|
||||||
|
if (packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Package ID is required", nameof(packageId));
|
||||||
|
|
||||||
|
return new(packageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Commands.Handlers;
|
||||||
|
|
||||||
|
public class AddPackageItemCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<AddPackageItemCommand, Result<Guid>>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result<Guid>> Handle(AddPackageItemCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken))
|
||||||
|
return Result.Fail($"Could not find package by ID {request.PackageId}");
|
||||||
|
|
||||||
|
if (!await context.ProductPrices.AnyAsync(p => p.Id == request.ProductPriceId && p.Active == true, cancellationToken))
|
||||||
|
return Result.Fail($"Could not find an active product price by ID {request.ProductPriceId}");
|
||||||
|
|
||||||
|
if (await context.PackageItems.AnyAsync(p => p.ProductPriceId == request.ProductPriceId && p.PackageId == request.PackageId, cancellationToken))
|
||||||
|
return Result.Fail<Guid>($"Product price {request.ProductPriceId} is already added to this package {request.PackageId}");
|
||||||
|
|
||||||
|
var newPackageItem = context.PackageItems.Add(new Entities.PackageItem
|
||||||
|
{
|
||||||
|
PackageId = request.PackageId,
|
||||||
|
ProductPriceId = request.ProductPriceId,
|
||||||
|
Active = true
|
||||||
|
});
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok(newPackageItem.Entity.Id)
|
||||||
|
: Result.Fail<Guid>($"Failed to add new package item by ID {request.ProductPriceId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Commands.Handlers;
|
||||||
|
|
||||||
|
public class CreatePackageCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<CreatePackageCommand, Result<Guid>>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result<Guid>> Handle(CreatePackageCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (await context.Packages.AnyAsync(p => p.Name == request.Name, cancellationToken))
|
||||||
|
return Result.Fail($"A package by the same name already exists: {request.Name}");
|
||||||
|
|
||||||
|
var newPackage = context.Packages.Add(new Entities.Package
|
||||||
|
{
|
||||||
|
Name = request.Name,
|
||||||
|
Description = request.Description,
|
||||||
|
Active = true
|
||||||
|
});
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok(newPackage.Entity.Id)
|
||||||
|
: Result.Fail($"Failed to create a new package by the name: {request.Name}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Commands.Handlers;
|
||||||
|
|
||||||
|
public class DeletePackageItemCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<DeletePackageItemCommand, Result>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result> Handle(DeletePackageItemCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken))
|
||||||
|
return Result.Fail($"Could not find package by ID {request.PackageId}");
|
||||||
|
|
||||||
|
var item = await context.PackageItems.FirstOrDefaultAsync(p => p.Id == request.PackageItemId && p.PackageId == request.PackageId, cancellationToken);
|
||||||
|
|
||||||
|
if(item is null)
|
||||||
|
return Result.Fail($"Product item {request.PackageItemId} is already added to this package {request.PackageId}");
|
||||||
|
|
||||||
|
context.PackageItems.Remove(item);
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok()
|
||||||
|
: Result.Fail($"Failed to delete package item by id {request.PackageItemId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Commands.Handlers;
|
||||||
|
|
||||||
|
public class DeletePackageItemsCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<DeletePackageItemsCommand, Result>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result> Handle(DeletePackageItemsCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken))
|
||||||
|
return Result.Fail($"Could not find package by ID {request.PackageId}");
|
||||||
|
|
||||||
|
var items = await context.PackageItems.Where(i => i.PackageId == request.PackageId).ToArrayAsync(cancellationToken);
|
||||||
|
|
||||||
|
context.PackageItems.RemoveRange(items);
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok()
|
||||||
|
: Result.Fail($"Failed to delete package {request.PackageId} items");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Commands.Handlers;
|
||||||
|
|
||||||
|
public class UpdatePackageCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdatePackageCommand, Result>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result> Handle(UpdatePackageCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (await context.Packages.AnyAsync(p => p.Name == request.Name, cancellationToken))
|
||||||
|
return Result.Fail($"A package by the same name already exists: {request.Name}");
|
||||||
|
|
||||||
|
var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken);
|
||||||
|
|
||||||
|
if (package is null)
|
||||||
|
return Result.Fail($"Could not find package by id {request.PackageId}");
|
||||||
|
|
||||||
|
package.Name = request.Name;
|
||||||
|
package.Description = request.Description;
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok()
|
||||||
|
: Result.Fail($"Failed to update package with id {request.PackageId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Commands.Handlers;
|
||||||
|
|
||||||
|
public class UpdatePackageStatusCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdatePackageStatusCommand, Result>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result> Handle(UpdatePackageStatusCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken);
|
||||||
|
|
||||||
|
if (package is null)
|
||||||
|
return Result.Fail($"Could not find package by id {request.PackageId}");
|
||||||
|
|
||||||
|
package.Active = request.Active;
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok()
|
||||||
|
: Result.Fail($"Failed to update package with id {request.PackageId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
namespace LiteCharms.Features.CartPackages.Commands;
|
||||||
|
|
||||||
|
public class UpdatePackageCommand : IRequest<Result>
|
||||||
|
{
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
public string? Description { get; set; }
|
||||||
|
|
||||||
|
private UpdatePackageCommand(Guid packageId, string? name, string? description)
|
||||||
|
{
|
||||||
|
PackageId = packageId;
|
||||||
|
Name = name;
|
||||||
|
Description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UpdatePackageCommand Create(Guid packageId, string? name, string? description)
|
||||||
|
{
|
||||||
|
if (packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException($"Package ID is required", nameof(packageId));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNullOrWhiteSpace(name, nameof(name));
|
||||||
|
ArgumentNullException.ThrowIfNullOrWhiteSpace(description, nameof(description));
|
||||||
|
|
||||||
|
return new(packageId, name, description);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
namespace LiteCharms.Features.CartPackages.Commands;
|
||||||
|
|
||||||
|
public class UpdatePackageStatusCommand : IRequest<Result>
|
||||||
|
{
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
public bool Active { get; set; }
|
||||||
|
|
||||||
|
private UpdatePackageStatusCommand(Guid packageId, bool active)
|
||||||
|
{
|
||||||
|
PackageId = packageId;
|
||||||
|
Active = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UpdatePackageStatusCommand Create(Guid packageId, bool active)
|
||||||
|
{
|
||||||
|
if(packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException($"Package id is required", nameof(packageId));
|
||||||
|
|
||||||
|
return new(packageId, active);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Queries;
|
||||||
|
|
||||||
|
public class GetPackageItemsQuery : IRequest<Result<PackageItem[]>>
|
||||||
|
{
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
private GetPackageItemsQuery(Guid packageId) => PackageId = packageId;
|
||||||
|
|
||||||
|
public static GetPackageItemsQuery Create(Guid packageId)
|
||||||
|
{
|
||||||
|
if (packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Package ID is required", nameof(packageId));
|
||||||
|
|
||||||
|
return new(packageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Queries;
|
||||||
|
|
||||||
|
public class GetPackageQuery : IRequest<Result<Package>>
|
||||||
|
{
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
private GetPackageQuery(Guid packageId) => PackageId = packageId;
|
||||||
|
|
||||||
|
public static GetPackageQuery Create(Guid packageId)
|
||||||
|
{
|
||||||
|
if(packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Package ID is required", nameof(packageId));
|
||||||
|
|
||||||
|
return new(packageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Queries;
|
||||||
|
|
||||||
|
public class GetPackagesQuery : IRequest<Result<Package[]>>
|
||||||
|
{
|
||||||
|
public DateOnly From { get; set; }
|
||||||
|
|
||||||
|
public DateOnly To { get; set; }
|
||||||
|
|
||||||
|
public int MaxRecords { get; set; }
|
||||||
|
|
||||||
|
public bool Active { get; set; }
|
||||||
|
|
||||||
|
private GetPackagesQuery(DateOnly from, DateOnly to, int maxRecords = 1000, bool active = true)
|
||||||
|
{
|
||||||
|
From = from;
|
||||||
|
To = to;
|
||||||
|
MaxRecords = maxRecords;
|
||||||
|
Active = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GetPackagesQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000, bool active = true)
|
||||||
|
{
|
||||||
|
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, active);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using LiteCharms.Extensions;
|
||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Queries.Handlers;
|
||||||
|
|
||||||
|
public class GetPackageItemsQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetPackageItemsQuery, Result<PackageItem[]>>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result<PackageItem[]>> Handle(GetPackageItemsQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken))
|
||||||
|
return Result.Fail<PackageItem[]>($"Package could not be found with ID {request.PackageId}");
|
||||||
|
|
||||||
|
var items = await context.PackageItems.AsNoTracking()
|
||||||
|
.OrderByDescending(o => o.CreatedAt)
|
||||||
|
.Where(p => p.PackageId == request.PackageId)
|
||||||
|
.ToArrayAsync(cancellationToken);
|
||||||
|
|
||||||
|
return items?.Length > 0
|
||||||
|
? Result.Ok(items.Select(i => i.ToModel()).ToArray())
|
||||||
|
: Result.Fail<PackageItem[]>($"Could not find package items by package ID {request.PackageId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail<PackageItem[]>(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using LiteCharms.Extensions;
|
||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Queries.Handlers;
|
||||||
|
|
||||||
|
public class GetPackageQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetPackageQuery, Result<Package>>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result<Package>> Handle(GetPackageQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken);
|
||||||
|
|
||||||
|
return package is not null
|
||||||
|
? Result.Ok(package.ToModel())
|
||||||
|
: Result.Fail($"Failed to find package by ID {request.PackageId}");
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail<Package>(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using LiteCharms.Extensions;
|
||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.CartPackages.Queries.Handlers;
|
||||||
|
|
||||||
|
public class GetPackagesQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetPackagesQuery, Result<Package[]>>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result<Package[]>> Handle(GetPackagesQuery 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 packages = await context.Packages
|
||||||
|
.AsNoTracking()
|
||||||
|
.OrderByDescending(o => o.CreatedAt)
|
||||||
|
.Where(p => p.CreatedAt >= fromDate && p.CreatedAt <= toDate)
|
||||||
|
.Where(p => p.Active == request.Active)
|
||||||
|
.Take(request.MaxRecords)
|
||||||
|
.ToArrayAsync(cancellationToken);
|
||||||
|
|
||||||
|
return packages?.Length > 0
|
||||||
|
? Result.Ok(packages.Select(o => o.ToModel()).ToArray())
|
||||||
|
: Result.Fail<Package[]>(new Error($"No packages found for the specified date range {request.From} - {request.To}."));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail<Package[]>(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,15 +6,21 @@ public class CreateNotificationCommand : IRequest<Result<Guid>>
|
|||||||
{
|
{
|
||||||
public NotificationDirection Direction { get; set; }
|
public NotificationDirection Direction { get; set; }
|
||||||
|
|
||||||
public string? Author { get; set; }
|
public string? Sender { get; set; }
|
||||||
|
|
||||||
public string? Title { get; set; }
|
public string? SenderAddress { get; set; }
|
||||||
|
|
||||||
public string? Description { get; set; }
|
public string? Subject { get; set; }
|
||||||
|
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
public NotificationPlatforms Platform { get; set; }
|
public NotificationPlatforms Platform { get; set; }
|
||||||
|
|
||||||
public string? PlatformAddress { get; set; }
|
public Priorities Priority { get; set; }
|
||||||
|
|
||||||
|
public string? Recipient { get; set; }
|
||||||
|
|
||||||
|
public string? RecipientAddress { get; set; }
|
||||||
|
|
||||||
public string? CorrelationId { get; set; }
|
public string? CorrelationId { get; set; }
|
||||||
|
|
||||||
@@ -22,39 +28,48 @@ public class CreateNotificationCommand : IRequest<Result<Guid>>
|
|||||||
|
|
||||||
public bool IsInternal { get; set; }
|
public bool IsInternal { get; set; }
|
||||||
|
|
||||||
private CreateNotificationCommand(NotificationDirection direction, string author, string title, string description, NotificationPlatforms platform, string platformAddress, string correlationId, string correlationIdType, bool isInternal)
|
public bool IsHtml { get; set; }
|
||||||
|
|
||||||
|
private CreateNotificationCommand(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, string correlationIdType, bool isInternal, bool isHtml = false)
|
||||||
{
|
{
|
||||||
Direction = direction;
|
Direction = direction;
|
||||||
Author = author;
|
Sender = sender;
|
||||||
Title = title;
|
SenderAddress = senderAddress;
|
||||||
Description = description;
|
Subject = subject;
|
||||||
|
Message = message;
|
||||||
Platform = platform;
|
Platform = platform;
|
||||||
PlatformAddress = platformAddress;
|
Priority = priority;
|
||||||
|
Recipient = recipient;
|
||||||
|
RecipientAddress = recipientAddress;
|
||||||
CorrelationId = correlationId;
|
CorrelationId = correlationId;
|
||||||
CorrelationIdType = correlationIdType;
|
CorrelationIdType = correlationIdType;
|
||||||
IsInternal = isInternal;
|
IsInternal = isInternal;
|
||||||
|
IsHtml = isHtml;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CreateNotificationCommand Create(NotificationDirection direction, string author, string title, string description, NotificationPlatforms platform, string platformAddress, string correlationId, string correlationIdType, bool isInternal)
|
public static CreateNotificationCommand Create(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, string correlationIdType, bool isInternal, bool isHtml = false)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(author))
|
if (string.IsNullOrWhiteSpace(sender))
|
||||||
throw new ArgumentException("Author cannot be null or whitespace.", nameof(author));
|
throw new ArgumentException("Sender name is required.", nameof(sender));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(title))
|
if (string.IsNullOrWhiteSpace(subject))
|
||||||
throw new ArgumentException("Title cannot be null or whitespace.", nameof(title));
|
throw new ArgumentException("Subject is required.", nameof(subject));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(description))
|
if (string.IsNullOrWhiteSpace(message))
|
||||||
throw new ArgumentException("Description cannot be null or whitespace.", nameof(description));
|
throw new ArgumentException("Message is required.", nameof(message));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(platformAddress))
|
if (string.IsNullOrWhiteSpace(recipient))
|
||||||
throw new ArgumentException("PlatformAddress cannot be null or whitespace.", nameof(platformAddress));
|
throw new ArgumentException("Recipient name is required.", nameof(recipient));
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(recipientAddress))
|
||||||
|
throw new ArgumentException("Recipient address is required.", nameof(recipientAddress));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(correlationId))
|
if (string.IsNullOrWhiteSpace(correlationId))
|
||||||
throw new ArgumentException("CorrelationId cannot be null or whitespace.", nameof(correlationId));
|
throw new ArgumentException("CorrelationId is required.", nameof(correlationId));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(correlationIdType))
|
if (string.IsNullOrWhiteSpace(correlationIdType))
|
||||||
throw new ArgumentException("CorrelationIdType cannot be null or whitespace.", nameof(correlationIdType));
|
throw new ArgumentException("CorrelationIdType is required.", nameof(correlationIdType));
|
||||||
|
|
||||||
return new(direction, author, title, description, platform, platformAddress, correlationId, correlationIdType, isInternal);
|
return new(direction, sender, senderAddress, subject, message, platform, priority, recipient, recipientAddress, correlationId, correlationIdType, isInternal, isHtml);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-10
@@ -6,29 +6,34 @@ public class CreateNotificationCommandHandler(IDbContextFactory<ShopDbContext> c
|
|||||||
{
|
{
|
||||||
public async ValueTask<Result<Guid>> Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
|
public async ValueTask<Result<Guid>> Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
var newNotification = context.Notifications.Add(new Entities.Notification
|
var newNotification = context.Notifications.Add(new Entities.Notification
|
||||||
{
|
{
|
||||||
Direction = request.Direction,
|
Direction = request.Direction,
|
||||||
Sender = request.Author,
|
SenderName = request.Sender,
|
||||||
Subject = request.Title,
|
Sender = request.SenderAddress,
|
||||||
Message = request.Description,
|
Recipient = request.Recipient,
|
||||||
|
RecipientAddress = request.RecipientAddress,
|
||||||
|
Subject = request.Subject,
|
||||||
|
Message = request.Message,
|
||||||
Platform = request.Platform,
|
Platform = request.Platform,
|
||||||
Recipient = request.PlatformAddress,
|
Priority = request.Priority,
|
||||||
CorrelationId = request.CorrelationId,
|
CorrelationId = request.CorrelationId,
|
||||||
CorrelationIdType = request.CorrelationIdType,
|
CorrelationIdType = request.CorrelationIdType,
|
||||||
IsInternal = request.IsInternal,
|
IsInternal = request.IsInternal,
|
||||||
|
IsHtml = request.IsHtml,
|
||||||
|
Processed = false
|
||||||
});
|
});
|
||||||
|
|
||||||
return newNotification is not null
|
return newNotification is not null
|
||||||
? Result.Ok(newNotification.Entity.Id)
|
? Result.Ok(newNotification.Entity.Id)
|
||||||
: Result.Fail(new Error("Failed to create notification"));
|
: Result.Fail(new Error("Failed to create notification"));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ public class GetOrdersQueryHandler(IDbContextFactory<ShopDbContext> contextFacto
|
|||||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
var orders = await context.Orders
|
var orders = await context.Orders
|
||||||
|
.AsNoTracking()
|
||||||
.OrderByDescending(o => o.CreatedAt)
|
.OrderByDescending(o => o.CreatedAt)
|
||||||
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
|
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
|
||||||
.Take(request.MaxRecords)
|
.Take(request.MaxRecords)
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||||
|
|
||||||
|
public class AddPackageToShoppingCartCommand : IRequest<Result>
|
||||||
|
{
|
||||||
|
public Guid ShoppingCartId { get; set; }
|
||||||
|
|
||||||
|
public Guid PackageId { get; set; }
|
||||||
|
|
||||||
|
private AddPackageToShoppingCartCommand(Guid shoppingCartId, Guid packageId)
|
||||||
|
{
|
||||||
|
ShoppingCartId = shoppingCartId;
|
||||||
|
PackageId = packageId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AddPackageToShoppingCartCommand Create(Guid shoppingCartId, Guid packageId)
|
||||||
|
{
|
||||||
|
if (shoppingCartId == Guid.Empty)
|
||||||
|
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
|
||||||
|
|
||||||
|
if (packageId == Guid.Empty)
|
||||||
|
throw new ArgumentException($"Package ID is required", nameof(packageId));
|
||||||
|
|
||||||
|
return new(shoppingCartId, packageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
|
||||||
|
|
||||||
|
public class AddPackageToShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<AddPackageToShoppingCartCommand, Result>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result> Handle(AddPackageToShoppingCartCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken))
|
||||||
|
return Result.Fail($"Package cold not be found by ID {request.PackageId}");
|
||||||
|
|
||||||
|
var shoppingCart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
|
||||||
|
|
||||||
|
if (shoppingCart is null)
|
||||||
|
return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}");
|
||||||
|
|
||||||
|
if (!await context.ShoppingCartPackages.AnyAsync(cp => cp.ShoppingCartId == request.ShoppingCartId, cancellationToken))
|
||||||
|
return Result.Fail($"Package {request.PackageId} is already in the cart");
|
||||||
|
|
||||||
|
var newShoppingCartPackage = context.ShoppingCartPackages.Add(new Entities.ShoppingCartPackage
|
||||||
|
{
|
||||||
|
ShoppingCartId = request.ShoppingCartId,
|
||||||
|
PackageId = request.PackageId
|
||||||
|
});
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok()
|
||||||
|
: Result.Fail($"Could not add package of id {request.PackageId} to shopping cart {request.ShoppingCartId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
|
||||||
|
|
||||||
|
public class RemovePackageFromShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<RemovePackageFromShoppingCartCommand, Result>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result> Handle(RemovePackageFromShoppingCartCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
|
||||||
|
return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}");
|
||||||
|
|
||||||
|
if (!await context.ShoppingCartPackages.AnyAsync(p => p.Id == request.ShoppingCartPackageId, cancellationToken))
|
||||||
|
return Result.Fail($"Shopping cart package {request.ShoppingCartPackageId} is not in the shopping cart {request.ShoppingCartId}");
|
||||||
|
|
||||||
|
var shoppingCartPackage = await context.ShoppingCartPackages.FirstOrDefaultAsync(cp => cp.Id == request.ShoppingCartPackageId, cancellationToken);
|
||||||
|
|
||||||
|
if (shoppingCartPackage is null)
|
||||||
|
return Result.Ok();
|
||||||
|
|
||||||
|
context.ShoppingCartPackages.Remove(shoppingCartPackage!);
|
||||||
|
|
||||||
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||||
|
? Result.Ok()
|
||||||
|
: Result.Fail($"Could remove package of id {request.ShoppingCartPackageId} from shopping cart {request.ShoppingCartId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||||
|
|
||||||
|
public class RemovePackageFromShoppingCartCommand : IRequest<Result>
|
||||||
|
{
|
||||||
|
public Guid ShoppingCartId { get; set; }
|
||||||
|
|
||||||
|
public Guid ShoppingCartPackageId { get; set; }
|
||||||
|
|
||||||
|
private RemovePackageFromShoppingCartCommand(Guid shoppingCartId, Guid shoppingCartPackageId)
|
||||||
|
{
|
||||||
|
ShoppingCartId = shoppingCartId;
|
||||||
|
ShoppingCartPackageId = shoppingCartPackageId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RemovePackageFromShoppingCartCommand Create(Guid shoppingCartId, Guid shoppingCartPackageId)
|
||||||
|
{
|
||||||
|
if (shoppingCartId == Guid.Empty)
|
||||||
|
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
|
||||||
|
|
||||||
|
if (shoppingCartPackageId == Guid.Empty)
|
||||||
|
throw new ArgumentException($"Shopping cart Package ID is required", nameof(shoppingCartPackageId));
|
||||||
|
|
||||||
|
return new(shoppingCartId, shoppingCartPackageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.ShoppingCarts.Queries;
|
||||||
|
|
||||||
|
public class GetShoppingCartPackagesQuery : IRequest<Result<ShoppingCartPackage[]>>
|
||||||
|
{
|
||||||
|
public Guid ShoppingCartId { get; set; }
|
||||||
|
|
||||||
|
private GetShoppingCartPackagesQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
|
||||||
|
|
||||||
|
public static GetShoppingCartPackagesQuery Create(Guid shoppingCartId)
|
||||||
|
{
|
||||||
|
if (shoppingCartId == Guid.Empty)
|
||||||
|
throw new ArgumentException("Shopping cart id is required", nameof(shoppingCartId));
|
||||||
|
|
||||||
|
return new(shoppingCartId);
|
||||||
|
}
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
using LiteCharms.Extensions;
|
||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
using LiteCharms.Models;
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||||
|
|
||||||
|
public class GetShoppingCartPackagesQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetShoppingCartPackagesQuery, Result<ShoppingCartPackage[]>>
|
||||||
|
{
|
||||||
|
public async ValueTask<Result<ShoppingCartPackage[]>> Handle(GetShoppingCartPackagesQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
|
||||||
|
return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}");
|
||||||
|
|
||||||
|
var packages = await context.ShoppingCartPackages.AsNoTracking()
|
||||||
|
.OrderByDescending(o => o.CreatedAt)
|
||||||
|
.Where(cp => cp.ShoppingCartId == request.ShoppingCartId)
|
||||||
|
.ToArrayAsync(cancellationToken);
|
||||||
|
|
||||||
|
return packages?.Length > 0
|
||||||
|
? Result.Ok(packages.Select(p => p.ToModel()).ToArray())
|
||||||
|
: Result.Fail($"Could not find packaged in shopping cart by ID {request.ShoppingCartId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail<ShoppingCartPackage[]>(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user