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