Ejemplo n.º 1
0
        public async Task <ActionResult <Order> > CreateOrder()
        {
            var user = await _userManager.FindByEmailFromClaimsPrinciple(HttpContext.User);

            if (user == null)
            {
                return(Unauthorized(new ErrorRes(401)));
            }
            // get basket from the repo
            var basket = await _basketRepo.GetCarts(user.Id);

            var total = _basketRepo.GetTotal(basket);

            if (user.address == null)
            {
                return(Unauthorized(new ErrorRes(400, "You must fill your address to create an order")));
            }
            // create order items
            var items = new List <OrderItem>();

            foreach (var item in basket)
            {
                var orderItem = new OrderItem(item.Id, item.Product.Name, item.Product.PictureUrl, item.Product.Price, item.Quantity);
                items.Add(orderItem);
            }

            // create order
            var order = new Order(items, user.Email, user.address, total);

            //Add Order
            _orderGenericsRepo.Add(order);
            // save to db
            if (await _orderGenericsRepo.SaveAll())
            {
                return(Ok(_mapper.Map <Order, OrderReturnDTO>(order)));
            }
            return(BadRequest(new ErrorRes(400, "Problem creating order")));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <ReturnCheckout> > GetBaskets()
        {
            var user = await _userManager.FindByEmailFromClaimsPrinciple(HttpContext.User);

            if (user == null)
            {
                return(Unauthorized(new ErrorRes(401)));
            }
            var basketItems = await _basketRepo.GetCarts(user.Id);

            return(Ok(new ReturnCheckout(_mapper.Map <IReadOnlyList <BasketItem>, IReadOnlyList <ReturnBasket> > (basketItems), _basketRepo.GetTotal(basketItems), _basketRepo.getTotalItems(basketItems))));
        }