Exemple #1
0
        public async Task <IActionResult> Create([Bind("Id,Title,Content,CategoryId")] Post post)
        {
            if (IsInRank("admin") | IsInRank("author"))
            {
                var             userId   = User.FindFirstValue(ClaimTypes.NameIdentifier);
                var             userName = User.FindFirstValue(ClaimTypes.Name);
                ApplicationUser appUser  = await _userManager.GetUserAsync(User);

                post.Author   = appUser;
                post.AuthorId = userId;

                post.ApprovedId = 2;  //u bazi u tablici Approved je pod id-om 1 "Yes", a pod 2 je "No", Approved "Yes" postavlja editor ili admin kao odobrenje za objavu
                post.Approved   = _context.Approved.FirstOrDefault(a => a.Id == 2);

                if (ModelState.IsValid)
                {
                    _context.Add(post);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("MyPosts", "Posts"));
                }
                ViewData["CategoryId"] = new SelectList(_context.Set <Category>(), "Id", "Name", post.Category.Name);
                return(View(post));
            }
            return(RedirectToAction(nameof(Index)));
        }
Exemple #2
0
        public async Task <IActionResult> Create([Bind("Id,CommContent")] Comment comment, int postid)
        {
            var             userId  = User.FindFirstValue(ClaimTypes.NameIdentifier); // will give the user's userId
            ApplicationUser appUser = await _userManager.GetUserAsync(User);

            comment.User   = appUser;
            comment.UserId = userId;
            comment.PostId = postid;
            comment.Post   = _context.Post.Include(p => p.Author).Include(p => p.Category).Where(p => p.Id == postid).FirstOrDefault();

            if (ModelState.IsValid)
            {
                _context.Add(comment);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", postid));
            }

            return(View(comment));
        }