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)); }