Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 388a5f4c87 | |||
| 40f4656036 | |||
| c31dd308a4 | |||
| 11dfd18a44 | |||
| e7f02eca9b |
@@ -14,7 +14,7 @@ builder.Services.AddScopedFeatureManagement();
|
||||
|
||||
builder.Services
|
||||
.AddLogging()
|
||||
.AddShopServices()
|
||||
.AddShopServices()
|
||||
.AddHostedService<ProductsSeederService>()
|
||||
.AddHostedService<CategorySeederService>()
|
||||
.AddHostedService<CustomerSeederService>()
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using LiteCharms.Features.Extensions;
|
||||
using LiteCharms.Features.MidrandBooks.Abstractions;
|
||||
using LiteCharms.Features.MidrandBooks.Extensions;
|
||||
using Microsoft.VisualStudio.TestPlatform.TestHost;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Tests.Common;
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Abstractions;
|
||||
|
||||
public interface IMidrandBooks;
|
||||
@@ -1,4 +1,5 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
using LiteCharms.Features.MidrandBooks.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Extensions;
|
||||
|
||||
@@ -8,11 +9,15 @@ public static class Shop
|
||||
{
|
||||
var serviceType = typeof(IService);
|
||||
|
||||
var implementations = Assembly.GetExecutingAssembly().GetTypes()
|
||||
var sharedImplementations = typeof(IFeatures).Assembly.GetTypes()
|
||||
.Where(t => serviceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
|
||||
|
||||
foreach (var implementation in implementations)
|
||||
services.AddScoped(implementation);
|
||||
foreach (var sharedImplementation in sharedImplementations) services.AddScoped(sharedImplementation);
|
||||
|
||||
var coreImplementations = typeof(IMidrandBooks).Assembly.GetTypes()
|
||||
.Where(t => serviceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
|
||||
|
||||
foreach (var coreImplementation in coreImplementations) services.AddScoped(coreImplementation);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
using LiteCharms.Features.MidrandBooks.Orders.Models;
|
||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders;
|
||||
|
||||
public sealed class CartService : IService
|
||||
{
|
||||
private Cart cart = new();
|
||||
|
||||
public Cart GetCart() => cart;
|
||||
|
||||
public void LoadCart(Cart savedCart) => cart = savedCart;
|
||||
|
||||
public void AddItem(ProductPrice productPrice)
|
||||
{
|
||||
var itemExists = false;
|
||||
|
||||
for (var i = 0; i < cart.Items.Count; i++)
|
||||
{
|
||||
if (cart.Items[i].Price!.Id == productPrice.Id)
|
||||
{
|
||||
cart.Items[i].Quantity++;
|
||||
cart.Items[i].Amount += productPrice.Amount;
|
||||
|
||||
itemExists = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!itemExists)
|
||||
cart.Items.Add(new CartItem
|
||||
{
|
||||
Price = productPrice,
|
||||
Amount = productPrice.Amount,
|
||||
Quantity = 1,
|
||||
});
|
||||
|
||||
CalculateTotalPrice();
|
||||
}
|
||||
|
||||
public void UpdateQuantity(long productPriceId, int newQuantity)
|
||||
{
|
||||
if (newQuantity <= 0)
|
||||
{
|
||||
RemoveAllSameItem(productPriceId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < cart.Items.Count; i++)
|
||||
{
|
||||
if (cart.Items[i].Price!.Id == productPriceId)
|
||||
{
|
||||
var oldQuantity = cart.Items[i].Quantity;
|
||||
var pricePerUnit = cart.Items[i].Price!.Amount;
|
||||
|
||||
cart.Items[i].Quantity = newQuantity;
|
||||
cart.Items[i].Amount = pricePerUnit * newQuantity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CalculateTotalPrice();
|
||||
}
|
||||
|
||||
public void RemoveOneItem(long productPriceId)
|
||||
{
|
||||
for (var i = 0; i < cart.Items.Count; i++)
|
||||
{
|
||||
if (cart.Items[i].Price!.Id == productPriceId)
|
||||
{
|
||||
if (cart.Items[i].Quantity <= 1)
|
||||
{
|
||||
cart.Items.RemoveAt(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
cart.Items[i].Quantity--;
|
||||
cart.Items[i].Amount -= cart.Items[i].Price!.Amount;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CalculateTotalPrice();
|
||||
}
|
||||
|
||||
public void RemoveAllSameItem(long productPriceId)
|
||||
{
|
||||
if (cart.Items.Count == 0) return;
|
||||
|
||||
var item = cart.Items.FirstOrDefault(i => i.Price?.Id == productPriceId);
|
||||
|
||||
if (item is not null) cart.Items.Remove(item);
|
||||
|
||||
CalculateTotalPrice();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if(cart.CustomerId is not null || cart.OrderId is not null)
|
||||
{
|
||||
cart.TotalPrice = 0;
|
||||
cart.TotalVat = 0;
|
||||
cart.Items.Clear();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
cart = new Cart();
|
||||
}
|
||||
|
||||
public decimal CalculateTotalPrice()
|
||||
{
|
||||
if (cart.Items.Count == 0) return 0;
|
||||
|
||||
var gross = cart.Items.Sum(i => i.Amount);
|
||||
|
||||
if (!cart.IsVatInclusive) cart.TotalVat = gross * cart.VatRate;
|
||||
|
||||
cart.TotalPrice = gross + cart.TotalVat;
|
||||
|
||||
return cart.TotalPrice;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Models;
|
||||
|
||||
public sealed class Cart
|
||||
{
|
||||
public long? CustomerId { get; set; }
|
||||
|
||||
public long? OrderId { get; set; }
|
||||
|
||||
public decimal TotalPrice { get; set; }
|
||||
|
||||
public decimal TotalVat { get; set; }
|
||||
|
||||
public decimal VatRate { get; set; } = 0.15m;
|
||||
|
||||
public bool IsVatInclusive { get; set; } = true;
|
||||
|
||||
public IList<CartItem> Items { get; set; } = [];
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Orders.Models;
|
||||
|
||||
public sealed class CartItem
|
||||
{
|
||||
public ProductPrice? Price { get; set; }
|
||||
|
||||
public long Quantity { get; set; }
|
||||
|
||||
public decimal Amount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace LiteCharms.Features.Abstractions;
|
||||
|
||||
public interface IFeatures;
|
||||
Reference in New Issue
Block a user