Added package management
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user