public async Task <IActionResult> CreateTest([FromBody] RecipeCreateView recipe)
        {
            if (ModelState.IsValid)
            {
                var ingredients = ingredientsService.GetAllByName(recipe.Ingredients).Select(x => x.Name).ToList();
                if (recipe.Ingredients.Count != ingredients.Count)
                {
                    var diff = recipe.Ingredients.Except(ingredients).ToList();

                    foreach (var ing in diff)
                    {
                        var newIng = new Ingredient();
                        newIng.Name = ing;

                        ingredientsService.Create(newIng);
                    }
                }
                var ingredientsDB = ingredientsService.GetAllByName(recipe.Ingredients);
                var recipeToDb    = recipe.ToRecipeCreate();

                var currentUser = await userManager.GetUserAsync(User);

                recipeService.Create(recipeToDb, ingredientsDB, currentUser.Id);

                return(Ok());
            }

            return(View(recipe));
        }
示例#2
0
        public async Task <IActionResult> Post([FromBody] PostRecipeInput item)
        {
            if (item == null)
            {
                return(BadRequest());
            }
            var recipe = await recipeService.Create(item);

            return(CreatedAtAction("Get", new { id = recipe.Id }, recipe));
        }
示例#3
0
        public ActionResult Create(Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                _recipeService.Create(recipe);
                return(RedirectToAction("Index"));
            }

            ViewBag.RecipeCategories = _recipeCategoryService.GetAll().ToList();
            return(View(recipe));
        }
示例#4
0
        public async Task <IActionResult> Create(CreateRecipeInputModel input, IFormCollection fc)
        {
            if (!ModelState.IsValid || !_categorieService.IsCategoryIdValid(input.CategoryId))
            {
                return(View(input));
            }

            var id = await _recipeService.Create(input, fc);

            return(RedirectToAction("Details", new { id }));
        }
示例#5
0
        public async Task <IActionResult> Post([FromBody] SaveRecipeRequest input)
        {
            var createRecipeInput = new CreateRecipeInput
            {
                Description = input.Description
            };

            await _recipeService.Create(createRecipeInput);

            return(Accepted());
        }
示例#6
0
        public IActionResult Add(Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                recipe.UserId = userManager.GetUserId(HttpContext.User);

                recipeService.Create(recipe);
            }

            return(RedirectToAction("List"));
        }
示例#7
0
        public Task SaveAsync(RecipeViewModel toSave)
        {
            var dataToSave = Map(toSave);

            if (toSave.Id == null)
            {
                return(_recipeService.Create(dataToSave));
            }

            return(_recipeService.Update(dataToSave));
        }
示例#8
0
 public ActionResult <CreateNewRecipeResponse> Create(CreateNewRecipeRequest request)
 {
     try
     {
         var result = _recipeService.Create(request);
         return(CreatedAtAction("GetRecipe", result));
     }
     catch
     {
         return(BadRequest(new { message = "Could not create recipe" }));
     }
 }
        public IActionResult Create(RecipeViewModel model)
        {
            if (ModelState.IsValid)
            {
                //ToDO: Create RecipeIngredients
                _recipeService.Create(model.Name, model.Description, model.Note, model.Image?.ToBytes(), model.CategoryId);

                return(RedirectToAction(nameof(Index)));
            }

            model.Categories = _categoryService.GetAll().ToSelectListItem <Category>(x => x.Name, x => x.Id.ToString());
            return(View(model));
        }
示例#10
0
        public async Task <IActionResult> Create([FromBody] CreateRecipe bundle)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _recipeService.Create(bundle);

            if (!result.IsSuccessed)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
        public async Task <IActionResult> CreateRecipe([FromBody] RecipePostPutModel data)
        {
            if (data == null)
            {
                ModelState.AddModelError("Body", "No body provided.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var createdId = await _recipeService.Create(data, CurrentUser.Id);

                return(Ok(createdId));
            }
            catch (Exception ex)
            {
                return(BadRequest($"Issue creating a new recipe: {ex.Message}"));
            }
        }
示例#12
0
        public RecipeModule(IAuthService _auth, IRecipeService _recipes)
            : base("/api/recipes")
        {
            this.Before = ctx => ModuleHelpers.VerifyJwt(ctx, _auth);
            Get("", async(req, res) =>
            {
                var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (user == null)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(new ErrorResponse {
                        Message = "Missing User from Token"
                    });
                    return;
                }
                var _page        = req.Query.FirstOrDefault(f => f.Key == "page").Value.FirstOrDefault();
                var _limit       = req.Query.FirstOrDefault(f => f.Key == "limit").Value.FirstOrDefault();
                var(page, limit) = ModuleHelpers.GetPagination(_page, _limit);
                var recipes      = await _recipes.FindByUser(user.Id ?? string.Empty, page, limit);
                await res.Negotiate(recipes);
                return;
            });

            Get("/{id}", async(req, res) =>
            {
                var strId  = req.RouteValues.As <string>("id");
                var recipe = await _recipes.FindOne(strId);
                var user   = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (recipe?.UserId != user?.Id)
                {
                    res.StatusCode = 403;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "You don't have access to this recipe",
                    }
                        );
                    return;
                }

                await res.Negotiate(recipe);
            });

            Post("", async(req, res) =>
            {
                var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (user?.Id == null)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(new ErrorResponse {
                        Message = "Missing User from Token"
                    });
                    return;
                }

                var(validationResult, payload) = await req.BindAndValidate <Recipe>();
                if (!validationResult.IsValid)
                {
                    res.StatusCode = 400;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "Failed Validation",
                        Errors  = validationResult.GetFormattedErrors()
                    }
                        );
                    return;
                }
                payload.UserId = user.Id;
                var recipe     = await _recipes.Create(payload);

                res.StatusCode = 201;
                await res.Negotiate(recipe);
            });

            Put("", async(req, res) =>
            {
                var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (user?.Id == null)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(new ErrorResponse {
                        Message = "Missing User from Token"
                    });
                    return;
                }

                var(validationResult, recipe) = await req.BindAndValidate <Recipe>();
                if (!validationResult.IsValid)
                {
                    res.StatusCode = 400;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "Failed Validation",
                        Errors  = validationResult.GetFormattedErrors()
                    }
                        );
                    return;
                }

                if (recipe.UserId != user.Id)
                {
                    res.StatusCode = 403;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "You don't have access to this recipe",
                    }
                        );
                    return;
                }

                var didUpdate = await _recipes.Update(recipe);
                await res.Negotiate(didUpdate);
            });

            Delete("/{id}", async(req, res) =>
            {
                var strId  = req.RouteValues.As <string>("id");
                var recipe = await _recipes.FindOne(strId);
                var user   = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (recipe.UserId != user?.Id)
                {
                    res.StatusCode = 403;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "You don't have access to this recipe",
                    }
                        );
                    return;
                }

                var didDestroy = await _recipes.Destroy(strId);
                if (!didDestroy)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "Failed To Delete This Recipe",
                    }
                        );
                    return;
                }

                res.StatusCode = 204;
            });
        }
示例#13
0
        public async Task <IActionResult> AddRecipe([FromBody] NewRecipeDto newRecipeDto)
        {
            var createRecipe = await _recipeService.Create(newRecipeDto);

            return(Ok(createRecipe));
        }