Sealed qualifying public classes Migrated database changes
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
using LiteCharms.Features.MidrandBooks.Payments.Entities;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
|
||||
[EntityTypeConfiguration<OrderConfiguration, Order>]
|
||||
public class Order : Models.Order
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
|
||||
public class OrderConfiguration : IEntityTypeConfiguration<Order>
|
||||
public sealed class OrderConfiguration : IEntityTypeConfiguration<Order>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Order> builder)
|
||||
{
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
|
||||
public class OrderItemConfiguration : IEntityTypeConfiguration<OrderItem>
|
||||
public sealed class OrderItemConfiguration : IEntityTypeConfiguration<OrderItem>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<OrderItem> builder)
|
||||
{
|
||||
builder.ToTable("OrderItems");
|
||||
|
||||
builder.HasKey(oi => oi.Id);
|
||||
builder.Property(oi => oi.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("new()");
|
||||
builder.Property(oi => oi.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(oi => oi.OrderId).IsRequired();
|
||||
builder.Property(oi => oi.AuthorBookId).IsRequired();
|
||||
builder.Property(oi => oi.ProductPriceId).IsRequired();
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
|
||||
[EntityTypeConfiguration<RefundConfiguration, Refund>]
|
||||
public class Refund : Models.Refund
|
||||
{
|
||||
public virtual Order? Order { get; set; }
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
|
||||
public class RefundConfiguration : IEntityTypeConfiguration<Refund>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Refund> builder)
|
||||
{
|
||||
builder.ToTable("Refunds");
|
||||
|
||||
builder.HasKey(r => r.Id);
|
||||
builder.Property(r => r.OrderId).IsRequired();
|
||||
builder.Property(o => o.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(o => o.UpdatedAt).HasDefaultValueSql("now()");
|
||||
builder.Property(o => o.Status).IsRequired();
|
||||
builder.Property(r => r.Amount).IsRequired().HasPrecision(18, 2);
|
||||
builder.Property(r => r.Reason).HasMaxLength(1000);
|
||||
|
||||
builder.HasOne(r => r.Order)
|
||||
.WithMany(o => o.Refunds)
|
||||
.HasForeignKey(r => r.OrderId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
|
||||
public class ShippingConfiguration : IEntityTypeConfiguration<Shipping>
|
||||
public sealed class ShippingConfiguration : IEntityTypeConfiguration<Shipping>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Shipping> builder)
|
||||
{
|
||||
@@ -13,6 +13,7 @@ public class ShippingConfiguration : IEntityTypeConfiguration<Shipping>
|
||||
builder.Property(s => s.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(s => s.UpdatedAt).HasDefaultValueSql("now()");
|
||||
builder.Property(s => s.Status).IsRequired();
|
||||
builder.Property(s => s.TrackingNumber).HasMaxLength(255);
|
||||
|
||||
builder.HasOne(s => s.Order)
|
||||
.WithOne(o => o.Shipping)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Entities;
|
||||
|
||||
public class ShippingProviderConfiguration : IEntityTypeConfiguration<ShippingProvider>
|
||||
public sealed class ShippingProviderConfiguration : IEntityTypeConfiguration<ShippingProvider>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<ShippingProvider> builder)
|
||||
{
|
||||
@@ -13,5 +13,6 @@ public class ShippingProviderConfiguration : IEntityTypeConfiguration<ShippingPr
|
||||
builder.Property(f => f.Name).IsRequired().HasMaxLength(100);
|
||||
builder.Property(f => f.Price).IsRequired().HasPrecision(18, 2);
|
||||
builder.Property(f => f.Enabled).HasDefaultValue(true);
|
||||
builder.Property(f => f.TrackingUrl).HasMaxLength(200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Models;
|
||||
|
||||
public record CreateOrder(long CustomerId, decimal TotalPrice, string? Notes);
|
||||
public sealed record CreateOrder(long CustomerId, decimal TotalPrice, string? Notes);
|
||||
|
||||
public record CreateOrderItem(long AuthorBookId, long ProductPriceId, int Quantity);
|
||||
public sealed record CreateOrderItem(long AuthorBookId, long ProductPriceId, int Quantity);
|
||||
|
||||
public sealed record CreateShipping(long OrderId, long AddressId, long ShippingProviderId, string? TrackingNumber);
|
||||
|
||||
public sealed record CreateShippingProvider(ShippingProviderTypes Type, string Name, decimal Price, string TrackingUrl);
|
||||
|
||||
public sealed record UpdateShippingProvider(long ProviderId, bool Enabled, string Name, decimal Price, string TrackingUrl);
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Models;
|
||||
|
||||
public class Refund
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
public Guid OrderId { get; set; }
|
||||
|
||||
public RefundTypes Type { get; set; }
|
||||
|
||||
public RefundStatus Status { get; set; }
|
||||
|
||||
public string? Reason { get; set; }
|
||||
|
||||
public decimal Amount { get; set; }
|
||||
}
|
||||
@@ -14,5 +14,7 @@ public class Shipping
|
||||
|
||||
public long ShippingProviderId { get; set; }
|
||||
|
||||
public string? TrackingNumber { get; set; }
|
||||
|
||||
public ShippingStatuses Status { get; set; }
|
||||
}
|
||||
|
||||
@@ -14,5 +14,7 @@ public class ShippingProvider
|
||||
|
||||
public decimal? Price { get; set; }
|
||||
|
||||
public string? TrackingUrl { get; set; }
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using LiteCharms.Features.MidrandBooks.Abstractions;
|
||||
using LiteCharms.Features.MidrandBooks.Extensions;
|
||||
using LiteCharms.Features.MidrandBooks.Orders.Models;
|
||||
using LiteCharms.Features.MidrandBooks.Postgres;
|
||||
using LiteCharms.Features.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders;
|
||||
|
||||
public class OrderService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
|
||||
public sealed class OrderService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
|
||||
{
|
||||
public async ValueTask<Result<long>> CreateOrderAsync(long customerId, CreateOrder request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -106,4 +108,356 @@ public class OrderService(IDbContextFactory<MidrandBooksDbContext> contextFactor
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> RemoveItemFromOrderAsync(long orderId, long orderItemId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var orderItem = await context.OrderItems.FirstOrDefaultAsync(oi => oi.Id == orderItemId && oi.OrderId == orderId, cancellationToken);
|
||||
|
||||
if (orderItem is null)
|
||||
return Result.Fail("Order item not found.");
|
||||
|
||||
context.OrderItems.Remove(orderItem);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to remove item from order.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> ClearOrderItemasAsync(long orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var orderItems = context.OrderItems.Where(oi => oi.OrderId == orderId);
|
||||
|
||||
context.OrderItems.RemoveRange(orderItems);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to clear order items.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> CancelOrderAsync(long orderId, CancellationToken cancellationToken = default) =>
|
||||
await UpdateOrderStatusAsync(orderId, OrderStatus.Cancelled, cancellationToken);
|
||||
|
||||
public async ValueTask<Result<Order>> GetOrderAsync(long orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var order = await context.Orders.AsNoTracking().FirstOrDefaultAsync(o => o.Id == orderId, cancellationToken);
|
||||
|
||||
return order is not null
|
||||
? Result.Ok(order.ToModel())
|
||||
: Result.Fail<Order>("Order not found.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Order>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Order[]>> GetOrdersByCustomerAsync(long customerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken))
|
||||
return Result.Fail<Order[]>("Customer not found.");
|
||||
|
||||
var orders = await context.Orders
|
||||
.AsNoTracking()
|
||||
.Where(o => o.CustomerId == customerId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Result.Ok(orders.Select(o => o.ToModel()).ToArray());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Order[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Order[]>> GetOrdersAsync(DateRange range, int index, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var orders = await context.Orders
|
||||
.AsNoTracking()
|
||||
.Where(o => o.CreatedAt >= range.From.ToDateTime(TimeOnly.MinValue) && o.CreatedAt <= range.To.ToDateTime(TimeOnly.MaxValue))
|
||||
.Skip(index * range.MaxRecords)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Result.Ok(orders.Select(o => o.ToModel()).ToArray());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Order[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateOrderStatusAsync(long orderId, OrderStatus newStatus, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var order = await context.Orders.FirstOrDefaultAsync(o => o.Id == orderId, cancellationToken);
|
||||
|
||||
if (order is null)
|
||||
return Result.Fail("Order not found.");
|
||||
|
||||
order.UpdatedAt = DateTime.UtcNow;
|
||||
order.Status = newStatus;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to update order status.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> AddShippingToOrderAsync(long orderId, CreateShipping request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(!await context.Orders.AnyAsync(o => o.Id == orderId, cancellationToken))
|
||||
return Result.Fail("Order not found.");
|
||||
|
||||
if(!await context.Addresses.AnyAsync(a => a.Id == request.AddressId, cancellationToken))
|
||||
return Result.Fail("Address not found.");
|
||||
|
||||
if(!await context.ShippingProviders.AnyAsync(sp => sp.Id == request.ShippingProviderId && sp.Enabled, cancellationToken))
|
||||
return Result.Fail("Shipping provider not found or disabled.");
|
||||
|
||||
if(await context.Shippings.AnyAsync(s => s.OrderId == orderId, cancellationToken))
|
||||
return Result.Fail("Shipping already exists for this order.");
|
||||
|
||||
var shipping = context.Shippings.Add(new Entities.Shipping
|
||||
{
|
||||
OrderId = orderId,
|
||||
AddressId = request.AddressId,
|
||||
ShippingProviderId = request.ShippingProviderId,
|
||||
Status = ShippingStatuses.Pending,
|
||||
TrackingNumber = request.TrackingNumber
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to add shipping to order.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateShippingStatusAsync(long orderId, ShippingStatuses newStatus, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var shipping = await context.Shippings.FirstOrDefaultAsync(s => s.OrderId == orderId, cancellationToken);
|
||||
|
||||
if (shipping is null)
|
||||
return Result.Fail("Shipping not found for this order.");
|
||||
|
||||
shipping.UpdatedAt = DateTime.UtcNow;
|
||||
shipping.Status = newStatus;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to update shipping status.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<Shipping>> GetShippingByOrderIdAsync(long orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var shipping = await context.Shippings
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(s => s.OrderId == orderId, cancellationToken);
|
||||
|
||||
return shipping is not null
|
||||
? Result.Ok(shipping.ToModel())
|
||||
: Result.Fail<Shipping>("Shipping not found for this order.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Shipping>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> RemoveShippingFromOrderAsync(long orderId, long shippingId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(!await context.Orders.AnyAsync(o => o.Id == orderId, cancellationToken))
|
||||
return Result.Fail("Order not found.");
|
||||
|
||||
var shipping = await context.Shippings.AsNoTracking()
|
||||
.FirstOrDefaultAsync(s => s.OrderId == orderId && s.Id == shippingId, cancellationToken);
|
||||
|
||||
if (shipping is null)
|
||||
return Result.Fail("Shipping not found for this order.");
|
||||
|
||||
context.Shippings.Remove(shipping);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to remove shipping from order.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateShippingTrackingNumberAsync(long orderId, long shippingId, string trackingNumber, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var shipping = await context.Shippings.FirstOrDefaultAsync(s => s.OrderId == orderId && s.Id == shippingId, cancellationToken);
|
||||
|
||||
if (shipping is null)
|
||||
return Result.Fail("Shipping not found for this order.");
|
||||
|
||||
shipping.UpdatedAt = DateTime.UtcNow;
|
||||
shipping.TrackingNumber = trackingNumber;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to update shipping tracking number.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> CreateShippingProviderAsync(CreateShippingProvider request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(await context.ShippingProviders.AnyAsync(sp => sp.Type == request.Type, cancellationToken))
|
||||
return Result.Fail("Shipping provider with the same type already exists.");
|
||||
|
||||
var shippingProvider = context.ShippingProviders.Add(new Entities.ShippingProvider
|
||||
{
|
||||
Name = request.Name,
|
||||
Type = request.Type,
|
||||
Price = request.Price,
|
||||
TrackingUrl = request.TrackingUrl
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to create shipping provider.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<ShippingProvider[]>> GetShippingProvidersAsync(bool isEnabled, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var providers = await context.ShippingProviders.AsNoTracking().Where(sp => sp.Enabled == isEnabled)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Result.Ok(providers.Select(sp => sp.ToModel()).ToArray());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShippingProvider[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<ShippingProvider>> GetShippingProviderAsync(long providerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var provider = await context.ShippingProviders.AsNoTracking()
|
||||
.FirstOrDefaultAsync(sp => sp.Id == providerId, cancellationToken);
|
||||
|
||||
return provider is not null
|
||||
? Result.Ok(provider.ToModel())
|
||||
: Result.Fail<ShippingProvider>("Shipping provider not found.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShippingProvider>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateShippingProviderAsync(UpdateShippingProvider request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var provider = await context.ShippingProviders.FirstOrDefaultAsync(sp => sp.Id == request.ProviderId, cancellationToken);
|
||||
|
||||
if (provider is null)
|
||||
return Result.Fail("Shipping provider not found.");
|
||||
|
||||
provider.UpdatedAt = DateTime.UtcNow;
|
||||
provider.Enabled = request.Enabled;
|
||||
provider.Name = request.Name;
|
||||
provider.Price = request.Price;
|
||||
provider.TrackingUrl = request.TrackingUrl;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail("Failed to update shipping provider status.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user