コード例 #1
0
        public IActionResult Purge()
        {
            int   athleteId = HttpContext.Session.GetInt32("AthleteId") ?? 0;
            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);
            }
            Processor.Deauthorization(token.AccessToken);
            Athlete athleteToDelete = _context.Athletes
                                      .FirstOrDefault(athlete => athlete.AthleteId == athleteId);

            _context.Remove(athleteToDelete);
            _context.Remove(token);
            _context.SaveChanges();
            HttpContext.Session.Clear();
            return(RedirectToAction("Index", "Home"));
        }
コード例 #2
0
        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();
        }