[HttpPost] [Route("MoveFromWishlistToCart")] public async Task<IActionResult> MoveFromWishlistToCart([FromBody] CartViewModel model) { var userId = User.FindFirstValue("userId"); if (userId == null) { return Unauthorized("User not logged in"); } var member = await _context.Members.FirstOrDefaultAsync(m => m.User_ID == int.Parse(userId)); if (member == null) { return Unauthorized("User is not a member"); } var wishlist = await _context.Wishlists.FirstOrDefaultAsync(w => w.Member_ID == member.Member_ID); if (wishlist == null) { return NotFound("Wishlist not found"); } var wishlistItem = await _context.Wishlist_Items .FirstOrDefaultAsync(w => w.Product_ID == model.Product_ID && w.Wishlist_ID == wishlist.Wishlist_ID); if (wishlistItem == null) { return NotFound("Product not found in the wishlist"); } var cart = await _context.Carts.FirstOrDefaultAsync(c => c.Member_ID == member.Member_ID); if (cart == null) { cart = new Cart { Member_ID = member.Member_ID }; _context.Carts.Add(cart); await _context.SaveChangesAsync(); } var existingCartItem = await _context.Cart_Items .FirstOrDefaultAsync(c => c.Product_ID == model.Product_ID && c.Cart_ID == cart.Cart_ID); if (existingCartItem != null) { existingCartItem.Quantity += 1; _context.Cart_Items.Update(existingCartItem); } else { var cartItem = new Cart_Item { Product_ID = model.Product_ID, Quantity = 1, Cart_ID = cart.Cart_ID }; _context.Cart_Items.Add(cartItem); } _context.Wishlist_Items.Remove(wishlistItem); await _context.SaveChangesAsync(); return Content("Product moved from wishlist to cart", "text/plain"); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter