예제 #1
0
        public IActionResult MakeSquads(int numSquads)
        {
            var errors = new List <string>();

            if (numSquads < 1 || numSquads > _context.Players.Count())
            {
                errors.Add("Number of squads must be at least one, and less than the total number of players.");
            }
            else if (_context.Squads.Any())
            {
                errors.Add("Squads already exist in the system. Please clear them to generate a new set.");
            }
            else
            {
                try
                {
                    var squads   = Enumerable.Range(1, numSquads).Select(i => new Squad(i)).ToList();
                    var waitlist = _squadService.QueryWaitingPlayers(_context);
                    _squadService.AssignWaitlistPlayersToSquads(squads, waitlist);

                    _context.Squads.AddRange(squads);
                    _context.SaveChanges();
                }
                catch (Exception e)
                {
                    errors.Add(e.Message);
                }
            }
            return(RedirectToAction("Index", new { errors }));
        }