コード例 #1
0
        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 }));
        }
コード例 #2
0
        public async Task <ActionResult <Cart> > PostCart(CartPostModel NewItem)
        {
            //Kiem tra hop le
            if (!ModelState.IsValid)
            {
                return(Ok(new { error_message = "Loi gio hang" }));
            }

            //Kiem tra sach co ton tai
            Book book = await _bookServices.GetBookById(NewItem.BookId);

            if (book is null)
            {
                return(Ok(new { error_message = "Sach khong ton tai" }));
            }

            //Kiem tra so luong
            if (book.Quantity <= 0)
            {
                return(Ok(new { error_message = "Sach da het so luong" }));
            }

            //Kiem tra so luong ton
            if (book.Quantity - NewItem.Amount <= 0)
            {
                return(Ok(new { error_message = "So luong sach con lai trong kho chi con " + NewItem.Amount }));
            }


            Cart CurrentCart = await _cartServices.FindAsync(NewItem);

            if (CurrentCart is not null)
            {
                if (NewItem.Amount <= 0)
                {
                    List <Cart> carts = await _cartServices.DeleteCartById(CurrentCart);

                    return(Ok(new { data = carts, success = true }));
                }

                //Cap nhat lai gio hang
                CurrentCart.Amount  += NewItem.Amount;
                CurrentCart.SubTotal = CurrentCart.Amount * book.Price;
                if (await _cartServices.UpdateAsync(CurrentCart))
                {
                    return(Ok(new { data = await _cartServices.GetCartFromUser(NewItem.UserId), success = true }));
                }
                else
                {
                    return(Ok(new { error_message = "Co loi khi cap nhat gio hang" }));
                }
            }
            else
            {
                if (NewItem.Amount <= 0)
                {
                    return(Ok(new { error_message = "So luong sach khong hop le" }));
                }

                Cart cart = new Cart
                {
                    UserId   = NewItem.UserId,
                    BookId   = NewItem.BookId,
                    Amount   = NewItem.Amount,
                    SubTotal = NewItem.Amount * book.Price
                };
                if (await _cartServices.AddNewCartAsync(cart))
                {
                    return(Ok(new { data = await _cartServices.GetCartFromUser(NewItem.UserId), success = true }));
                }
                else
                {
                    return(Ok(new { error_message = "Loi them gio hang" }));
                }
            }
            //return CreatedAtAction("GetCart", new { id = cart.Id }, cart);
        }