public Article AddArticle(ArticleAddModel addModel, string email) { if (!IsUserAdmin(email)) return null; var userId = _db.Users.Single(x => x.Email == email).Id; var article = new Article { Body = addModel.Body, Title = addModel.Title, CreatedBy = userId, UpdatedBy = userId, DateCreated = DateTime.Now, DateUpdated = DateTime.Now }; var addedArticle = _db.Articles.Add(article); _db.SaveChanges(); return addedArticle; }
public bool ModifyArticle(ArticleAddModel model, string email) { if (!IsUserAdmin(email)) return false; var article = _db.Articles.Single(x => x.Id == model.Id); var userId = _db.Users.Single(x => x.Email == email).Id; article.Title = model.Title; article.Body = model.Body; article.UpdatedBy = userId; article.DateUpdated = DateTime.Now; _db.Articles.Attach(article); _db.Entry(article).State = EntityState.Modified; _db.SaveChanges(); return true; }
public void SubmitArticle_ValidModelDBFailsToCreateArticle_RedirectToAdd() { _fakeArticleService.Setup(x => x.AddArticle(It.IsAny<ArticleAddModel>(), It.IsAny<string>())).Returns((Article)null); var model = new ArticleAddModel {Body = "Test", Title = "Test article"}; var result = _articleController.SubmitArticle(model); Assert.AreEqual("Add", result.RouteValues["action"]); }
public void SubmitArticle_ValidModelDBCreatesArticle_RedirectToCreatesArticle() { _fakeArticleService.Setup(x => x.AddArticle(It.IsAny<ArticleAddModel>(), It.IsAny<string>())).Returns(_testArticle); var model = new ArticleAddModel {Body = "Test", Title = "Test article"}; var result = _articleController.SubmitArticle(model); var rvd = new RouteValueDictionary { {"id", _testArticle.Id }, {"action", "View"} }; Assert.AreEqual(rvd, result.RouteValues); }
public void ModifyArticle_ValidModelDBUpdatesArticle_RedirectsToEditedArticle() { _fakeArticleService.Setup(x => x.ModifyArticle(It.IsAny<ArticleAddModel>(), It.IsAny<string>())).Returns(true); var model = new ArticleAddModel { Title = "Test article", Id = 1, Body = "Body"}; var result = _articleController.ModifyArticle(model); var rvd = new RouteValueDictionary { {"id", model.Id }, {"action", "View"} }; Assert.AreEqual(rvd, result.RouteValues); }
public void ModifyArticle_ValidModelDBDoesNotUpdateArticle_RedirectsToEdit() { var model = new ArticleAddModel { Title = "Test article", Id = 1, Body = "Body"}; var result = _articleController.ModifyArticle(model); var rvd = new RouteValueDictionary { {"id", model.Id }, {"action", "Edit"} }; Assert.AreEqual(rvd, result.RouteValues); }
public void ModifyArticle_InvalidModel_RedirectsToEdit() { var model = new ArticleAddModel(); var result = _articleController.ModifyArticle(model); var rvd = new RouteValueDictionary { {"id", model.Id }, {"action", "Edit"} }; Assert.AreEqual(rvd, result.RouteValues); }