public async Task <ActionResult <Cartdetail> > PostCartdetail(Cartdetail cartdetail) { _context.Cartdetails.Add(cartdetail); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCartdetail", new { id = cartdetail.Id }, cartdetail)); }
public async Task <IActionResult> PutCart(int id, Product product) { var cart = await _context.Carts.FirstAsync(c => c.Userid == id); var id_cart = cart.Id; var Cartdetails = await _context.Cartdetails.FirstOrDefaultAsync(p => p.Cartid == id_cart && p.Productid == product.Id); if (Cartdetails != null) { Cartdetails.Quanity = Cartdetails.Quanity + 1; } else { Cartdetail cartdetail11 = new Cartdetail() { Quanity = 1, Cartid = id_cart, Productid = product.Id, }; await _context.Cartdetails.AddAsync(cartdetail11); } // _context.Entry(cart).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CartExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Cartdetail> > PutCartdetail(int id, Cartdetail cartdetail) { cartdetail = await _context.Cartdetails.FindAsync(cartdetail.Id); if (id == -1) { if (cartdetail.Quanity > 1) { cartdetail.Quanity = cartdetail.Quanity - 1; _context.Entry(cartdetail).State = EntityState.Modified; } } else if (id == 1) { cartdetail.Quanity = cartdetail.Quanity + 1; _context.Entry(cartdetail).State = EntityState.Modified; } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CartdetailExists(id)) { return(NotFound()); } else { throw; } } return(cartdetail); }