Skip to content

Instantly share code, notes, and snippets.

@laur3d
Created September 21, 2020 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laur3d/e5bae491203bea7ec7a66d51382d8e5d to your computer and use it in GitHub Desktop.
Save laur3d/e5bae491203bea7ec7a66d51382d8e5d to your computer and use it in GitHub Desktop.
[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