Esempio n. 1
0
        public async Task <IActionResult> CreateRecipeAsync([FromBody] RecipeCreationViewModel model)
        {
            var result = await _service.AddAsync(model);

            result.Url = this.Url.RouteUrl("GetRecipeById", new { id = result.Id });
            return(CreatedAtRoute("GetRecipeById", new { id = result.Id }, result));
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateRecipeAsync(int id, [FromBody] CreateRecipe command)
        {
            if (id != AccountID)
            {
                return(Forbid());
            }

            var user = await _userService.GetAsync(id);

            var recipe = await _recipeService.AddAsync(command, user);

            var recipeDto = _mapper.Map <RecipeDto>(recipe);

            return(Created($"{Request.Host}{Request.Path}/{recipe.Id}", recipeDto));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddAsync(SaveRecipeDto recipeDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var recipe   = _mapper.Map <SaveRecipeDto, Recipe>(recipeDto);
            var response = await _recipeService.AddAsync(recipe);

            if (!response.Success)
            {
                return(BadRequest(new { response.Error }));
            }

            var resource = _mapper.Map <Recipe, RecipesDto>(response.Resource);

            return(Ok(resource));
        }
Esempio n. 4
0
        public async Task <HttpResponseMessage> Create([FromBody] RecipeDTO recipeDTO)
        {
            if (!ModelState.IsValid)
            {
                string errorMessage = string.Empty;

                foreach (ModelState keyValuePairs in ModelState.Values)
                {
                    foreach (ModelError modelError in keyValuePairs.Errors)
                    {
                        errorMessage += " - " + modelError.ErrorMessage;
                    }
                }

                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage));
            }

            if (await RecipeService.AddAsync(recipeDTO) != null)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, new { message = "Recipe was succesfully created" }));
            }

            return(Request.CreateResponse(HttpStatusCode.InternalServerError, new { message = "Something horrible went wrong." }));
        }