Пример #1
0
        public async Task <IActionResult> ShowCategory(int id)
        {
            Category category = _context.Categories.Where(c => c.Id == id).Include(c => c.Topics).ThenInclude(t => t.Creator).SingleOrDefault();
            Topic    newTopic = TempData.Get <Topic>("NewTopic");

            if (newTopic == null)
            {
                newTopic            = new Topic();
                newTopic.CategoryId = category.Id;
            }
            ViewBag.NewTopic = newTopic;
            if (TempData["Errors"] != null)
            {
                ViewBag.Errors = TempData["Errors"];
            }
            User user = await GetCurrentUserAsync();

            User_Cateogry uc = _context.User_Cateogries.SingleOrDefault(c => c.CategoryId == id && c.ModeratorId == user.Id);

            if (uc != null)
            {
                ViewBag.IsModerator = true;
            }
            else
            {
                ViewBag.IsModerator = false;
            }
            ViewBag.AuthLevel = await GetCurrentUserAuthorizationLevelAsync();

            return(View("ShowCategory", category));
        }
Пример #2
0
        public async Task <IActionResult> ShowTopic(int id)
        {
            Comment newComment = TempData.Get <Comment>("NewComment");

            if (newComment == null)
            {
                newComment         = new Comment();
                newComment.TopicId = id;
            }
            ViewBag.NewComment = newComment;
            if (TempData["Errors"] != null)
            {
                ViewBag.Errors = TempData["Errors"];
            }
            Topic topic = _context.Topics.Where(t => t.Id == id).Include(t => t.Creator).Include(t => t.Comments).ThenInclude(c => c.Commentor).SingleOrDefault();

            topic.Views += 1;
            _context.SaveChanges();
            User user = await GetCurrentUserAsync();

            ViewBag.UserId    = user.Id;
            ViewBag.AuthLevel = await GetCurrentUserAuthorizationLevelAsync();

            User_Cateogry uc = _context.User_Cateogries.SingleOrDefault(c => c.CategoryId == topic.CategoryId && c.ModeratorId == user.Id);

            if (uc != null)
            {
                ViewBag.IsModerator = true;
            }
            else
            {
                ViewBag.IsModerator = false;
            }
            return(View("ShowTopic", topic));
        }
Пример #3
0
        public async Task <bool> UpdateModerator(int categoryId, string userId)
        {
            User   user        = _context.Users.Where(u => u.Id == userId).Include(u => u.ModeratedCategories).SingleOrDefault();
            string currentRole = (await _userManager.GetRolesAsync(user))[0];

            if (user.ModeratedCategories.Any(mc => mc.CategoryId == categoryId))
            {
                _context.User_Cateogries.Remove(user.ModeratedCategories.SingleOrDefault(uc => uc.CategoryId == categoryId));
                if (user.ModeratedCategories.Count == 1)
                {
                    if (currentRole != "admin")
                    {
                        await _userManager.RemoveFromRoleAsync(user, "moderator");

                        await _userManager.AddToRoleAsync(user, "basic");
                    }
                }
            }
            else
            {
                User_Cateogry uc = new User_Cateogry();
                uc.ModeratorId = userId;
                uc.CategoryId  = categoryId;
                _context.Add(uc);
                if (currentRole != "admin")
                {
                    await _userManager.RemoveFromRoleAsync(user, "basic");

                    await _userManager.AddToRoleAsync(user, "moderator");
                }
            }
            _context.SaveChanges();
            return(true);
        }