Skip to content

Commit 27518e5

Browse files
committed
Modify CatalogItem for stocking
1 parent 0611978 commit 27518e5

1 file changed

Lines changed: 77 additions & 5 deletions

File tree

src/Services/Catalog/Catalog.API/Model/CatalogItem.cs

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Catalog.API.Infrastructure.Exceptions;
2+
using System;
23

34
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model
45
{
@@ -14,8 +15,6 @@ public class CatalogItem
1415

1516
public string PictureUri { get; set; }
1617

17-
public int Stock { get; set; }
18-
1918
public int CatalogTypeId { get; set; }
2019

2120
public CatalogType CatalogType { get; set; }
@@ -24,6 +23,79 @@ public class CatalogItem
2423

2524
public CatalogBrand CatalogBrand { get; set; }
2625

27-
public CatalogItem() { }
26+
// Quantity in stock
27+
public int AvailableStock { get; set; }
28+
29+
// Available stock at which we should reorder
30+
public int RestockThreshold { get; set; }
31+
32+
33+
// Maximum number of units that can be in-stock at any time (due to physicial/logistical constraints in warehouses)
34+
public int MaxStockThreshold { get; set; }
35+
36+
/// <summary>
37+
/// True if item is on reorder
38+
/// </summary>
39+
public bool OnReorder { get; set; }
40+
41+
public CatalogItem() { }
42+
43+
44+
/// <summary>
45+
/// Decrements the quantity of a particular item in inventory and ensures the restockThreshold hasn't
46+
/// been breached. If so, a RestockRequest is generated in CheckThreshold.
47+
///
48+
/// If there is sufficient stock of an item, then the integer returned at the end of this call should be the same as quantityDesired.
49+
/// In the event that there is not sufficient stock available, the method will remove whatever stock is available and return that quantity to the client.
50+
/// In this case, it is the responsibility of the client to determine if the amount that is returned is the same as quantityDesired.
51+
/// It is invalid to pass in a negative number.
52+
/// </summary>
53+
/// <param name="quantityDesired"></param>
54+
/// <returns>int: Returns the number actually removed from stock. </returns>
55+
///
56+
public int RemoveStock(int quantityDesired)
57+
{
58+
if (AvailableStock == 0)
59+
{
60+
throw new CatalogDomainException($"Empty stock, product item {Name} is sold out");
61+
}
62+
63+
if (quantityDesired <= 0)
64+
{
65+
throw new CatalogDomainException($"Item units desired should be greater than cero");
66+
}
67+
68+
int removed = Math.Min(quantityDesired, this.AvailableStock);
69+
70+
this.AvailableStock -= removed;
71+
72+
return removed;
73+
}
74+
75+
/// <summary>
76+
/// Increments the quantity of a particular item in inventory.
77+
/// <param name="quantity"></param>
78+
/// <returns>int: Returns the quantity that has been added to stock</returns>
79+
/// </summary>
80+
public int AddStock(int quantity)
81+
{
82+
int original = this.AvailableStock;
83+
84+
// The quantity that the client is trying to add to stock is greater than what can be physically accommodated in the Warehouse
85+
if ((this.AvailableStock + quantity) > this.MaxStockThreshold)
86+
{
87+
// For now, this method only adds new units up maximum stock threshold. In an expanded version of this application, we
88+
//could include tracking for the remaining units and store information about overstock elsewhere.
89+
this.AvailableStock += (this.MaxStockThreshold - this.AvailableStock);
90+
}
91+
else
92+
{
93+
this.AvailableStock += quantity;
94+
}
95+
96+
this.OnReorder = false;
97+
98+
return this.AvailableStock - original;
99+
}
28100
}
29-
}
101+
}

0 commit comments

Comments
 (0)