public TemporaryReservation MakeATemporaryReservation(AddProduct addProduct) { var productStock = _productStocks.Get(addProduct.ProductId); var temporaryReservation = productStock.MakeATemporaryReservation(addProduct.Quantity); if (temporaryReservation != null) { _productStocks.Save(productStock); } return(temporaryReservation); }
public HttpResponseMessage AddProduct(AddProduct addProduct) { var productStock = _productStocks.Get(addProduct.ProductId); return(productStock .MakeATemporaryReservation_(addProduct.Quantity) .ContinueWith(temporaryReservation => { _productStocks.Save(productStock); var cart = _carts.Get(addProduct.CartId); return cart.Add_(addProduct.ProductId, addProduct.Quantity, temporaryReservation); }) .ContinueWith(cart => { _carts.Save(cart); return Either <HttpResponseMessage, Error> .Left(new HttpResponseMessage(HttpStatusCode.OK)); }) .OnError(error => new HttpResponseMessage(HttpStatusCode.BadRequest)) .Result()); }
public HttpResponseMessage AddProduct(AddProduct addProduct) { var productStock = _productStocks.Get(addProduct.ProductId); try { var temporaryReservation = productStock.MakeATemporaryReservation(addProduct.Quantity); _productStocks.Save(productStock); var cart = _carts.Get(addProduct.CartId); cart.Add(addProduct.ProductId, addProduct.Quantity, temporaryReservation); _carts.Save(cart); } catch (BusinessException e) { var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Forbidden) { Content = new StringContent(e.Message) }; return(httpResponseMessage); } return(new HttpResponseMessage(HttpStatusCode.Created)); }
public HttpResponseMessage AddProduct(AddProduct addProduct) { var productStock = _productStocks.Get(addProduct.ProductId); var temporaryReservation = productStock.MakeATemporaryReservation(addProduct.Quantity); if (temporaryReservation != null) { _productStocks.Save(productStock); var cart = _carts.Get(addProduct.CartId); if (cart.Add(addProduct.ProductId, addProduct.Quantity, temporaryReservation)) { _carts.Save(cart); } else { return(Error("Sorry, cannot add to cart")); } } else { return(Error("Sorry, not enough stock")); } return(new HttpResponseMessage(HttpStatusCode.Created)); }