Ejemplo n.º 1
0
        public async Task <IActionResult> post(ListCartRequest request, bool isReduce = false)
        {
            var userId = Guid.Parse(User.Claims.First(x => x.Type == "userId").Value.ToString());
            var result = await cartService.Post(userId, request, isReduce);

            return(Ok(result));
        }
Ejemplo n.º 2
0
        public ApiResult <object> CreateOrder(ListCartRequest ListCartRequest)
        {
            List <CartViewModel> cartViews = new List <CartViewModel>();
            float totalPrice = 0;

            ListCartRequest?.CartRequests.ForEach(cart =>
            {
                var book = eShopDbContext.Books.Find(cart.bookId);
                if (book != null && book.Available > 0)
                {
                    cartViews.Add(new CartViewModel()
                    {
                        bookId    = book.Id,
                        bookName  = book.Name,
                        bookImage = book.BookImage,
                        price     = book.Price,
                        sale      = book.Sale,
                        quantity  = cart.quantity > book.Available ? book.Available : cart.quantity,
                    });

                    totalPrice += book.Price * (1 - book.Sale) * (cart.quantity > book.Available ? book.Available : cart.quantity);
                }
            });

            return(new ApiResult <object>(success: true, messge: "Thanh cong", payload: new { products = cartViews, totalPrice = totalPrice }));
        }
Ejemplo n.º 3
0
        public async Task <ApiResult <bool> > AddOrder(ListCartRequest ListCartRequest, OrderRequest OrderRequest)
        {
            float totalPrice = 0;
            List <OrderDetail> orderDetails = new List <OrderDetail>();

            ListCartRequest.CartRequests.ForEach(cart =>
            {
                var book = eShopDbContext.Books.Find(cart.bookId);
                if (book != null && book.Available > 0)
                {
                    var bookPrice = (book.Price - book.Price * book.Sale) * cart.quantity;
                    totalPrice   += bookPrice;
                    orderDetails.Add(new OrderDetail()
                    {
                        BookId     = book.Id,
                        Quantity   = cart.quantity > book.Available ? book.Available : cart.quantity,
                        TotalPrice = bookPrice,
                    });
                    book.Available = cart.quantity > book.Available ? 0 : book.Available - cart.quantity;

                    //NOTE : tinh diem pho bien
                    book.WeekScore  += 10 * cart.quantity;
                    book.MonthScore += 10 * cart.quantity;
                    book.YearScore  += 10 * cart.quantity;
                }
            });

            if (orderDetails.Count > 0)
            {
                var newOrder = new Order()
                {
                    UserId       = OrderRequest.UserId,
                    Address      = OrderRequest.Address,
                    TotalPrice   = totalPrice,
                    OrderDetails = orderDetails,
                    DateCreate   = DateTime.Now
                };
                eShopDbContext.Orders.Add(newOrder);
            }

            var dbResult = await eShopDbContext.SaveChangesAsync();

            if (orderDetails.Count < 1)
            {
                return(new ApiResult <bool>(success: false, messge: "Dat hang khong thanh cong", payload: false));
            }

            if (dbResult > 0)
            {
                return(new ApiResult <bool>(success: true, messge: "Da dat hang thanh cong", payload: true));
            }
            return(new ApiResult <bool>(success: false, messge: "Dat hang khong thanh cong", payload: false));
        }
        public async Task <IActionResult> Get()
        {
            try
            {
                var request = new ListCartRequest();
                request.UserId = Guid.Parse(User.FindFirst(ClaimTypes.PrimarySid).Value);

                var response = await _mediator.Send(request, CancellationToken.None);

                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 5
0
 public ListCartResponse ListCart(ListCartRequest request)
 {
     try
     {
         _logger.LogInformation("Enter");
         var itemData = _cartRepository.ListCart(request.UserId);
         var response = BuildResponseFromItemData(itemData);
         _logger.LogInformation("Exit");
         return(response);
     }
     catch (Exception e)
     {
         _logger.LogError($"Exception {e}");
         return(null);
     }
 }
Ejemplo n.º 6
0
        public async Task <ApiResult <List <CartViewModel> > > Post(Guid userId, ListCartRequest listCart, bool isReduce)
        {
            listCart?.CartRequests.ForEach(x =>
            {
                var temp = eShopDbContext.CartItems.Find(userId, x.bookId);
                var book = eShopDbContext.Books.Find(x.bookId);

                if (temp != null)
                {
                    if (isReduce)
                    {
                        if (x.quantity == 0)
                        {
                            eShopDbContext.CartItems.Remove(temp);
                        }
                        else
                        {
                            temp.Quantity = x.quantity > book.Available ? book.Available : x.quantity;
                        }
                    }
                    else
                    {
                        if (book.Available > 0)
                        {
                            temp.Quantity = temp.Quantity + x.quantity > book.Available ? book.Available : temp.Quantity + x.quantity;
                        }
                    }
                }
                else
                {
                    if (book.Available > 0)
                    {
                        eShopDbContext.CartItems.AddAsync(new CartItem()
                        {
                            UserId   = userId,
                            BookId   = x.bookId,
                            Quantity = x.quantity > book.Available ? book.Available : x.quantity,
                        });
                    }
                }
            });

            var db = await eShopDbContext.SaveChangesAsync();

            var data = from user in eShopDbContext.Users
                       join cart in eShopDbContext.CartItems on user.Id equals cart.UserId
                       join book in eShopDbContext.Books on cart.BookId equals book.Id
                       select new { user = user, book = book, quantity = cart.Quantity };

            var result = data?.Select(x =>
                                      new CartViewModel()
            {
                bookId    = x.book.Id,
                bookName  = x.book.Name,
                price     = x.book.Price,
                sale      = x.book.Sale,
                bookImage = x.book.BookImage,
                quantity  = x.quantity
            }

                                      ).ToList();

            if (result != null)
            {
                return(new ApiResult <List <CartViewModel> >(success: true, messge: "Thanh cong", payload: result));
            }
            return(new ApiResult <List <CartViewModel> >(success: false, messge: "Khong co du lieu tra ve", payload: null));
        }
Ejemplo n.º 7
0
        public IActionResult create([FromBody] ListCartRequest listCartRequest)
        {
            var result = orderService.CreateOrder(listCartRequest);

            return(Ok(result));
        }