Created
September 21, 2020 07:43
-
-
Save laur3d/e5bae491203bea7ec7a66d51382d8e5d to your computer and use it in GitHub Desktop.
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
[JsonObject(MemberSerialization.OptIn)] | |
public class ShoppingCartEntity : IShoppingCart | |
{ | |
[JsonProperty("list")] | |
private List<CartItem> list { get; set; } = new List<CartItem>(); | |
public void Add(CartItem item) | |
{ | |
// Get existing | |
var existingItem = this.list.FirstOrDefault(i => i.ProductId == item.ProductId); | |
if (existingItem == null) | |
{ | |
this.list.Add(item); | |
} | |
else | |
{ | |
existingItem.Count += item.Count; | |
} | |
} | |
public void Remove(CartItem item) | |
{ | |
var existingItem = this.list.FirstOrDefault(i => i.ProductId == item.ProductId); | |
if (existingItem == null) | |
{ | |
return; | |
} | |
if (existingItem.Count > item.Count) | |
{ | |
existingItem.Count -= item.Count; | |
} | |
else | |
{ | |
this.list.Remove(existingItem); | |
} | |
} | |
public Task<ReadOnlyCollection<CartItem>> GetCartItems() | |
{ | |
return Task.FromResult(this.list.AsReadOnly()); | |
} | |
[FunctionName(nameof(ShoppingCartEntity))] | |
public static Task Run([EntityTrigger] IDurableEntityContext ctx) | |
=> ctx.DispatchAsync<ShoppingCartEntity>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment