Fixing AddItem and adding more tests/cartservice/CartServiceTests

Updated AddItem functionality so it handles update quantities scenario.
Added tests
This commit is contained in:
Simon Zeltser 2018-06-27 10:33:18 -07:00
parent 400d51a9fe
commit 546562fe53
2 changed files with 56 additions and 24 deletions

View file

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using cartservice.interfaces;
using Google.Protobuf;
@ -40,14 +41,23 @@ namespace cartservice.cartstore
if (value.IsNull)
{
cart = new Hipstershop.Cart();
cart.UserId = userId;
cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity });
}
else
{
cart = Hipstershop.Cart.Parser.ParseFrom(value);
var existingItem = cart.Items.SingleOrDefault(i => i.ProductId == productId);
if (existingItem == null)
{
cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity });
}
else
{
existingItem.Quantity += quantity;
}
}
cart.UserId = userId;
cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity });
await db.HashSetAsync(userId, new[]{ new HashEntry(CART_FIELD_NAME, cart.ToByteArray()) });
}