//Add a new recipe to the JSON storage. private void AddRecipe(object sender, RoutedEventArgs e) { //Get the user inputs. string recipeName = RecipeNameInput.Text; string recipeTime = RecipeTimeInput.Text; string jsonToOutput; using (var r = new StreamReader("recipes.json")) { //Read the JSON file and convert it to an array. string json = r.ReadToEnd(); JsonConvert.DeserializeObject(json); var jsonArray = JArray.Parse(json); //Create a new JSON element which is created from the users input and add save the file again. var newRecipe = new JObject { ["name"] = recipeName, ["time"] = recipeTime }; jsonArray.Add(newRecipe); jsonToOutput = JsonConvert.SerializeObject(jsonArray, Formatting.Indented); } File.WriteAllText($"{Directory.GetCurrentDirectory()}/recipes.json", jsonToOutput); FillRecipesBar(); //Call an event so the MainWindow dropdown can update its recipes. RecipeUpdate?.Invoke(); Close(); }
/// <inheritdoc /> public async Task UpdateAsync(long id, RecipeUpdate recipe) { var entity = await _repo.GetAsync(id); _repo.Update(entity); _mapper.Map(recipe, entity); await _repo.SaveAsync(); }
/// <summary> /// This updates a recipe in the database /// </summary> /// <param name="recipe"></param> /// <returns>returns a 200 Ok to let you know it was completed</returns> public IHttpActionResult Put(RecipeUpdate recipe) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateRecipeService(); if (!service.UpdateRecipe(recipe)) { return(InternalServerError()); } return(Ok()); }
public bool UpdateRecipe(RecipeUpdate model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Recipes .Single(e => e.RecipeId == model.RecipeId && e.OwnerId == _userId); entity.RecipeName = model.RecipeName; entity.TypeofCuisine = model.TypeofCuisine; entity.TypeOfDish = model.TypeOfDish; entity.Ingredients = model.Ingredients; entity.Instructions = model.Instructions; entity.SourceId = model.SourceId; return(ctx.SaveChanges() == 1); } }
public RecipeUpdate UpdateRecipeModelLoadPlans(RecipeUpdate model) { using (var ctx = new ApplicationDbContext()) { var query = ctx .Plans .Where(plan => plan.UserId == _userId && plan.IsSaved == true) .Select(plan => new PlanListItem { PlanId = plan.PlanId, Title = plan.Title }).ToList(); model.Plans = query; return(model); } }
public bool UpdateRecipe(RecipeUpdate model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Recipes .Single(e => e.RecipeId == model.RecipeId && e.UserId == _userId && e.IsSaved == true); entity.Name = model.Name; entity.Directions = model.Directions; entity.Ingredients = model.Ingredients; entity.Steps = model.Steps; if (model.PlanId != null) { entity.Plan = ctx.Plans.Single(p => p.PlanId == model.PlanId); } return(ctx.SaveChanges() >= 1); } }
//Remove a JSON file from storage. private void RemoveRecipe(object sender, RoutedEventArgs e) { int counter = 0; string jsonToOutput; using (var r = new StreamReader("recipes.json")) { //Read the JSON file and convert it to an array. var id = 0; string json = r.ReadToEnd(); dynamic array = JsonConvert.DeserializeObject(json); //Get the number of the selected recipe. foreach (var item in array) { if (item.name == cbRecipe.SelectedItem.ToString()) { id = counter; } counter++; } cbRecipe.SelectedItem = 1; //Convert to array list, remove the selected recipe, and convert it back to json so it can be saved. ArrayList arrLst = new ArrayList(array); arrLst.RemoveAt(id); dynamic editedArray = arrLst.ToArray(); jsonToOutput = JsonConvert.SerializeObject(editedArray, Formatting.Indented); } File.WriteAllText($"{Directory.GetCurrentDirectory()}/recipes.json", jsonToOutput); FillRecipesBar(); //Call an event so the MainWindow dropdown can update its recipes. RecipeUpdate?.Invoke(); Close(); }
// GET: Recipe/Update/{id} public ActionResult Edit(int id) { var service = CreateRecipeService(); try { var detail = service.GetRecipeById(id); var model = new RecipeUpdate { RecipeId = detail.RecipeId, Name = detail.Name, Directions = detail.Directions, Ingredients = detail.Ingredients, Steps = detail.Steps }; return(View(service.UpdateRecipeModelLoadPlans(model))); } catch (InvalidOperationException) { TempData["NoResult"] = "The Recipe could not be found."; return(RedirectToAction("Index")); } }
public ActionResult Edit(int id, RecipeUpdate model) { var service = CreateRecipeService(); if (!ModelState.IsValid) { return(View(service.UpdateRecipeModelLoadPlans(model))); } if (model.RecipeId != id) { ModelState.AddModelError("", "ID Mismatch"); return(View(service.UpdateRecipeModelLoadPlans(model))); } if (service.UpdateRecipe(model)) { TempData["SaveResult"] = "Your Recipe was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Your Recipe could not be updated."); return(View(service.UpdateRecipeModelLoadPlans(model))); }
public async Task <IActionResult> Update(int id, [FromBody] RecipeUpdate recipe) { await _recipeService.UpdateAsync(id, recipe); return(NoContent()); }