Пример #1
0
        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());
        }
Пример #2
0
        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;
            }
        }
Пример #3
0
 private Task <Summoner> FindSummonerAsync(string region, string summonerName)
 {
     return(CacheUtil.GetItemAsync($"{region}:{summonerName}".ToLowerInvariant(),
                                   () => Riot.FindSummonerAsync(region, summonerName)));
 }