public async Task <IActionResult> DeleteCartByUser(int id) { bool IsDelete = await _cartServices.DeleteCartAsync(id); if (IsDelete) { return(Ok(new { data = "Xoa thanh cong", success = true })); } else { return(Ok(new { data = "Gio hang khong ton tai", success = true })); } }
public async Task <IActionResult> PostInvoice(InvoicePostModel model) { //_context.Invoices.Add(invoice); //await _context.SaveChangesAsync(); //return CreatedAtAction("GetInvoice", new { id = invoice.Id }, invoice); //Tạo ra 1 invoice cho user khi nhấn thanh toán var invoice = _mapper.Map <Invoice>(model); await _context.Invoices.AddAsync(invoice); bool isSave = await _invoiceService.SaveInvoiceAsync(invoice); if (!isSave) { return(Ok(new { error_message = "Khong the luu don hang do co loi xay ra" })); } //Lấy món hàng trong cart rồi thêm vào InvoiceDetail var cartItems = await _cartServices.GetCartFromUser(model.UserId); if (cartItems.Count == 0) { return(Ok(new { error_message = "Gio hang rong, xin vui long mua hang" })); } var invoiceDetails = _mapper.Map <IList <InvoiceDetail> >(cartItems); //Gán id Invoice vào InvoiceDetail foreach (var item in invoiceDetails) { item.InvoiceId = invoice.Id; } //Lưu vào db isSave = await _invoiceDetailsService.SaveToDatabase(invoiceDetails); if (!isSave) { return(Ok(new { error_message = "Khong the luu chi tiet don hang do co loi xay ra" })); } await _cartServices.DeleteCartAsync(model.UserId); invoice.TotalMoney = invoiceDetails.Sum(i => i.SubTotal); isSave = await _invoiceService.SaveInvoiceAsync(invoice); return(Ok(new { data = invoiceDetails, success = true })); }
public async Task <IActionResult> DeleteCartByUser(int id) { bool IsDeleteRedis = await database.KeyDeleteAsync(id.ToString()); if (IsDeleteRedis) { await database.KeyDeleteAsync(id.ToString()); return(Ok(new { data = "Xoa thanh cong", success = true })); } IsDeleteRedis = await _cartServices.DeleteCartAsync(id); if (IsDeleteRedis) { return(Ok(new { data = "Xoa thanh cong", success = true })); } return(Ok(new { data = "Gio hang khong ton tai", success = true })); }