public async Task <IActionResult> Edit(int id, [Bind("IdGroup,Name,FieldActivityId,OrganizationId,GroupParentsId,UserResponsibleId,NumberEmployees,Level")] Groupu groupu) { if (id != groupu.IdGroup) { return(NotFound()); } if (ModelState.IsValid) { Groupu groupuDB = _context.Groupu.FirstOrDefault(g => g.Name.Equals(groupu.Name) && g.IdGroup != id); if (groupuDB == null) { Groupu groupuParent = _context.Groupu.Find(groupu.GroupParentsId); if (groupuParent.Name.Equals("")) { groupu.GroupParentsId = null; } try { User currentUser = _context.User.Where(u => u.UserName.Equals(User.Identity.Name)).FirstOrDefault(); groupu.CreatorGroup = currentUser.FamilyName + " " + currentUser.Name + " " + currentUser.PatronymicName; _context.Update(groupu); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GroupuExists(groupu.IdGroup)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError("", "Группа с данным именем уже существует"); } } List <User> users = _context.User.Select(u => new User { IdUser = u.IdUser, FioPhoneNumber = u.FamilyName + " " + u.Name + " " + u.PatronymicName + " " + u.Telephone }).ToList(); ViewData["OrganizationId"] = new SelectList(_context.Organization, "IdOrganization", "Name", groupu.Organization); ViewData["FieldActivityId"] = new SelectList(_context.Fieldactivity, "IdFieldActivity", "Name", groupu.FieldActivityId); ViewData["GroupParentsId"] = new SelectList(_context.Groupu, "IdGroup", "Name", groupu.GroupParents); ViewData["UserResponsibleId"] = new SelectList(users, "IdUser", "FioPhoneNumber", groupu.UserResponsible); return(View(groupu)); }
public IQueryable <Groupu> FilterGroups(List <Groupu> groupsUser) { Groupu mainGroup = _context.Groupu.Where(g => g.Name.Equals("Main")).FirstOrDefault(); if (groupsUser.Contains(mainGroup)) { return(_context.Groupu); } else { return(_context.Groupu.Where(g => groupsUser.Contains(g))); } }
// GET: Groupus public async Task <IActionResult> Index() { List <Groupu> groupsUser = _serviceUser.GetGroupsUser(User.Identity.Name); Groupu mainGroup = _context.Groupu.Where(g => g.Name.Equals("Main")).FirstOrDefault(); if (groupsUser.Contains(mainGroup)) { return(View(await _context.Groupu.Include(g => g.FieldActivity).Include(g => g.Organization).Include(g => g.GroupParents).Include(g => g.UserResponsible).ToListAsync())); } else { return(View(await _context.Groupu.Include(g => g.FieldActivity).Include(g => g.Organization).Include(g => g.GroupParents).Include(g => g.UserResponsible).Where(g => groupsUser.Contains(g)).ToListAsync())); } }
public List <Groupu> GetAllChildGroupsBFS(Groupu rootGroup, Groupu groupStart, Groupu goal) { // rootGroup - корневая группа // groupStart - начальный потомок // goal - пункт назначения, его нет // Список всех посещенных потомков List <Groupu> childGroupus = new List <Groupu>(); // инициализируем очередь Queue <Groupu> queueGroupu = new Queue <Groupu>(); // добавляем groupStart в очередь queueGroupu.Enqueue(rootGroup); // помечаем groupStart как посещенную вершину во избежание повторного добавления в очередь //groupStart.Visited = true; while (queueGroupu.Count > 0) { // удаляем первый (верхний) элемент из очереди Groupu directChildgr = queueGroupu.Dequeue(); //Добавляем посещенную вершину в список потомков childGroupus.Add(directChildgr); // rootGroup.InverseGroupParents - прямые потомки foreach (Groupu childgr in directChildgr.InverseGroupParents) { _context.Entry(childgr).Collection(g => g.InverseGroupParents).Load(); // добавляем его в очередь queueGroupu.Enqueue(childgr); //Добавляем посещенную вершину в список потомков //childGroupus.Add(childgr); ////если вершина ещё не посещалась //if (!childgr.Visited) //{ // // добавляем его в очередь // queueGroupu.Enqueue(childgr); // // помечаем вершину как посещенную // childgr.Visited = true; // //Добавляем посещенную вершину в список потомков // childGroupus.Add(childgr); // if (childgr.IdGroup == goal.IdGroup) return childGroupus; //} } } // Возвращение списка потомков return(childGroupus); }
public IQueryable <Groupsusers> FilterGroupsUsers(List <Groupu> groupsUser) { Groupu mainGroup = _context.Groupu.Where(g => g.Name.Equals("Main")).FirstOrDefault(); if (groupsUser.Contains(mainGroup)) { var voterCollectorContext = _context.Groupsusers.Include(g => g.GroupU).Include(g => g.GroupU.FieldActivity).Include(g => g.User); return(voterCollectorContext); } else { var voterCollectorContext = _context.Groupsusers.Include(g => g.GroupU).Include(g => g.GroupU.FieldActivity).Include(g => g.User). Where(g => groupsUser.Contains(g.GroupU)); return(voterCollectorContext); } }
public async Task <IActionResult> CountFriendsByGroup([FromBody] GroupDTO groupDTO) { List <Groupu> groupsUser = _serviceUser.GetGroupsUser(User.Identity.Name); Groupu mainGroup = _context.Groupu.FirstOrDefault(g => g.Name.Equals("Main")); NumberFriendsDTO numberFriendsDTO; if (mainGroup != null && groupsUser.Contains(mainGroup)) { numberFriendsDTO = _serviceFriends.CountNumberFriendsByGroup(groupDTO); } else { numberFriendsDTO = _serviceFriends.CountNumberFriendsByGroupAndGroupsUsers(groupDTO, groupsUser); } return(Ok(numberFriendsDTO)); }
// GET: Users public async Task <IActionResult> Index() { List <Groupu> groupsUser = _serviceUser.GetGroupsUser(User.Identity.Name); Groupu mainGroup = _context.Groupu.Where(g => g.Name.Equals("Main")).FirstOrDefault(); if (groupsUser.Contains(mainGroup)) { var voterCollectorContext = _context.User.Include(u => u.Role); return(View(await voterCollectorContext.ToListAsync())); } else { List <User> voterCollectorContext = await _context.User.Include(u => u.Role).ToListAsync(); List <User> voterCollector = voterCollectorContext.Where(u => groupsUser.Intersect(_serviceUser.GetGroupsUser(u.UserName)).Count() != 0).ToList(); return(View(voterCollector)); } }
public async Task <IActionResult> Create([Bind("IdGroup,Name,FieldActivityId,OrganizationId,GroupParentsId,UserResponsibleId,NumberEmployees,Level")] Groupu groupu) { if (ModelState.IsValid) { Groupu groupuDB = _context.Groupu.FirstOrDefault(g => g.Name.Equals(groupu.Name)); if (groupuDB == null) { User currentUser = _context.User.Where(u => u.UserName.Equals(User.Identity.Name)).FirstOrDefault(); groupu.CreatorGroup = currentUser.FamilyName + " " + currentUser.Name + " " + currentUser.PatronymicName; Groupu groupuParent = _context.Groupu.Find(groupu.GroupParentsId); if (groupuParent.Name.Equals("")) { groupu.GroupParentsId = null; } _context.Add(groupu); _context.SaveChanges(); Groupsusers groupsusers = new Groupsusers(); groupsusers.GroupUId = groupu.IdGroup; groupsusers.UserId = currentUser.IdUser; _context.Add(groupsusers); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError("", "Группа с данным именем уже существует"); } } // List <User> users = _context.User.Select(u => new User { IdUser = u.IdUser, FioPhoneNumber = u.FamilyName + " " + u.Name + " " + u.PatronymicName + " " + u.Telephone }).ToList(); ViewData["OrganizationId"] = new SelectList(_context.Organization, "IdOrganization", "Name"); ViewData["FieldActivityId"] = new SelectList(_context.Fieldactivity, "IdFieldActivity", "Name"); ViewData["GroupParentsId"] = new SelectList(_context.Groupu, "IdGroup", "Name"); ViewData["UserResponsibleId"] = new SelectList(users, "IdUser", "FioPhoneNumber"); return(View(groupu)); }
public async Task <IActionResult> GetChildGroups(int?id) { if (id == null) { return(NotFound()); } Groupu rootGroup = _context.Groupu.Include(g => g.InverseGroupParents).Include(g => g.GroupParents) .Include(g => g.UserResponsible).FirstOrDefault(g => g.IdGroup == id); if (rootGroup == null) { return(NotFound()); } List <Groupu> groups = _serviceGroup.GetAllChildGroupsBFS(rootGroup, rootGroup, rootGroup); //groups.Sort((g1, g2) => ((g1.Level ?? 0) - (g2.Level ?? 0))); return(View(groups)); }
public async Task <IActionResult> Login(LoginModel model) { if (ModelState.IsValid) { User user = await db_context.User .Include(u => u.Role) .FirstOrDefaultAsync(u => u.UserName == model.UserName && u.Password == model.Password); if (user != null) { await Authenticate(user); // аутентификация // Загрузка групп авторизовавшегося пользователя List <Groupu> groupsUser = _serviceUser.GetGroupsUser(user.UserName); // Получение группы "Волонтеры" Groupu volunteerGroup = db_context.Groupu.Where(g => g.Name.Equals("Волонтеры")).FirstOrDefault(); // Если пользователь состоит только в группе "Волонтеры" if (groupsUser.Count == 1 && volunteerGroup != null && groupsUser.Contains(volunteerGroup)) { return(RedirectToAction("Index", "QRcode")); } // проверяем, принадлежит ли URL приложению else if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl)) { return(Redirect(model.ReturnUrl)); } else { return(RedirectToAction("Index", "Friends")); } } else { ModelState.AddModelError("", "Неправильный логин и (или) пароль"); } } return(View(model)); }
public async Task <IActionResult> ReportOrganization(int id) { Groupu parentGroupu = _context.Groupu.Include(g => g.Organization).Include(g => g.UserResponsible).Include(g => g.InverseGroupParents) .Where(g => g.IdGroup == id).FirstOrDefault(); List <Report> reports = new List <Report>(); if (parentGroupu != null) { List <Friend> friends = _context.Friend.ToList(); string nameParent = parentGroupu.Name; if (parentGroupu.InverseGroupParents != null && parentGroupu.InverseGroupParents.Count > 0) { foreach (Groupu grpLvl2 in parentGroupu.InverseGroupParents) { _context.Entry(grpLvl2).Collection(g => g.InverseGroupParents).Load(); _context.Entry(grpLvl2).Reference(g => g.UserResponsible).Load(); Report report = new Report { Responseble = grpLvl2.UserResponsible != null ? grpLvl2.UserResponsible.FamilyName + " " + grpLvl2.UserResponsible.Name + " " + grpLvl2.UserResponsible.PatronymicName + " (" + grpLvl2.UserResponsible.Telephone + ")" : "", IdOdject = grpLvl2.IdGroup, NameObject = grpLvl2.Organization != null ? grpLvl2.Organization.Name : grpLvl2.Name, Level = grpLvl2.Level ?? 0, NumberEmployees = grpLvl2.NumberEmployees ?? 0, NameParent = nameParent, childGroup = false }; int numberVoters = 0; int numberVoted = 0; int numberQRcodes = 0; List <Groupu> groupsChild = _serviceGroup.GetAllChildGroupsBFS(grpLvl2, grpLvl2, grpLvl2); if (groupsChild != null && groupsChild.Count > 0) { foreach (Groupu groupu in groupsChild) { if (groupu.IdGroup != grpLvl2.IdGroup) { report.childGroup = true; } numberVoters += friends.Where(f => f.GroupUId == groupu.IdGroup).Count(); numberVoted += friends.Where(f => f.GroupUId == groupu.IdGroup && f.Voter == true).Count(); numberQRcodes += friends.Where(f => f.GroupUId == groupu.IdGroup && f.TextQRcode != null && !f.TextQRcode.Equals("")).Count(); } } else { numberVoters += friends.Where(f => f.GroupUId == grpLvl2.IdGroup).Count(); numberVoted += friends.Where(f => f.GroupUId == grpLvl2.IdGroup && f.Voter == true).Count(); numberQRcodes += friends.Where(f => f.GroupUId == grpLvl2.IdGroup && f.TextQRcode != null && !f.TextQRcode.Equals("")).Count(); } report.NumberVoters = numberVoters; report.NumberVoted = numberVoted; report.NumberQRcodesText = numberQRcodes; if (numberVoters != 0) { report.PersentVotedByVoters = Math.Round((double)numberVoted / numberVoters * 100, 2); } if (report.NumberEmployees != 0) { report.PersentVotedByEmploees = Math.Round((double)numberVoted / report.NumberEmployees * 100, 2); } if (report.NumberEmployees != 0) { report.PersentVotersByEmploees = Math.Round((double)numberVoters / report.NumberEmployees * 100, 2); } reports.Add(report); } } else { reports.Add(new Report { NameParent = nameParent }); } } reports.Sort((r1, r2) => r1.NameObject.CompareTo(r2.NameObject)); return(View(reports)); }