public int CreateAthlete(Athlete newAthlete, Token newToken) { _context.Add(newToken); _context.SaveChanges(); newAthlete.TokenId = newToken.TokenId; _context.Add(newAthlete); _context.SaveChanges(); var newAthleteStats = new AthleteStats(); newAthleteStats.AthleteId = newAthlete.AthleteId; _context.Add(newAthleteStats); _context.SaveChanges(); int newAthleteStatsId = newAthleteStats.AthleteStatsId; var newBikeTotal = new BikeTotal(); newBikeTotal.AthleteStatsId = newAthleteStatsId; _context.Add(newBikeTotal); var newRunTotal = new RunTotal(); newRunTotal.AthleteStatsId = newAthleteStatsId; _context.Add(newRunTotal); var newSwimTotal = new SwimTotal(); newSwimTotal.AthleteStatsId = newAthleteStatsId; _context.Add(newSwimTotal); _context.SaveChanges(); return(newAthlete.AthleteId); }
public void UpdateAthlete(int athleteId) { // Retrieve token info for this athlete Token token = _context.Athletes .Include(athlete => athlete.Token) .FirstOrDefault(athlete => athlete.AthleteId == athleteId) .Token; // If token expires with one hour, refresh token if (token.ExpiresAt < DateTime.Now.AddSeconds(3600)) { AuthorizationModel tokenModel = StravaController .loadNewToken(token.RefreshToken).Result; token.AccessToken = tokenModel.Access_Token; token.RefreshToken = tokenModel.Refresh_Token; token.ExpiresAt = DateTime.Now.AddSeconds(tokenModel.Expires_In); token.ExpiresIn = TimeSpan.FromSeconds(tokenModel.Expires_In); } // Retrieve current athlete from the database Athlete dbAthlete = _context.Athletes .FirstOrDefault(athlete => athlete.AthleteId == athleteId); // API call to retrieve athlete stats AthleteStats currentStats = StravaController .loadAthleteStats(dbAthlete.Id, token.AccessToken).Result; // Update user's totals in the database BikeTotal bikeTotalToUpdate = _context.BikeTotals .Include(bikeTotal => bikeTotal.UserStats) .First(bikeTotal => bikeTotal.UserStats.AthleteId == dbAthlete.AthleteId); bikeTotalToUpdate.Distance = currentStats.YTD_Ride_Totals.Distance; bikeTotalToUpdate.Elevation_Gain = currentStats.YTD_Ride_Totals.Elevation_Gain; bikeTotalToUpdate.UpdatedAt = DateTime.Now; RunTotal runTotalToUpdate = _context.RunTotals .Include(runTotal => runTotal.UserStats) .First(runTotal => runTotal.UserStats.AthleteId == dbAthlete.AthleteId); runTotalToUpdate.Distance = currentStats.YTD_Run_Totals.Distance; runTotalToUpdate.Elevation_Gain = currentStats.YTD_Run_Totals.Elevation_Gain; runTotalToUpdate.UpdatedAt = DateTime.Now; SwimTotal swimTotalToUpdate = _context.SwimTotals .Include(swimTotal => swimTotal.UserStats) .First(swimTotal => swimTotal.UserStats.AthleteId == dbAthlete.AthleteId); swimTotalToUpdate.Distance = currentStats.YTD_Swim_Totals.Distance; swimTotalToUpdate.UpdatedAt = DateTime.Now; _context.SaveChanges(); }
public static async Task <AthleteStats> LoadAthleteStatsInfo(int athleteId, string accessToken) { string url = $"https://www.strava.com/api/v3/athletes/{athleteId}/stats?access_token={accessToken}"; Console.WriteLine("Load Athlete Stats running"); using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url)) { if (response.IsSuccessStatusCode) { AthleteStats result = await response.Content.ReadAsAsync <AthleteStats>(); return(result); } else { throw new Exception(response.ReasonPhrase); } } }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { string email = model.Email; var userName = email.Substring(0, email.IndexOf('@')); var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; // initialize the new Application User entity var result = await _userManager.CreateAsync(user, model.Password); // confirm new Application User was created successfully if (result.Succeeded) // create the necessary athlete/ coach profile, stats, and assign the User to its Role { var roleModel = _roleManager.Roles.SingleOrDefault(r => r.Id == model.RoleId); switch (roleModel.Name) { case "Athlete": var athleteProfile = new AthleteProfile { UserId = user.Id }; // initialize a new Athlete Profile entity var athleteStats = new AthleteStats { UserId = user.Id, DateOFEntry = DateTime.Now }; // initialize a new Athlete Stats entity and add to Athlete role _context.AthleteProfiles.Add(athleteProfile); _context.AthleteStats.Add(athleteStats); await _userManager.AddToRoleAsync(user, roleModel.Name); break; case "Coach": var coachProfile = new CoachProfile { UserId = user.Id }; // initialize a new Coach Profile entity and add to Coach role _context.CoachProfiles.Add(coachProfile); await _userManager.AddToRoleAsync(user, roleModel.Name); break; default: break; } await _userManager.SetUserNameAsync(user, userName); // set the Username within the User.Identity await _userManager.UpdateAsync(user); _context.SaveChanges(); _logger.LogInformation("User created a new account with password."); _logger.LogInformation("Profile created"); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }