예제 #1
0
        public IActionResult EditArticle(int id, [Bind("Author, Title, Text, ImageFile, Category")]
                                         Article article)
        {
            if (id != article.Id)
            {
                return(NotFound());
            }

            var savedArticle = ctx.Articles.FirstOrDefault(a => a.Id.Equals(id));

            if (savedArticle == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(View(article));
            }

            article.Image =
                article.ImageFile == null ?
                savedArticle.Image :
                SaveImage(article.ImageFile);
            article.CreatedDate = savedArticle.CreatedDate;
            article.Views       = savedArticle.Views;

            ctx.Articles.Update(article);
            ctx.SaveChanges();

            return(RedirectToAction(nameof(Index)));
        }
예제 #2
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using var ctx = new OctoCodesContext(serviceProvider.GetRequiredService <DbContextOptions <OctoCodesContext> >());

            if (ctx.Users.Any())
            {
                return;
            }

            ctx.Users.Add(
                new User
            {
                Username = "******",
                Password = AdminController.EncryptPassword("Password1")
            }
                );
            ctx.SaveChanges();
        }
예제 #3
0
        public IActionResult AddComment([Bind("ArticleId, Author, Text")] ArticleCommentsViewModel viewModel)
        {
            var article = ctx.Articles.First(article => article.Id == viewModel.ArticleId);

            if (article == null)
            {
                return(NotFound());
            }

            var comment = new Comment
            {
                CreatedDateTime = DateTime.Now,
                Author          = viewModel.Author,
                Article         = article,
                Text            = viewModel.Text,
            };

            ctx.Comments.Add(comment);
            ctx.SaveChanges();
            return(RedirectToAction(nameof(Article), new{ id = comment.Article.Id }));
        }