public async Task <IActionResult> Add(JuryAddTeamModel model) { var users = new HashSet <IUser>(); if (model.UserName != null) { var userNames = model.UserName.Split(',', StringSplitOptions.RemoveEmptyEntries); foreach (var userName in userNames) { var user = await UserManager.FindByNameAsync(userName.Trim()); if (user == null) { ModelState.AddModelError("xys::no_user", $"No such user {userName.Trim()}."); } else if ((await Context.FindMemberByUserAsync(user.Id)) != null) { ModelState.AddModelError("xys::duplicate_user", "Duplicate user."); } else { users.Add(user); } } } var affiliations = await Context.ListAffiliationsAsync(false); if (!affiliations.TryGetValue(model.AffiliationId, out _)) { ModelState.AddModelError("xys::no_aff", "No such affiliation."); } if (!ModelState.IsValid) { return(Window(model)); } var team = await Context.CreateTeamAsync( users : users.Count > 0?users : null, team : new Team { AffiliationId = model.AffiliationId, Status = 1, CategoryId = model.CategoryId, ContestId = Contest.Id, TeamName = model.TeamName, }); await HttpContext.AuditAsync("added", $"{team.TeamId}"); return(Message( title: "Add team", message: $"Team {model.TeamName} (t{team.TeamId}) added.", type: BootstrapColor.success)); }
public IActionResult Add(JuryAddTeamModel model) { var teamid = Service.CreateTeam(new Team { AffiliationId = model.AffiliationId, Status = 1, CategoryId = model.CategoryId, ContestId = Contest.ContestId, TeamName = model.TeamName, UserId = model.UserId, }); return(Message( "Add team", $"Team {model.TeamName} (t{teamid}) added.", MessageType.Success)); }
public async Task <IActionResult> Add( int cid, JuryAddTeamModel model, [FromServices] UserManager userManager) { HashSet <int> users = new HashSet <int>(); if (model.UserName != null) { var userNames = model.UserName.Split(',', StringSplitOptions.RemoveEmptyEntries); foreach (var userName in userNames) { var user = await userManager.FindByNameAsync(userName.Trim()); if (user == null) { ModelState.AddModelError("xys::no_user", $"No such user {userName.Trim()}."); } else if ((await Store.FindByUserAsync(cid, user.Id)) != null) { ModelState.AddModelError("xys::duplicate_user", "Duplicate user."); } else if (users.Contains(user.Id)) { continue; } else { users.Add(user.Id); } } } var affs = await Store.ListAffiliationAsync(cid, false); var aff = affs.FirstOrDefault(a => a.AffiliationId == model.AffiliationId); if (aff == null) { ModelState.AddModelError("xys::no_aff", "No such affiliation."); } if (!ModelState.IsValid) { ViewBag.Aff = affs; ViewBag.Cat = await Store.ListCategoryAsync(cid); return(Window(model)); } var teamid = await Store.CreateAsync( uids : users.Count > 0?users.ToArray() : null, team : new Team { AffiliationId = model.AffiliationId, Status = 1, CategoryId = model.CategoryId, ContestId = Contest.ContestId, TeamName = model.TeamName, }); await HttpContext.AuditAsync("added", $"{teamid}"); return(Message( title: "Add team", message: $"Team {model.TeamName} (t{teamid}) added.", type: MessageType.Success)); }