Ejemplo n.º 1
0
        public async Task <IActionResult> ByGroup(
            int cid, AddTeamByGroupModel model,
            [FromServices] IStudentStore store)
        {
            var cls = await store.FindClassAsync(model.GroupId);

            if (cls == null)
            {
                return(NotFound());
            }

            var affs = await Store.ListAffiliationAsync(cid, false);

            var cats = await Store.ListCategoryAsync(cid);

            var aff = affs.SingleOrDefault(a => a.AffiliationId == model.AffiliationId);
            var cat = cats.SingleOrDefault(c => c.CategoryId == model.CategoryId);

            if (aff == null || cat == null)
            {
                return(NotFound());
            }

            var stus = await store.ListStudentsAsync(cls);

            var stu  = stus.ToLookup(s => new { s.Id, s.Name });
            var uids = await Store.ListMemberUidsAsync(cid);

            foreach (var item in stu)
            {
                var lst = item.Where(s => s.IsVerified ?? false);
                if (model.AddNonTemporaryUser)
                {
                    lst = lst.Where(s => s.Email == null);
                }
                var users = lst.Select(s => s.UserId.Value)
                            .Where(i => !uids.Contains(i))
                            .ToHashSet();

                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      = $"{item.Key.Id}{item.Key.Name}",
                });
            }

            StatusMessage = "Import success.";
            return(RedirectToAction(nameof(List)));
        }