public async Task<IHttpActionResult> DeleteSummoner(SummonerModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var user = await _users.GetUserAsync();
            var summoner = user.Summoners.FirstOrDefault(DbUtil.CreateComparer(model.Region, model.SummonerName));

            if (summoner == null)
            {
                return Conflict("Summoner not found.");
            }

            if (await _summoners.RemoveAsync(model.Region, model.SummonerName))
            {
                return Ok();
            }

            if (!await _flair.SetUpdateFlagAsync(new[] {user}))
            {
                return Conflict("Unable to remove summoner.");
            }
            return Conflict("Unable to remove summoner.");
        }
        public async Task<ActionResult> Register(SummonerModel model)
        {
            ViewModel = await CreateViewModelAsync();
            if (!ModelState.IsValid)
            {
                return Error(ModelState);
            }

            // Rule: Summoner must not be registered to a User.
            if (await Summoners.IsSummonerRegistered(model.Region, model.SummonerName))
            {
                return Error("Summoner is already registered.");
            }

            // Rule: Summoner must exist.
            var cacheKey = string.Concat(model.Region, ":", model.SummonerName).ToLowerInvariant();
            var summoner = await CacheUtil.GetItemAsync(cacheKey,
                () => Riot.FindSummonerAsync(model.Region, model.SummonerName));

            if (summoner == null)
            {
                return Error("Summoner not found.");
            }

            return Success();
        }
        public async Task<IHttpActionResult> Register(SummonerModel model)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            try
            {
                // Summoner MUST exist.
                var summoner = await FindSummonerAsync(model.Region, model.SummonerName);

                if (summoner == null)
                    return Conflict("Summoner not found.");

                // Summoner MUST NOT be registered.
                if (await Summoners.IsSummonerRegistered(model.Region, model.SummonerName))
                    return Conflict("Summoner is already registered.");
                
                return Ok(new
                {
                    code = RegistrationCode.Generate((await Users.GetUserAsync()).Name, summoner.Id, model.Region)
                });
            }
            catch (RiotHttpException e)
            {
                switch ((int) e.StatusCode)
                {
                    case 500:
                    case 503:
                        return Conflict("Error communicating with Riot (Service unavailable)");
                }
                throw;
            }
        }
        public async Task<IHttpActionResult> Validate(SummonerModel model)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            try
            {
                // Summoner MUST exist.
                var riotSummoner = await Riot.FindSummonerAsync(model.Region, model.SummonerName);
                var user = await Users.GetUserAsync();

                if (riotSummoner == null)
                {
                    return Conflict("Summoner not found.");
                }

                // Summoner MUST NOT be registered.
                if (await Summoners.IsSummonerRegistered(model.Region, model.SummonerName))
                {
                    return Conflict("Summoner is already registered.");
                }

                var runePages = await Riot.GetRunePagesAsync(model.Region, riotSummoner.Id);
                var code = RegistrationCode.Generate(user.Name, riotSummoner.Id, model.Region);

                if (!runePages.Any(page => string.Equals(page.Name, code, StringComparison.InvariantCultureIgnoreCase)))
                {
                    return StatusCode(HttpStatusCode.ExpectationFailed);
                }

                // Create the data entity and associate it with the current user
                var currentSummoner =
                    await Summoners.AddSummonerAsync(user, riotSummoner.Id, model.Region, riotSummoner.Name);

                // If the user doesn't have an active summoner, assign the new summoner as active.
                if (await Summoners.GetActiveSummonerAsync(user) == null)
                    await Summoners.SetActiveSummonerAsync(currentSummoner);

                // Queue up the league update.
                var jobId = BackgroundJob.Enqueue<LeagueUpdateJob>(job => job.Execute(currentSummoner.Id));
                // Queue up flair update.
                BackgroundJob.ContinueWith<FlairUpdateJob>(jobId, job => job.Execute(user.Id));
                return Ok();
            }
            catch (RiotHttpException e)
            {
                switch ((int) e.StatusCode)
                {
                    case 500:
                    case 503:
                        return Conflict("Error communicating with Riot (Service unavailable)");
                }
                throw;
            }
        }