Exemplo n.º 1
0
        public async Task <IActionResult> Like(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Article article =
                await _context.Articles.FirstOrDefaultAsync(a => a.Id == id);

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

            ApplicationUser user = await _userManager.GetUserAsync(User);

            Like like = new Like {
                Article = article, User = user
            };

            _context.Add(like);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Article", new { id = article.Id }));
        }
Exemplo n.º 2
0
        NewArticle(NewArticleViewModel newArticle)
        {
            if (!ModelState.IsValid)
            {
                return(View(newArticle));
            }

            if (!newArticle.CoverImage.IsPNGImage())
            {
                ModelState.AddModelError(nameof(newArticle.CoverImage),
                                         "Invalid PNG image.");
                return(View(newArticle));
            }

            string wwwrootDirectory = _webHostEnvironment.WebRootPath;
            string imagesDirectory  = Path.Combine(wwwrootDirectory, "images");
            string imageName        = Path.GetRandomFileName() + ".png";
            string imagePath        = Path.Combine(imagesDirectory, imageName);

            using (FileStream fileStream =
                       new FileStream(imagePath, FileMode.Create)) {
                newArticle.CoverImage.CopyTo(fileStream);
            }

            ApplicationUser user = await _userManager.GetUserAsync(User);

            Article article =
                new Article {
                Type   = newArticle.Type, Title = newArticle.Title,
                Body   = newArticle.Body, Image = imageName,
                Author = user
            };

            _context.Add(article);
            await _context.SaveChangesAsync();

            return(RedirectToAction("MyArticles"));
        }