public async Task <IActionResult> Edit(long id, [Bind("ID,Headline,Caption,PostedText,UserName")] Post post) { if (id != post.ID) { return(NotFound()); } Category category = _postService.getCategoryForPost(id); var user = await GetCurrentUserAsync(); string userId = user?.Id; post.UserId = user; post.Category = category; post.CategoryId = category.ID; post.Approved = false; post.Edited = true; post.PostedAt = _postService.getPostedAtForPost(id); post.approvedByUser = null; post.approvedByUserId = null; try { await _postService.Update(post); } catch (DbUpdateConcurrencyException) { if (!_postService.PostExists(post.ID)) { return(NotFound()); } else { throw; } } //menageModeratorRoleForUser(category, user); var userRoles = await _userManager.GetRolesAsync(user); if (!userRoles.Contains("Admin")) { int usersPostsForCategory = _userService.CountApprovedPostsFromUserForCategory(userId, category.ID); int postBeforeDelete = usersPostsForCategory + 1; if (usersPostsForCategory < category.PostRule && postBeforeDelete == category.PostRule) { int moderatorCategories = _userService.countModeratorCategories(userId); 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"); } } } return(RedirectToAction("Index")); }
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)); }