This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace LiteCharms.Features.TechShop.CartPackages.Entities;
|
||||
|
||||
[EntityTypeConfiguration<PackageConfirguration, Package>]
|
||||
public class Package : Models.Package
|
||||
{
|
||||
public virtual ICollection<PackageItem>? PackageItems { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace LiteCharms.Features.TechShop.CartPackages.Entities;
|
||||
|
||||
public class PackageConfirguration : IEntityTypeConfiguration<Package>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Package> builder)
|
||||
{
|
||||
builder.ToTable(nameof(Package));
|
||||
|
||||
builder.HasKey(f => f.Id);
|
||||
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(f => f.UpdatedAt).IsRequired(false).HasDefaultValueSql(null);
|
||||
builder.Property(f => f.Name).IsRequired();
|
||||
builder.Property(f => f.Summary).IsRequired().HasMaxLength(512);
|
||||
builder.Property(f => f.Description).IsRequired().HasMaxLength(2048);
|
||||
builder.Property(f => f.ImageUrl).IsRequired(false).HasMaxLength(2048);
|
||||
builder.Property(f => f.Active);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using LiteCharms.Features.TechShop.Products.Entities;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.CartPackages.Entities;
|
||||
|
||||
[EntityTypeConfiguration<PackageItemConfiguration, PackageItem>]
|
||||
public class PackageItem : Models.PackageItem
|
||||
{
|
||||
public virtual Package? Package { get; set; }
|
||||
|
||||
public virtual ProductPrice? ProductPrice { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace LiteCharms.Features.TechShop.CartPackages.Entities;
|
||||
|
||||
public class PackageItemConfiguration : IEntityTypeConfiguration<PackageItem>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PackageItem> builder)
|
||||
{
|
||||
builder.ToTable(nameof(PackageItem));
|
||||
|
||||
builder.HasKey(f => f.Id);
|
||||
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(f => f.PackageId).IsRequired();
|
||||
builder.Property(f => f.ProductPriceId).IsRequired();
|
||||
builder.Property(f => f.Active);
|
||||
|
||||
builder.HasOne(f => f.Package)
|
||||
.WithMany(f => f.PackageItems)
|
||||
.HasForeignKey(pi => pi.PackageId)
|
||||
.IsRequired()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasOne(f => f.ProductPrice)
|
||||
.WithMany()
|
||||
.HasForeignKey(pi => pi.ProductPriceId)
|
||||
.IsRequired()
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace LiteCharms.Features.TechShop.CartPackages.Models;
|
||||
|
||||
public class Package
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
public string? Name { get; set; }
|
||||
|
||||
public string? Summary { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
|
||||
public string? ImageUrl { get; set; }
|
||||
|
||||
public bool Active { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace LiteCharms.Features.TechShop.CartPackages.Models;
|
||||
|
||||
public class PackageItem
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public Guid PackageId { get; set; }
|
||||
|
||||
public Guid ProductPriceId { get; set; }
|
||||
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
|
||||
public bool Active { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
using LiteCharms.Features.Extensions;
|
||||
using LiteCharms.Features.Models;
|
||||
using LiteCharms.Features.TechShop.CartPackages.Models;
|
||||
using LiteCharms.Features.TechShop.Extensions;
|
||||
using LiteCharms.Features.TechShop.Postgres;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.CartPackages;
|
||||
|
||||
public class PackageService(IDbContextFactory<ShopDbContext> contextFactory)
|
||||
{
|
||||
public async ValueTask<Result<Guid>> AddPackageItemAsync(Guid packageId, Guid productPriceId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken))
|
||||
return Result.Fail($"Could not find package by ID {packageId}");
|
||||
|
||||
if (!await context.ProductPrices.AnyAsync(p => p.Id == productPriceId && p.Active == true, cancellationToken))
|
||||
return Result.Fail($"Could not find an active product price by ID {productPriceId}");
|
||||
|
||||
if (await context.PackageItems.AnyAsync(p => p.ProductPriceId == productPriceId && p.PackageId == packageId, cancellationToken))
|
||||
return Result.Fail<Guid>($"Product price {productPriceId} is already added to this package {packageId}");
|
||||
|
||||
var newPackageItem = context.PackageItems.Add(new Entities.PackageItem
|
||||
{
|
||||
PackageId = packageId,
|
||||
ProductPriceId = 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 {productPriceId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Guid>> CreatePackageAsync(string? name, string? summary, string? description, string? ImageUrl, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (await context.Packages.AnyAsync(p => p.Name == name, cancellationToken))
|
||||
return Result.Fail($"A package by the same name already exists: {name}");
|
||||
|
||||
var newPackage = context.Packages.Add(new Entities.Package
|
||||
{
|
||||
Name = name,
|
||||
Summary = summary,
|
||||
Description = description,
|
||||
ImageUrl = ImageUrl,
|
||||
Active = true
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(newPackage.Entity.Id)
|
||||
: Result.Fail($"Failed to create a new package by the name: {name}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> DeletePackageItemAsync(Guid packageId, Guid packageItemId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken))
|
||||
return Result.Fail($"Could not find package by ID {packageId}");
|
||||
|
||||
var item = await context.PackageItems.FirstOrDefaultAsync(p => p.Id == packageItemId && p.PackageId == packageId, cancellationToken);
|
||||
|
||||
if (item is null)
|
||||
return Result.Fail($"Product item {packageItemId} is already added to this package {packageId}");
|
||||
|
||||
context.PackageItems.Remove(item);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to delete package item by id {packageItemId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> DeletePackageItemsAsync(Guid packageId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken))
|
||||
return Result.Fail($"Could not find package by ID {packageId}");
|
||||
|
||||
var items = await context.PackageItems.Where(i => i.PackageId == packageId).ToArrayAsync(cancellationToken);
|
||||
|
||||
context.PackageItems.RemoveRange(items);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to delete package {packageId} items");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Package>> GetPackageAsync(Guid packageId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == packageId, cancellationToken);
|
||||
|
||||
return package is not null
|
||||
? Result.Ok(package.ToModel())
|
||||
: Result.Fail($"Failed to find package by ID {packageId}");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Package>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<PackageItem[]>> GetPackageItemsAsync(Guid packageId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken))
|
||||
return Result.Fail<PackageItem[]>($"Package could not be found with ID {packageId}");
|
||||
|
||||
var items = await context.PackageItems.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(p => p.PackageId == 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 {packageId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<PackageItem[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Package[]>> GetPackagesAsync(Guid packageId, DateRange range, bool active, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fromDate = range.From.ToDateTime(TimeOnly.MinValue);
|
||||
var toDate = range.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 == active)
|
||||
.Take(range.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 {range.From} - {range.To}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Package[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Guid>> UpdatePackageAsync(Guid packageId, string? name, string? summary, string? description, string? ImageUrl, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (await context.Packages.AnyAsync(p => p.Name == name, cancellationToken))
|
||||
return Result.Fail($"A package by the same name already exists: {name}");
|
||||
|
||||
var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == packageId, cancellationToken);
|
||||
|
||||
if (package is null)
|
||||
return Result.Fail($"Could not find package by id {packageId}");
|
||||
|
||||
package.Name = name;
|
||||
package.Summary = summary;
|
||||
package.Description = description;
|
||||
package.ImageUrl = ImageUrl;
|
||||
package.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to update package with id {packageId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Guid>> UpdatePackageStatusAsync(Guid packageId, bool active, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == packageId, cancellationToken);
|
||||
|
||||
if (package is null)
|
||||
return Result.Fail($"Could not find package by id {packageId}");
|
||||
|
||||
package.Active = active;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to update package with id {packageId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user