private int Watch(UserModel summoner, string server) { var summonerDb = Shared.GetDbSummoner(summoner.Id, dal).Result; if (summonerDb != null) { if (summonerDb.Watch) { logger.LogInformation("Summoner is already set to watching. Skipping action."); return(0); } summonerDb.Watch = true; dal.Update(summonerDb); } else { summonerDb = new DatabaseAccessLayer.Models.UserModel { Server = server.ToLower(), SummonerId = summoner.Id, Username = summoner.Name, Watch = true, }; dal.Add(summonerDb); } Shared.CommitDatabaseChanges(dal, logger).Wait(); return(0); }
public async Task <IActionResult> Watch( [FromRoute] string server, [FromRoute] string userName) { server = server.ToLower(); var key = $"{server}/{userName}"; if (!registrationCache.RegistrationCodeCache.ContainsKey(key)) { return(Unauthorized()); } var stateCode = registrationCache.RegistrationCodeCache.Get(key); UserModel user = new UserModel(); string obtainedCode; try { user = await wrapper.GetSummonerByName(server, userName); } catch (ResponseException e) { if (e.Response.StatusCode == HttpStatusCode.NotFound) { return(BadRequest("user not found")); } } try { obtainedCode = await wrapper.GetThirdPartyCode(server, user.Id); if (obtainedCode != stateCode) { return(Unauthorized()); } } catch (ResponseException e) { if (e.Response.StatusCode == HttpStatusCode.NotFound) { return(Unauthorized()); } } var dbUser = await dal.GetUserByNameAsync(server, user.Name); if (dbUser == null) { dbUser = new DatabaseAccessLayer.Models.UserModel { Server = server, Username = user.Name, SummonerId = user.Id, Watch = true, }; dal.Add(dbUser); } else { dbUser.SummonerId = user.Id; dbUser.Watch = true; dal.Update(dbUser); } await dal.CommitChangesAsync(); return(Ok()); }