Пример #1
0
 internal async Task <Cart> FindAsync(CartPostModel newItem)
 {
     return(await _bookstoreContext.Carts
            .Where(c => c.UserId == newItem.UserId)
            .Where(c => c.BookId == newItem.BookId)
            .FirstOrDefaultAsync());
 }
Пример #2
0
        public void AddToCart(CartPostModel input)
        {
            // var user = httpContext.HttpContext.User;
            //var claims = user.Claims;
            var data = new GoodsModel
            {
                _id    = 1,
                Name   = "鲜享黑米",
                Price  = 10,
                Remark = "鲜享黑米,不好吃不要钱!",
                Images = new List <ImageBase> {
                    new ImageBase {
                        Path = "http://a0.att.hudong.com/78/52/01200000123847134434529793168.jpg"
                    }
                },
                Types = new List <ClassifyModel> {
                    new ClassifyModel {
                        Remark = "谷物类", Name = "谷物类", _id = 1
                    }
                }
            };

            StaticData.CartUserGoods.TryAdd(1, new List <GoodsModel> {
                data
            });
        }
        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);
        }
Пример #4
0
 public async Task <WrapResult <object> > Post(CartPostModel input)
 {
     cartService.AddToCart(input);
     return(await Task.FromResult(new WrapResult <object> {
     }));
 }