public Contracts.v0.Cart AddAnimal(string cartId, int animalId) { semaphoreSlim.Wait(TimeSpan.FromSeconds(1).Milliseconds); try { var domainCart = _cache.Get <Domain.Cart>(cartId); if (domainCart == null) { domainCart = new Domain.Cart(); } ; if (domainCart.CartContents.TryGetValue(animalId, out int quantity)) { domainCart.CartContents[animalId] = quantity + 1; } else { domainCart.CartContents.Add(animalId, 1); } _cache.Set(cartId, domainCart); return(ListAnimals(cartId)); } finally { semaphoreSlim.Release(); } }
public async Task Create(Guid id) { Domain.Cart cart = new Domain.Cart(id); _carts.Add(cart); await Task.CompletedTask; }
public static CartDto ToDto(this Domain.Cart cart) { var cartDto = new CartDto { Id = cart.Id.ToString(), CartTotal = cart.CartTotal, CartItemTotal = cart.CartItemTotal, CartItemPromoSavings = cart.CartItemPromoSavings, ShippingPromoSavings = cart.ShippingPromoSavings, ShippingTotal = cart.ShippingTotal, IsCheckOut = cart.IsCheckout }; cartDto.Items.AddRange(cart.CartItems.Select(cc => new CartItemDto { ProductId = cc.Product.ProductId.ToString(), ProductName = cc.Product.Name, Price = cc.Price, Quantity = cc.Quantity, PromoSavings = cc.PromoSavings }) .ToList()); return cartDto; }
public CartActor(Guid userId, IActorRef queryActor, IActorRef consoleWriterActor) { _userId = userId; _queryActor = queryActor; _consoleWriterActor = consoleWriterActor; _cart = new Domain.Cart(_userId); Ready(); }
private Domain.Cart GetOrCreateCart(Guid?cartToken) { var userId = GetUserId(); Domain.Cart userCart = null; Domain.Cart cartFromToken = null; if (userId.HasValue) { userCart = _cartContext.Carts .Include(x => x.CartItems) .ThenInclude(x => x.Product) .FirstOrDefault(x => x.OwnerUserId == userId ); } if (cartToken.HasValue && userCart?.CartAccessToken != cartToken) { cartFromToken = _cartContext.Carts .Include(x => x.CartItems) .ThenInclude(x => x.Product) .FirstOrDefault(x => x.CartAccessToken == cartToken && !x.OwnerUserId.HasValue ); } if (cartToken.HasValue && userCart != null) // token + user { if (cartFromToken != null) // token + user (same) { userCart.Merge(cartFromToken); _cartContext.Carts.Remove(cartFromToken); _cartContext.SaveChanges(); // todo not good } return(userCart); } if (cartFromToken != null) { if (userId.HasValue) // token + user without cart { cartFromToken.OwnerUserId = userId; _cartContext.SaveChanges(); // todo not good } return(cartFromToken); // token + no user } // todo changing basket (ex. switch from token to user) should be a http redirect // no token + user // no token + no user return(CreateCart(userId)); }
private Domain.Cart CreateCart(Guid?userId) { var cartToken = Guid.NewGuid(); var cart = new Domain.Cart() { CartAccessToken = cartToken, OwnerUserId = userId }; _cartContext.Add(cart); _cartContext.SaveChanges(); // todo not good return(cart); }
private void Ready() { Recover <ItemAddedToCart>(ev => { _cart.AddProductToCart(ev.ProductId); }); Recover <SnapshotOffer>(offer => { var json = offer.Snapshot as string; if (json != null) { _cart = JsonConvert.DeserializeObject <Domain.Cart>(json); } }); CommandAsync <AddProductToCartCommand>(async command => { var stockStatus = await _queryActor.Ask <bool>(new ProductStockStatusQuery(command.ProductId)); if (stockStatus == false) { Sender.Tell("Out of stock"); } try { _cart.AddProductToCart(command.ProductId); } catch (Exception e) { var result = CommandResult.Error(e.Message); Sender.Tell(result); return; } PersistEventAndSnapshot(new ItemAddedToCart(_userId, command.ProductId)); Sender.Tell(CommandResult.Success()); }); Command <SaveSnapshotSuccess>(x => { _consoleWriterActor.Tell($"Snapshot saved cart for user {_userId}"); }); Command <SaveSnapshotFailure>(x => { _consoleWriterActor.Tell($"Snapshot failed to save cart for user {_userId}"); }); }
public async Task <GetCartByIdResponse> InsertItemToCartAsync(InsertItemToNewCartRequest request) { var cart = new Domain.Cart(); cart.InsertItemToCart(new CartItem { Product = new Product(request.ProductId), PromoSavings = 0.0D, Quantity = request.Quantity }); cart = await InitCart(cart, populatePrice : true); cart = PriceCalculatorContext.Execute(cart); await _mutateRepository.AddAsync(cart); return(GetCartByIdResponse(cart)); }
private CartDto MapToApiModel(Domain.Cart cart) { return(new CartDto() { CartId = cart.Id, CartAccessToken = cart.CartAccessToken, Total = cart.Total, NumberOfItems = cart.NumberOfItems, CartItems = cart.CartItems.Select(x => new CartDto.CartItemDto() { Quantity = x.Quantity, ProductPrice = x.Product.Price, ProductId = x.ProductId, ProductName = x.Product.Name, Value = x.Value }) }); }
public static CartDto ToDto(this Domain.Cart cart) { return(new CartDto { Id = cart.Id, CartTotal = cart.CartTotal, CartItemTotal = cart.CartItemTotal, CartItemPromoSavings = cart.CartItemPromoSavings, ShippingPromoSavings = cart.ShippingPromoSavings, ShippingTotal = cart.ShippingTotal, IsCheckout = cart.IsCheckout, Items = cart.CartItems.Select(cc => new CartDto.CartItemDto { ProductId = cc.Product.ProductId, ProductName = cc.Product.Name, Price = cc.Price, Quantity = cc.Quantity, PromoSavings = cc.PromoSavings }).ToList() }); }
public Domain.Cart Execute(Domain.Cart cart) { if (cart == null) { throw new Exception("Cart is null."); } if (cart.CartItems != null && cart.CartItems?.Count() > 0) { cart.CartItemTotal = 0; foreach (CartItem item in cart.CartItems) { cart.CartItemPromoSavings = cart.CartItemPromoSavings + (item.PromoSavings * item.Quantity); cart.CartItemTotal = cart.CartItemTotal + (item.Product.Price * item.Quantity); } cart = _shippingGateway.CalculateShipping(cart); } cart = _promoGateway.ApplyShippingPromotions(cart); cart.CartTotal = AddTaxCost(cart.CartItemTotal + cart.ShippingTotal); return(cart); }
private async Task <Domain.Cart> InitCart(Domain.Cart cart = null, bool populatePrice = false) { if (cart == null) { cart = new Domain.Cart(); } if (populatePrice == false) { cart.CartItemPromoSavings = 0; cart.CartTotal = 0; cart.ShippingPromoSavings = 0; cart.ShippingTotal = 0; cart.CartItemTotal = 0; } if (cart.CartItems != null) { foreach (CartItem item in cart.CartItems) { var product = await _catalogService.GetProductByIdAsync(new GetProductByIdRequest { Id = item.Product.ProductId }); if (product == null) { throw new Exception("Could not find product."); } item.Product = new Product(product.Id, product.Name, product.Price, product.Desc); item.Price = product.Price; item.PromoSavings = 0; } } return(cart); }
private GetCartByIdResponse GetCartByIdResponse(Domain.Cart cart) { return(new GetCartByIdResponse { Id = cart.Id, CartTotal = cart.CartTotal, CartItemTotal = cart.CartItemTotal, CartItemPromoSavings = cart.CartItemPromoSavings, ShippingPromoSavings = cart.ShippingPromoSavings, ShippingTotal = cart.ShippingTotal, IsCheckout = cart.IsCheckout, Items = cart.CartItems.Select(cc => { return new GetCartByIdResponse.CartItemResponse { ProductId = cc.Product.ProductId, ProductName = cc.Product.Name, Price = cc.Price, Quantity = cc.Quantity, PromoSavings = cc.PromoSavings }; }).ToList() }); }
public static async Task <Domain.Cart> InitCart(this Domain.Cart cart, ICatalogGateway catalogGateway, bool isPopulatePrice = false) { if (cart == null) { cart = new Domain.Cart(); } if (isPopulatePrice == false) { cart.CartItemPromoSavings = 0; cart.CartTotal = 0; cart.ShippingPromoSavings = 0; cart.ShippingTotal = 0; cart.CartItemTotal = 0; } if (cart.CartItems == null) { return(cart); } foreach (var item in cart.CartItems) { var product = await catalogGateway.GetProductByIdAsync(item.Product.ProductId); if (product == null) { throw new Exception("Could not find product."); } item.Product = new Product(product.Id, product.Name, product.Price, product.Desc); item.Price = product.Price; item.PromoSavings = 0; } return(cart); }
public Domain.Cart ApplyCartItemPromotions(Domain.Cart cart) { // TODO: will calculate it later return(cart); }
public Domain.Cart CalculateShipping(Domain.Cart cart) { // TODO: will calculate it later return(cart); }