public async Task <ShoppingCartItemVM> GetCartAsync() { var items = await _shoppingCartRepo.GetAllAsync(); var discounts = _discountFactory.GetDiscounts(); var model = new ShoppingCartItemVM { ShoppingCartItems = items, GrossTotal = CalculateTotalPrice(items), DiscountTotal = discounts.Calculate(items) }; return(model); }
public IHttpActionResult PostShoppingCartItem(ShoppingCartItemVM shoppingCartItemVM) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } shoppingCartItemVM.DatePlaced = DateTime.Now; // Map VM to an actual item. var mappedShoppingCartItem = Map.Map(shoppingCartItemVM); mappedShoppingCartItem.UserId = _user.UserId; // Check if there is already a quantity of the product in the shopping cart // for the user. var shoppingCartItem = Map.ShoppingCartRepo.RetrieveAll().Where( QItem => QItem.UserId == _user.UserId && QItem.ProductId == mappedShoppingCartItem.ProductId && QItem.Standard == mappedShoppingCartItem.Standard ).SingleOrDefault(); // If there are already matching items if (shoppingCartItem != null) { shoppingCartItem.Quantity += shoppingCartItemVM.Quantity; shoppingCartItem.DatePlaced = DateTime.Now; shoppingCartItem.UserId = _user.UserId; Map.ShoppingCartRepo.Update(shoppingCartItem); } else { // Make an item shoppingCartItem = Map.Map(shoppingCartItemVM); shoppingCartItem.DatePlaced = DateTime.Now; shoppingCartItem.UserId = _user.UserId; Map.ShoppingCartRepo.Insert(shoppingCartItem); } Map.ShoppingCartRepo.Save(); return(CreatedAtRoute("DefaultApi", new { id = shoppingCartItem.Id }, shoppingCartItem)); }
public HttpResponseMessage GetWishlistDetails(HttpRequestMessage request, int customerId) { return(CreateHttpResponse(request, () => { HttpResponseMessage response = request.CreateErrorResponse(HttpStatusCode.NotFound, "No items found"); if (true) { var customer = _customerService.GetCustomerById(customerId); var cart = customer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList(); var gridModel = new DataSourceResult { Data = cart.Select(sci => { decimal taxRate; var store = _storeService.GetStoreById(sci.StoreId); var sciModel = new ShoppingCartItemVM { Id = sci.Id, Store = store != null ? store.Name : "Unknown", ProductId = sci.ProductId, Quantity = sci.Quantity, ProductName = sci.Product.Name, AttributeInfo = _productAttributeFormatter.FormatAttributes(sci.Product, sci.AttributesXml, sci.Customer), UnitPrice = _priceFormatter.FormatPrice(_taxService.GetProductPrice(sci.Product, _priceCalculationService.GetUnitPrice(sci), out taxRate)), Total = _priceFormatter.FormatPrice(_taxService.GetProductPrice(sci.Product, _priceCalculationService.GetSubTotal(sci), out taxRate)), UpdatedOn = _dateTimeHelper.ConvertToUserTime(sci.UpdatedOnUtc, DateTimeKind.Utc) }; return sciModel; }), Total = cart.Count }; response = request.CreateResponse <DataSourceResult>(HttpStatusCode.OK, gridModel); } else { response = request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized user"); } return response; })); }
public IHttpActionResult PutShoppingCartItem(int id, ShoppingCartItemVM shoppingCartItemVM) { if (!ModelState.IsValid || shoppingCartItemVM.Id == null) { return(BadRequest(ModelState)); } //Check that item belongs to user before editing if (id != shoppingCartItemVM.Id || shoppingCartItemVM.UserId != _user.UserId) { return(BadRequest()); } // All is good in the hood. Update table. var shoppingCartItem = Map.ShoppingCartRepo.RetrieveById((int)shoppingCartItemVM.Id); shoppingCartItem.Quantity = shoppingCartItemVM.Quantity; Map.ShoppingCartRepo.Update(shoppingCartItem); try { Map.ShoppingCartRepo.Save(); } catch (DbUpdateConcurrencyException) { if (!ShoppingCartItemExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }