Ejemplo n.º 1
0
        public async Task <ActionResult <CartItemModel> > AddItemToCart(int cartId, int productId)
        {
            try
            {
                var location = _linkGenerator.GetPathByAction("GetCartItem", "Carts", new { cartId, productId });

                if (string.IsNullOrEmpty(location))
                {
                    return(BadRequest("Could not use current cart id and product id"));
                }

                var item = await _cartRepository.AddItemShoppingCartAsync(productId, cartId);

                if (await _cartRepository.SaveChangesAsync())
                {
                    return(Created(location, _mapper.Map <CartItemModel>(item)));
                }

                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Opps!"));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Something went wrong!"));
            }
        }