-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slimmed immutable shopping cart in exercises
- Loading branch information
1 parent
f2dca14
commit b291a39
Showing
6 changed files
with
215 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ic.Slimmed/Immutable/PricingCalculator.cs → ...d/Immutable/Products/PricingCalculator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/Products/ProductItems.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Immutable; | ||
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Tools; | ||
|
||
namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Products; | ||
|
||
using ProductId = Guid; | ||
using ProductIdWithPrice = string; | ||
using Quantity = int; | ||
using Price = decimal; | ||
|
||
public record ProductItem(ProductId ProductId, Quantity Quantity); | ||
|
||
public record PricedProductItem( | ||
ProductId ProductId, | ||
Quantity Quantity, | ||
Price UnitPrice | ||
) | ||
{ | ||
public Price TotalPrice => Quantity * UnitPrice; | ||
} | ||
|
||
public class ProductItems(ImmutableDictionary<ProductIdWithPrice, Quantity> Items) | ||
{ | ||
public static ProductItems Empty => new ProductItems(ImmutableDictionary<ProductIdWithPrice, Quantity>.Empty); | ||
|
||
public ProductItems Add(PricedProductItem productItem) => | ||
new(Items.Set(Key(productItem), currentQuantity => currentQuantity + productItem.Quantity)); | ||
|
||
public ProductItems Remove(PricedProductItem productItem) => | ||
new(Items.Set(Key(productItem), currentQuantity => currentQuantity - productItem.Quantity)); | ||
|
||
public bool HasEnough(PricedProductItem productItem) => | ||
Items.TryGetValue(Key(productItem), out var currentQuantity) && currentQuantity >= productItem.Quantity; | ||
|
||
private static ProductIdWithPrice Key(PricedProductItem pricedProductItem) => | ||
$"{pricedProductItem.ProductId}_{pricedProductItem.UnitPrice}"; | ||
} |
Oops, something went wrong.