コード例 #1
0
        static bool ImportWorkoutDetails(int workoutId, IRepository <Workout> repository, string workoutDetailsFolder)
        {
            var myWorkout = repository.GetById(workoutId).Result;

            if (myWorkout == null)
            {
                return(false);
            }

            var detailsFile = Path.Combine(workoutDetailsFolder, $@"{workoutId}.xml");

            WorkoutDetailParser.LoadWorkoutData(detailsFile, myWorkout);
            repository.Update(myWorkout).Wait();
            return(true);
        }
コード例 #2
0
        private async Task <ApiResultBase> LoadWorkoutDetails(Workout workout)
        {
            var result = new ApiResultBase();

            var workoutDetailsFolder = _configuration["WorkoutDetailsFolder"];

            workoutDetailsFolder = Environment.ExpandEnvironmentVariables(workoutDetailsFolder);
            var workoutDetailsFile = Path.Combine(workoutDetailsFolder, $"{workout.Id}.xml");

            if (System.IO.File.Exists(workoutDetailsFile))
            {
                result.Success = WorkoutDetailParser.LoadWorkoutData(workoutDetailsFile, workout);
                result.Message = result.Success ? $"Loaded details for workout {workout.Id} from file" : $"Details were not loaded for workout {workout.Id}";
                return(result);
            }

            using var detailsStream = await WorkoutDetailsRetriever.DownloadWorkoutDetails(_configuration["OnlineWorkoutDetailsSource"], workout.Id)
                                      .ConfigureAwait(false);

            if (detailsStream == null)
            {
                result.Message = $"No details found for workout with Id={workout.Id}!";
                result.Success = false;
                return(result);
            }

            detailsStream.Position = 0;
            WorkoutDetailParser.LoadWorkoutData(detailsStream, workout);

            using var outputFile   = new FileStream(workoutDetailsFile, FileMode.Create);
            detailsStream.Position = 0;
            await detailsStream.CopyToAsync(outputFile)
            .ConfigureAwait(false);

            result.Success = true;
            result.Message = $"Loaded details for workout {workout.Id} from online source";
            return(result);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            /*           var x = TestEnum1.Value1;
             *         var s = x.GetDisplayName();
             *
             *         var enumType = Enum.GetUnderlyingType(typeof(TestFlags1));
             *         var enumType2 = Enum.GetUnderlyingType(typeof(TestEnum1));
             *         Console.WriteLine($"value1={(int)TestEnum1.Value1},value3={(int)TestEnum1.Value3}");
             *         //var enumValues1 = Enum.GetValues(typeof(TestFlags1));
             *         //var enumValList = new List<int>((int[])enumValues1);
             *         //int maxVal = TestEnum1.GetMaxValue(typeof(TestEnum1)); // flags.GetMaxValue();// enumValList.Sum();
             *         TestFlags1 fl1 = EnumExtensions.IntToFlags<TestFlags1>(7);
             *         var maxVal = typeof(TestFlags1).GetMaxValue();
             */
            var workoutId = 0;

            if (args.Length == 1)
            {
                if (!int.TryParse(args[0], out workoutId) || workoutId < 1)
                {
                    Console.WriteLine("A valid WorkoutId must be specified!".ToInvariant());
                    return;
                }
            }

            IConfiguration config = new ConfigurationBuilder()
                                    .AddJsonFile("appsettings.json", true, true)
                                    .Build();

            var workoutDetailsFolder = config["WorkoutDetailsFolder"];

            workoutDetailsFolder = Environment.ExpandEnvironmentVariables(workoutDetailsFolder);
            var allSwimsFile   = Path.Combine(workoutDetailsFolder, "AllSwims.json");//"C:\\Temp\\Workouts.json"
            var loadedWorkouts = WorkoutListParser.LoadWorkoutList(allSwimsFile);

            Console.WriteLine($"Workouts loaded: {loadedWorkouts?.Count ?? 0}");

            string connectionString = config.GetConnectionString("DefaultConnection");

            using var dbContext = new SqlServerDbContext(connectionString);
            var repository = RepositoryFactory.CreateWorkoutRepository(dbContext);

            var storedWorkouts = repository.GetList(new WorkoutsSortedByDateSpecification()).Result;

            if (workoutId == 0)
            {
                var workouts = storedWorkouts.OrderBy(w => w.Id);
                foreach (var workout in workouts)
                {
                    if (ImportWorkoutDetails(workout.Id, repository, workoutDetailsFolder))
                    {
                        Console.WriteLine($"Workout {workout.Id} details loaded");
                    }
                    else
                    {
                        Console.WriteLine($"Workout {workout.Id} details load failed!");
                    }

                    Thread.Sleep(300);
                }

                return;
            }

            var newWorkouts = GetNewWorkouts(loadedWorkouts, storedWorkouts);

            SaveWorkouts(newWorkouts, repository);

            var myWorkout = storedWorkouts.Where(w => w.Id == workoutId).FirstOrDefault();

            if (myWorkout == null)
            {
                myWorkout = loadedWorkouts.Where(w => w.Id == workoutId).FirstOrDefault();
                if (myWorkout == null)
                {
                    Console.WriteLine($"Workout with id {workoutId} is not found!");
                    return;
                }
            }

            //var xmlDataStream = WorkoutDetailsRetriever.DownloadWorkoutDetails("https://www.swim.com/export/xml/", 1248785).Result;
            //WorkoutDetailParser.LoadWorkoutData(xmlDataStream, myWorkout);

            var detailsFile = Path.Combine(workoutDetailsFolder, $@"{workoutId}.xml");

            WorkoutDetailParser.LoadWorkoutData(detailsFile, myWorkout);

            //var workoutInDb = repository.Get(workoutId).Result;
            var workoutInDb = repository.GetById(workoutId).Result;

            if (workoutInDb == null)
            {
                var id = repository.Add(myWorkout).Result;
            }
            else
            {
                //var updated = repository.UpdateModifiedFields(myWorkout).Result;
                repository.Update(myWorkout).Wait();
            }
        }