예제 #1
0
        private async Task ProcessStravaActivityEvent(StravaEvent stravaEvent, Athlete athlete, ILogger log)
        {
            switch (stravaEvent.ActionType)
            {
            case ActionType.Create:
                log.LogInformation($"Adding activity with id={stravaEvent.ObjectId} for athlete id={stravaEvent.AthleteId}.");
                await this.AddActivity(stravaEvent, athlete, log);

                break;

            case ActionType.Update:
                log.LogInformation($"Updating activity with id={stravaEvent.ObjectId} for athlete id={stravaEvent.AthleteId}.");
                await this.UpdateActivity(stravaEvent, log);

                break;

            case ActionType.Delete:
                log.LogInformation($"Removing activity with id={stravaEvent.ObjectId} for athlete id={stravaEvent.AthleteId}.");
                await this.DeleteActivity(stravaEvent, log);

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(stravaEvent.ActionType), "Unsupported type of Strava activity aspect type");
            }
        }
예제 #2
0
 private void HandlePreviouslyImported(SkateLogEntry existingLogEntry, StravaEvent existingStravaEvent)
 {
     // If a skate log entry exists for this activity and the event is not marked as imported, then we need to correct the event.
     if (existingLogEntry != null && existingStravaEvent != null && !existingStravaEvent.Imported)
     {
         existingStravaEvent.Imported = true;
         context.StravaEvents.Update(existingStravaEvent);
     }
 }
예제 #3
0
 private void HandleEventNotExisting(SaveStravaEventCommand request, ApplicationUser user, SkateLogEntry existingLogEntry, StravaEvent existingStravaEvent)
 {
     if (existingStravaEvent == null)
     {
         var isImported = existingLogEntry != null;
         var newEntity  = new StravaEvent {
             ApplicationUserId = user.Id, Imported = isImported, StravaActivityId = request.ActivityId
         };
         context.StravaEvents.Add(newEntity);
     }
 }
예제 #4
0
        private async Task UpdateActivity(StravaEvent stravaEvent, ILogger log)
        {
            if (stravaEvent.Updates == null || (string.IsNullOrEmpty(stravaEvent.Updates.Title) && string.IsNullOrEmpty(stravaEvent.Updates.Type) && !stravaEvent.Updates.Private.HasValue))
            {
                log.LogWarning($"Activity with id={stravaEvent.ObjectId} was not updated.");
                return;
            }

            await this.activityService.UpdateActivity(stravaEvent.AthleteId, stravaEvent.ObjectId, stravaEvent.Updates.Title, stravaEvent.Updates.Type, stravaEvent.Updates.Private);

            log.LogInformation($"Activity with id={stravaEvent.ObjectId} was successfully updated.");
        }
예제 #5
0
        private async Task AddActivity(StravaEvent stravaEvent, Athlete athlete, ILogger log)
        {
            var stravaToken = this.tokenService.GetStravaToken(athlete);

            var stravaActivity = await this.stravaWrapper.GetActivity(stravaToken, stravaEvent.ObjectId);

            if (stravaActivity == null)
            {
                log.LogWarning($"Activity with id={stravaEvent.ObjectId} does not exists or is private.");
                return;
            }

            var activity = this.mapper.Map <Activity>(stravaActivity);

            await this.activityService.AddActivity(activity);

            log.LogInformation($"Activity with id={stravaEvent.ObjectId} was successfully processed.");
        }
예제 #6
0
        public async Task <IgnoreActivitiesCommandResponse> Handle(IgnoreActivitiesCommand request, CancellationToken cancellationToken)
        {
            if (request.Skater == null || request.StravaActivityIds == null || !request.StravaActivityIds.Any())
            {
                return(new IgnoreActivitiesCommandResponse {
                    ActivitiesIgnored = 0
                });
            }

            var stravaEvents = await context.StravaEvents.Where(x => x.ApplicationUserId.Equals(request.Skater.Id)).ToListAsync();

            foreach (var stravaActivityId in request.StravaActivityIds)
            {
                var existingStravaEvent = stravaEvents.FirstOrDefault(x => x.StravaActivityId.Equals(stravaActivityId, StringComparison.CurrentCultureIgnoreCase));
                if (existingStravaEvent == null)
                {
                    var newStravaEvent = new StravaEvent
                    {
                        ApplicationUserId = request.Skater.Id,
                        StravaActivityId  = stravaActivityId,
                        Imported          = true
                    };

                    context.StravaEvents.Add(newStravaEvent);
                }
                else if (existingStravaEvent != null && !existingStravaEvent.Imported)
                {
                    existingStravaEvent.Imported = true;
                    context.StravaEvents.Update(existingStravaEvent);
                }
            }

            return(new IgnoreActivitiesCommandResponse {
                ActivitiesIgnored = await context.SaveChangesAsync()
            });
        }
예제 #7
0
        private async Task DeleteActivity(StravaEvent stravaEvent, ILogger log)
        {
            await this.activityService.DeleteActivity(stravaEvent.AthleteId, stravaEvent.ObjectId);

            log.LogInformation($"Activity with id={stravaEvent.ObjectId} was successfully deleted.");
        }
예제 #8
0
 private void HandleActivityTypeChange(SaveStravaEventCommand request, SkateLogEntry existingLogEntry, StravaEvent existingStravaEvent)
 {
     // If the activity has had a type change in Strava and previously the import did not result in a log entry
     // Then we want to mark it so that it can be imported again.
     if (existingLogEntry == null &&
         existingStravaEvent != null &&
         existingStravaEvent.Imported &&
         request.Updates != null &&
         request.Updates.ContainsKey("type"))
     {
         existingStravaEvent.Imported = false;
         context.StravaEvents.Update(existingStravaEvent);
     }
 }