예제 #1
0
        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;
        }
예제 #2
0
        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;
        }
예제 #3
0
        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"]);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }