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; } }