예제 #1
0
 public ActionResult Edit(int id, RecipeDetailsModel updated)
 {
     if (ModelState.IsValid)
     {
         ro.UpdateRecipe(id, updated);
         return RedirectToAction("FullDetails", new { id = id });
     }
     else
     {
         return View(updated);
     }
 }
예제 #2
0
 public ActionResult Submit(RecipeDetailsModel r)
 {
     if (ModelState.IsValid)
     {
         var id = ro.AddRecipe(r);
         return RedirectToAction("FullDetails", new { id = id });
     }
     else
     {
         return View(r);
     }
 }
예제 #3
0
 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();
     }
 }
예제 #4
0
 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);
     }
 }
예제 #5
0
 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;
     }
 }
예제 #6
0
 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);
     }
 }
예제 #7
0
        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");
        }
예제 #8
0
        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>());
        }
예제 #9
0
        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");
        }