public ActionResult Details(int id) { Product p = ctx.Products.Find(id); Cart c = ctx.Carts.Where(crt => crt.SessionId == HttpContext.Session.SessionID).FirstOrDefault(); ProductViewModel pvm = new ProductViewModel(); pvm.Product = p; CartItemRequest cir = new CartItemRequest(); cir.CartId = c.Id; cir.ProductId = p.ProductId; cir.Count = 0; CartProduct cp = c.CartItems.Where(ci => ci.ProductId == p.ProductId).FirstOrDefault(); if (cp != null) { cir.Count = cp.Count; } pvm.ItemRequest = cir; return(View(pvm)); }
public async Task <(bool state, object response)> AddItemAsync(CartItemRequest request, int CartId) { CartItem cartItem = new CartItem { CartId = CartId, ProductId = request.ProductId, Quantity = request.Quantity, CreatedAt = DateTimeOffset.Now }; await _context.CartItems.AddAsync(cartItem); await _context.SaveChangesAsync(); cartItem = await _context.CartItems .Where(item => item.Id == cartItem.Id) .Include(i => i.Product) .FirstAsync(); if (cartItem == null || cartItem.Product == null) { return(false, new { message = "Something Went wrong, Product does not exists" }); } return(true, transform(cartItem)); }
private async Task <bool> AddItemToCart(Cart cart, CartItemRequest itemRequest) { Item item = await _itemRepository.FindByID(itemRequest.ItemID); if (item == null) { _logger.LogError("Item with ID - " + itemRequest.ItemID + " was not found."); return(false); } if (item.IsDeleted) { _logger.LogError("Item is archived - " + itemRequest.ItemID); return(false); } CartItem existingItem = cart.Items.Where(i => i.ItemID == item.ID).FirstOrDefault(); if (existingItem == null) { _logger.LogInformation("Adding new item to cart."); cart.Items.Add(new CartItem { Item = item, Count = itemRequest.Count }); } else { _logger.LogInformation("Item is already in cart, changing count from " + existingItem.Count + " to " + existingItem.Count + itemRequest.Count); existingItem.Count += itemRequest.Count; } return(true); }
public async Task <IActionResult> Put([FromBody] CartItemRequest itemRequest) { Cart cart = await GetUserCart(); bool itemAdded = await AddItemToCart(cart, itemRequest); if (itemAdded) { return(StatusCode((int)HttpStatusCode.NoContent)); } return(StatusCode((int)HttpStatusCode.NotFound, new ErrorResponse(ErrorReasons.NotFound, "This item is no loger available"))); }
public Task <StandardResponse> AddToCart([FromBody] CartItemRequest req) { // irrelevant currently // verify the session we want to access var from = Request.HttpContext.Session.Id; // seems to be the place to make sure the item isn't already in the cart //make sure the request is for the correct cart if (req.cart_id == _cart.GetCart()) { var item = int.Parse(req.hsmv); var canAdd = _cart.AddToCart(item); if (canAdd) { // record the addition to the cart var details = new ActionLog() { action = Log.Action.Add, target = item, cart_id = req.cart_id }; _log.Action(details); var result = new StandardResponse() { code = StatusCode(200), message = "success" }; return(Task.FromResult(result)); } } var error = new StandardResponse() { message = @"Your trying to access a cart that isn't yours or the session isn't active bruh. Or maybe the cart didn't add the item correctly." }; return(Task.FromResult(error)); }
/// <inheritdoc /> /// <summary> /// Changes the quantity of an item already in the cart A quantity of zero will remove the item from the cart altogether. <br><br><b>Permissions Needed:</b> SHOPPING_CARTS_ADMIN or owner /// </summary> /// <param name="id">The id of the cart</param> /// <param name="cartItemRequest">The cart item request object</param> public void UpdateItemInCart(string id, CartItemRequest cartItemRequest) { // verify the required parameter 'id' is set if (id == null) { throw new KnetikException(400, "Missing required parameter 'id' when calling UpdateItemInCart"); } mWebCallEvent.WebPath = "/carts/{id}/items"; if (!string.IsNullOrEmpty(mWebCallEvent.WebPath)) { mWebCallEvent.WebPath = mWebCallEvent.WebPath.Replace("{format}", "json"); } mWebCallEvent.WebPath = mWebCallEvent.WebPath.Replace("{" + "id" + "}", KnetikClient.ParameterToString(id)); mWebCallEvent.HeaderParams.Clear(); mWebCallEvent.QueryParams.Clear(); mWebCallEvent.AuthSettings.Clear(); mWebCallEvent.PostBody = null; mWebCallEvent.PostBody = KnetikClient.Serialize(cartItemRequest); // http body (model) parameter // authentication settings mWebCallEvent.AuthSettings.Add("oauth2_client_credentials_grant"); // authentication settings mWebCallEvent.AuthSettings.Add("oauth2_password_grant"); // make the HTTP request mUpdateItemInCartStartTime = DateTime.Now; mWebCallEvent.Context = mUpdateItemInCartResponseContext; mWebCallEvent.RequestType = KnetikRequestType.PUT; KnetikLogger.LogRequest(mUpdateItemInCartStartTime, "UpdateItemInCart", "Sending server request..."); KnetikGlobalEventSystem.Publish(mWebCallEvent); }
// public void AddItems(ProductViewModel value) public ActionResult AddItems(ProductViewModel pvm) { CartItemRequest cir = pvm.ItemRequest; Cart c = ctx.Carts.Where(crt => crt.SessionId == HttpContext.Session.SessionID).FirstOrDefault(); CartProduct curr = c.CartItems.Where(ci => ci.CartId == cir.CartId && ci.ProductId == cir.ProductId).FirstOrDefault(); if (curr != null) { curr.Count = cir.Count; } else { CartProduct cp = new CartProduct(); cp.ProductId = cir.ProductId; cp.CartId = cir.CartId; cp.Count = cir.Count; c.CartItems.Add(cp); } ctx.SaveChanges(); return(RedirectToAction("Index")); //return View("Index", c); }
public async Task <IActionResult> AddItemToCart(CartItemRequest request, int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var(success, resault) = await _cartServices.cart(id); if (success == false) { return(BadRequest(resault)); } var(state, response) = await _cartItemServices.AddItemAsync(request, id); if (state) { return(Ok(response)); } return(BadRequest(response)); }