Esempio n. 1
0
        public List <ExerciseItem> LoadAllExerciseItems()
        {
            //Do version control here
            DataTable dt = null;

            bool loop = true;

            while (loop)
            {
                try
                {
                    dt   = DatabaseHelper.LoadItems(connection, allExercisesTableName, allExercisesTableData);
                    loop = false;
                }
                catch (SQLiteException ex)
                {
                    if (ex.Message.Contains("no such column: MuscleGroupId"))
                    {
                        DatabaseHelper.AddColumn(connection, allExercisesTableName, "MuscleGroupId int");
                        continue; //try again with new column
                    }
                    else if (ex.Message.Contains("no such column: Weight"))
                    {
                        DatabaseHelper.AddColumn(connection, allExercisesTableName, "Weight float");
                        continue; //try again with new column
                    }
                    else
                    {
                        throw new Exception("Error Loading database, this isnt a migration issue that we have handled");
                    }
                }
            }


            if (dt == null)
            {
                throw new Exception("Something has gone wrong in loading the database");
            }

            foreach (var row in dt.AsEnumerable()) //This loop could get nasty on long iterations. Keep an eye on it
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (row.IsNull(i))
                    {
                        //throw new Exception("One of the datasets are null. Need to manually edit to fix");
                        System.Diagnostics.Debug.WriteLine(row[i] + " was null. Converting to 0");
                        row[i] = 0; //0 is a very cool value because it can be many types. still keep an eye on it
                    }
                }
            }

            List <ExerciseItem> loadedItems = (from rw in dt.AsEnumerable()
                                               select new ExerciseItem
            {
                GUIDID = Convert.ToString(rw[nameof(nameExerciseItem.GUIDID)]),
                ExerciseName = Convert.ToString(rw[nameof(nameExerciseItem.ExerciseName)]),
                ExerciseTypeId = Convert.ToInt32(rw[nameof(nameExerciseItem.ExerciseTypeId)]),
                RequiredReps = Convert.ToInt32(rw[nameof(nameExerciseItem.RequiredReps)]),
                RequiredTime = Convert.ToInt32(rw[nameof(nameExerciseItem.RequiredTime)]),
                RequiredSets = Convert.ToInt32(rw[nameof(nameExerciseItem.RequiredSets)]),
                RequiredSetsCount = Convert.ToInt32(rw[nameof(nameExerciseItem.RequiredSetsCount)]),
                DueTime = Convert.ToDateTime(rw[nameof(nameExerciseItem.DueTime)]),
                IsUsedInRoster = Convert.ToBoolean(rw[nameof(nameExerciseItem.IsUsedInRoster)]),
                MuscleGroupId = Convert.ToInt32(rw[nameof(nameExerciseItem.MuscleGroupId)]),
                Weight = Convert.ToSingle(rw[nameof(nameExerciseItem.Weight)]),
            }).ToList();

            return(loadedItems);
        }