public ActionResult Edit(int id, RecipeDetailsModel updated) { if (ModelState.IsValid) { ro.UpdateRecipe(id, updated); return RedirectToAction("FullDetails", new { id = id }); } else { return View(updated); } }
public ActionResult Submit(RecipeDetailsModel r) { if (ModelState.IsValid) { var id = ro.AddRecipe(r); return RedirectToAction("FullDetails", new { id = id }); } else { return View(r); } }
public void UpdateRecipe(int id, RecipeDetailsModel updated) { using (var db = new RecipesEntities()) { var dbRecipe = db.Recipes.Where(r => r.ID == id).First(); dbRecipe.Name = updated.Name; dbRecipe.Summary = updated.Summary; dbRecipe.CookingTime = updated.CookingTimeInMinutes; dbRecipe.Ingredients = updated.Ingredients; dbRecipe.Instructions = updated.Instructions; db.SaveChanges(); } }
public HttpResponseMessage Post(RecipeDetailsModel value) { if (ModelState.IsValid) { var id = ro.AddRecipe(value); var resp = new HttpResponseMessage(HttpStatusCode.Created); resp.Headers.Location = new Uri("/api/recipe/" + id.ToString(), UriKind.Relative); return resp; } else { return new HttpResponseMessage(HttpStatusCode.BadRequest); } }
public int AddRecipe(RecipeDetailsModel r) { using (var db = new RecipesEntities()) { var toAdd = new Recipes { Name = r.Name, Summary = r.Summary, CookingTime = r.CookingTimeInMinutes, Ingredients = r.Ingredients, Instructions = r.Instructions }; db.Recipes.Add(toAdd); db.SaveChanges(); return toAdd.ID; } }
public HttpResponseMessage Put(int id, RecipeDetailsModel value) { if (ModelState.IsValid) { if (ro.GetRecipe(id) != null) { ro.UpdateRecipe(id, value); return new HttpResponseMessage(HttpStatusCode.OK); } else { return new HttpResponseMessage(HttpStatusCode.NotFound); } } else { return new HttpResponseMessage(HttpStatusCode.BadRequest); } }
public void PostSubmitAddsRecipeAndRedirectsCorrectly() { roStub.AddRecipe(null) .ReturnsForAnyArgs(42); var testModel = new RecipeDetailsModel(); var result = sut.Submit(testModel) as RedirectToRouteResult; Assert.IsNotNull(result, "Result redirects"); roStub.Received().AddRecipe(testModel); Assert.AreEqual(42, (int)result.RouteValues["id"], "Correct ID in redirect"); }
public void PostEditValidates() { var testModel = new RecipeDetailsModel(); sut.ModelState.AddModelError("dummy", "error"); var result = sut.Edit(1, testModel) as ViewResult; Assert.IsNotNull(result, "Get view result on error"); Assert.AreSame(testModel, result.Model); roStub.DidNotReceive().UpdateRecipe(1, Arg.Any<RecipeDetailsModel>()); }
public void PostEditUpdatesDetailsAndRedirects() { var testModel = new RecipeDetailsModel(); var result = sut.Edit(13, testModel) as RedirectToRouteResult; Assert.IsNotNull(result, "Result redirects"); roStub.Received().UpdateRecipe(13, testModel); Assert.AreEqual(13, (int)result.RouteValues["id"], "Correct ID in redirect"); }