Пример #1
0
        public void CreateRecipe(RecipeWithIngredientsDto recipeWithIngredientsDTO)
        {
            // mapping
            var recipeToCreate = new Recipe
            {
                Title       = recipeWithIngredientsDTO.Title,
                Description = recipeWithIngredientsDTO.Description,
                Ingredients = recipeWithIngredientsDTO.Ingredients.Select(i => new Ingredient
                {
                    Id    = i.Id,
                    Name  = i.Name,
                    Unit  = i.Unit,
                    Value = i.Value
                }).ToList()
            };

            _unitOfWork.Recipes
            .Create(recipeToCreate);
            _unitOfWork.Complete();

            // map just created entities' Ids to recipeWithIngredientsDto input DTOs
            // to show them (Ids) in a response
            recipeWithIngredientsDTO.Id          = recipeToCreate.Id;
            recipeWithIngredientsDTO.Ingredients = recipeToCreate.Ingredients
                                                   .Select(i => new IngredientDto
            {
                Id    = i.Id,
                Name  = i.Name,
                Unit  = i.Unit,
                Value = i.Value
            });
        }
Пример #2
0
        public IActionResult Post([FromBody] RecipeWithIngredientsDto recipe)
        {
            if (recipe == null)
            {
                return(BadRequest("Recipe object is null"));
            }

            try
            {
                _recipeService.CreateRecipe(recipe);

                return(CreatedAtRoute("RecipeById", new { id = recipe.Id }, recipe));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, $"{ex.Message}"));
            }
        }