Ejemplo n.º 1
0
        public async Task <ActionResult <CartItem> > PostCartItem(CartItem cart)
        {
            if (cart == null)
            {
                throw new System.ArgumentNullException(nameof(cart));
            }
            else if (cart.BookItems == null)
            {
                throw new System.ArgumentNullException(nameof(cart.BookItems));
            }

            // Validate user
            var userId   = cart.UserId;
            var userName = cart.UserName;
            var password = cart.Password;

            // If validation fails, return bad request
            if (!CommonOperations.ValidateUser(userId, userName, password))
            {
                return(BadRequest());
            }

            // Retrieving all books from cart
            ICollection <BookItem> books = await CommonOperations.ExtractBooks(cart, _context);

            cart.BookItems = books;

            // Setting the initial status
            cart.Status = "open";

            _context.CartItems.Add(cart);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetCartItem), new { id = cart.CartId }, cart));
        }