Esempio n. 1
0
        public async Task <IActionResult> Update(long id)
        {
            Post post = await _postService.GetById(id);

            post.Approved       = true;
            post.approvedByUser = await GetCurrentUserAsync();

            post.approvedByUserId = post.approvedByUser.Id;
            try
            {
                await _postService.Update(post);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_postService.PostExists(post.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            var userRoles = await _userManager.GetRolesAsync(post.UserId);

            if (!userRoles.Contains("Admin"))
            {
                int countPostsForCategory = _userService.CountApprovedPostsFromUserForCategory(post.UserId.Id, post.Category.ID);
                if (countPostsForCategory == post.Category.PostRule)
                {
                    //zapisi u baza kaj m-n relacijata
                    UserCategory obj = new UserCategory();
                    obj.UserId     = post.UserId.Id;
                    obj.User       = post.UserId;
                    obj.CategoryId = post.CategoryId;
                    obj.Category   = post.Category;

                    await _userCategoryService.Insert(obj);

                    var result = await _userManager.AddToRoleAsync(post.UserId, "Moderator");
                }
            }

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(long id, [Bind("ID,CategoryName,PostRule")] Category category)
        {
            ViewData["Header"] = "BlogApp";
            if (id != category.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                //novo
                Category notUpdated = await _categoryService.getById(id);

                try
                {
                    await _categoryService.Update(category);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!_categoryService.CategoryExists(category.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                if (notUpdated.PostRule < category.PostRule)
                {
                    //nekoi od mometalnite moderatori za kategorijata mozebi ke treba da im se odzeme taa nadleznost
                    //bidejki so zgolemuvanjeto na uslovot(PostRule) mozebi veke ne go ispolnuvaaat
                    List <ApplicationUser> categoryModerators = _userCategoryService.FindModeratorsForCategory(category.ID);
                    foreach (ApplicationUser user in categoryModerators)
                    {
                        int userPostsOnCategory = _userService.CountApprovedPostsFromUserForCategory(user.Id, category.ID);
                        if (userPostsOnCategory < category.PostRule)
                        {
                            int moderatorCategories = _userService.countModeratorCategories(user.Id);
                            moderatorCategories -= 1;

                            UserCategory obj = new UserCategory();
                            obj.UserId     = user.Id;
                            obj.User       = user;
                            obj.CategoryId = category.ID;
                            obj.Category   = category;
                            int deleteResult = await _userCategoryService.DeleteAsync(obj);

                            if (moderatorCategories == 0)
                            {
                                var result = await _userManager.RemoveFromRoleAsync(user, "Moderator");
                            }
                        }
                    }
                }
                else if (notUpdated.PostRule > category.PostRule)
                {
                    //so namaluvanjeto na uslovot da se bide moderator za odredena kategorija mozebi ke treba da se
                    //dodadat novi korisnici za moderator na taa kategorija bidejki so nameluvanjeto nauslovot istiot ke go zadovoluvaat

                    //da se selektiraat site useri sto ne se Admini,
                    //a imaat dovolno approved postovi za da stanat Moderatori za dadenata kategorija

                    List <string> nonAdminUsers = _userService.AllNonAdminUsersId();
                    foreach (string userId in nonAdminUsers)
                    {
                        List <ApplicationUser> alreadyModeratorForCategory = _userService.ModeratorForCategory(userId, category.ID);
                        if (alreadyModeratorForCategory.Count == 0)
                        {
                            int approvedPostsForCategory = _userService.CountApprovedPostsFromUserForCategory(userId, id);

                            if (approvedPostsForCategory >= category.PostRule)
                            {
                                ApplicationUser newModerator = _userService.getById(userId);
                                UserCategory    obj          = new UserCategory();
                                obj.UserId     = userId;
                                obj.User       = newModerator;
                                obj.CategoryId = category.ID;
                                obj.Category   = category;
                                await _userCategoryService.Insert(obj);

                                var userRoles = await _userManager.GetRolesAsync(newModerator);

                                bool add = userRoles.Contains("Moderator");
                                if (!add)
                                {
                                    await _userManager.AddToRoleAsync(newModerator, "Moderator");
                                }
                            }
                        }
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(category));
        }