コード例 #1
0
        public ActionResult <ShopListRecipeModel> AddShopListRecipe(ShopListRecipeModel model)
        {
            try
            {
                if (model == null)
                {
                    return(BadRequest("ShopListRecipeModel is incorrect"));
                }

                var shopListRecipe = _mapper.Map <ShopListRecipe>(model);

                if (!_shopListRecipeService.ValidateAddShopListRecipe(shopListRecipe, model.ShopListId, model.RecipeId, out errorMessage))
                {
                    return(BadRequest(errorMessage));
                }

                if (_repository.SaveChanges())
                {
                    return(Created($"api/shoplistrecipes/{model.ShopListRecipeId}", _mapper.Map <ShopListRecipeModel>(shopListRecipe)));
                }

                return(BadRequest("Failed to add ShopListRecipe in database"));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside AddShopListRecipe Action: {ex}");
                return(StatusCode(StatusCodes.Status500InternalServerError, "Internal Server error"));
            }
        }
コード例 #2
0
        public ActionResult <ShopListRecipeModel> UpdateShopListRecipe(int id, ShopListRecipeModel model)
        {
            try
            {
                if (model == null)
                {
                    return(BadRequest("ShopListReipeModel is incorrect"));
                }

                var result = _repository.GetShopListRecipeById(id);

                if (result == null)
                {
                    return(NotFound($"Unable to find ShopListRecipe with id: {id}"));
                }

                if (!_shopListRecipeService.ValidateUpdateShopListRecipe(model.ShopListId, model.RecipeId, out errorMessage))
                {
                    return(BadRequest(errorMessage));
                }

                _mapper.Map(model, result);

                if (_repository.SaveChanges())
                {
                    return(_mapper.Map <ShopListRecipeModel>(result));
                }

                return(BadRequest("Failed to update ShopListRecipe in database"));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateShopListRecipe Action: {ex}");
                return(StatusCode(StatusCodes.Status500InternalServerError, "Internal Server error"));
            }
        }