public async Task <ActionResult> Create(TeamsCreateViewModel model) { if (ModelState.IsValid) { if (await _db.Teams.AnyAsync(c => c.Name == model.Name) || model.Name == "") { ModelState.AddModelError("", "Команда с таким названием уже существует или не задано название команды"); } else { var team = new Team { Id = Guid.NewGuid(), Name = model.Name, MaxTeamNumber = model.MaxTeamNumber, Rating = 0, Contests = new HashSet <Contest>(), Participants = new List <ApplicationUser>() }; _db.Teams.Add(team); await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } } return(View(model)); }
public ActionResult Create() { if (!User.IsInRole("administrator")) { TempData["Message"] = "Только администратор имеет право создавать команды"; return(RedirectToAction("Index")); //ViewBag.ErrorMessage = "Только администратор имеет право создавать команды"; //return View("Error"); } var model = new TeamsCreateViewModel { Name = "", MaxTeamNumber = 1 }; return(View(model)); }
public async Task <IActionResult> Create(TeamsCreateViewModel model) { if (ModelState.IsValid) { User currentUser = await GetCurrentUser(); Team newTeam = new Team { //TeamName = model.TeamName, WorkingOnProject = _context.Projects.First(t => t.ProjectName == model.WorkingOnProject), //Developers = model.Developers.ToList(); }; if (_context.Teams.Any(x => x.TeamName == model.TeamName)) { ModelState.AddModelError($"TeamName", "Team with such name exists already."); } else { newTeam.TeamName = model.TeamName; } try { string[] names = model.Leader.Split(); newTeam.Leader = _context.Users .Include(u => u.Role) .First(u => u.FirstName == names.First() && u.LastName == names.Last()); if (newTeam.Leader.Role.Name == "Team Lead") { ModelState.AddModelError("Leader", "This user is already a leader of an existing team."); } else if (newTeam.Leader.Role.Name == "CEO") { ModelState.AddModelError("Leader", "This is the CEO."); } else { newTeam.Leader.Role = _context.Roles.First(r => r.Name == "Team Lead"); newTeam.Leader.Team = newTeam; } } catch { ModelState.AddModelError("Leader", "No such user."); } var developers = model.Developers.Where(c => c != null).ToArray(); model.BoxesToShow = developers.Count() < 2 ? 2 : developers.Count(); bool mark = false; if (developers.Count() < 2) { ModelState.AddModelError("Developers", "Team must have at least two members exept the Leader."); mark = true; } bool duplicates = false; if (developers.Count() != developers.Distinct().Count()) { duplicates = true; } for (int i = 0; i < developers.Count(); i++) { if (duplicates) { for (int j = i + 1; j < developers.Count(); j++) { if (developers[j] == developers[i]) { ModelState.AddModelError($"Developers[{j}]", "Already taken in above choices."); break; } } } string[] names = developers[i].Split(); if (_context.Users.Any(u => u.FirstName == names.First() && u.LastName == names.Last())) { User member = _context.Users .Include(u => u.Role) .First(u => u.FirstName == names[0] && u.LastName == names[1]); if (member == newTeam.Leader) { ModelState.AddModelError($"Developers[{i}]", "This user is Team Leader of this team."); } else if (member.Role.Name == "Team Lead") { ModelState.AddModelError($"Developers[{i}]", "This user is a leader of an existing team."); } else { member.Team = newTeam; member.Role = _context.Roles.First(r => r.Name == "Developer"); } } else { mark = true; ModelState.AddModelError($"Developers[{i}]", "No such user."); } } if (mark || duplicates) { return(View(model)); } _context.Add(newTeam); _context.SaveChanges(); return(RedirectToAction(nameof(Index))); } return(View(model)); }