Example #1
0
        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);
        }
Example #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();
        }