/// <summary> /// /// </summary> /// <param name="exerciseId"></param> /// <returns></returns> public async Task<ExerciseModel> GetExerciseByIdAsync(int exerciseId) { if (Online) { try { ExerciseModel exercise = await mgnService.GetExerciseByIdAsync(exerciseId); Exercise insert = new Exercise(); insert.Id = exercise.Id; insert.Name = exercise.Name; insert.Url = exercise.Url; insert.Description = exercise.Description; db.insertUpdateExercise(insert); return exercise; } catch (ServerException ex) { System.Console.WriteLine("Fehler beim Abrufen einer Übung: " + ex.StackTrace); throw; } } else { try { Exercise exercise = db.GetExerciseById(exerciseId); ExerciseModel output = new ExerciseModel(); output.Id = exercise.Id; output.Name = exercise.Name; output.Url = exercise.Url; output.Description = exercise.Description; List<EntryModel<int>> temp = new List<EntryModel<int>>(); foreach (var item in db.GetAllSchedulesByExercise(output.Id)) { EntryModel<int> e = new EntryModel<int>(); e.Id = item; temp.Add(e); } output.Schedules = temp; return output; } catch(Exception exc) { System.Console.WriteLine("Fehler beim lokalen Abrufen einer Übung: " + exc.StackTrace); return null; } } }
/* /// <summary> /// Gibt einen Trainingsplan zurück /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task<ScheduleModel> GetScheduleByIdAsync(int id) { if (Online) { try { ScheduleModel schedule = await mgnService.GetScheduleByIdAsync(id); return schedule; } catch (ServerException ex) { System.Console.WriteLine("Fehler beim Online-Abrufen eines Trainingsplans: " + ex.StackTrace); throw; } } else { try { Schedule schedule = db.GetScheduleById(id); ScheduleModel result = new ScheduleModel(); result.Id = schedule.Id; result.Name = schedule.Name; result.Url = schedule.Url; result.UserId = schedule.UserId; List<EntryModel<int>> temp = new List<EntryModel<int>>(); foreach (var item in db.GetAllExercisesBySchedule(result.Id)) { EntryModel<int> e = new EntryModel<int>(); e.Id = item; temp.Add(e); } result.Exercises = temp; return result; } catch (Exception exc) { System.Console.WriteLine("Fehler beim lokalen Abrufen eines Trainingsplans: " + exc.StackTrace); return null; } } }*/ /// <summary> /// Gibt die Übungen eines Trainingsplans zurück /// </summary> /// <param name="scheduleId"></param> /// <returns></returns> public async Task<List<ExerciseModel>> GetExercisesForSchedule(int scheduleId) { if (Online) { try { ScheduleModel schedule = await mgnService.GetScheduleByIdAsync(scheduleId); IEnumerable<EntryModel<int>> scheduleExercises = schedule.Exercises; List<ExerciseModel> result = new List<ExerciseModel>(); foreach (var exercise in scheduleExercises) { //Für jede zugewiesene Id die Exercise raussuchen und der IEnumerable hinzufügen ExerciseModel temp = await mgnService.GetExerciseByIdAsync(exercise.Id); result.Add(temp); } //Übernehmen der Exercises in die lokale DB foreach (var item in result) { Exercise e = new Exercise(); e.Description = item.Description; e.Id = item.Id; e.Name = item.Name; e.Url = item.Url; e.WasOffline = false; db.insertUpdateExercise(e); //db.InsertScheduleHasExercises(scheduleId, e.Id, false); } return result; } catch (ServerException ex) { System.Console.WriteLine("Fehler beim Online-Abrufen der Übungen eines Trainingsplans: " + ex.StackTrace); throw; } } else { try { List<ExerciseModel> result = new List<ExerciseModel>(); //Anhand der scheduleId alle Exercises aus der ScheduleHasExercises-Tabelle holen Schedule test = db.GetScheduleById(scheduleId); List<Exercise> exercises = db.GetExercisesOfSchedule(scheduleId); foreach (var item in exercises) { ExerciseModel e = new ExerciseModel(); e.Description = item.Description; e.Name = item.Name; e.Url = item.Name; e.Id = item.Id; List<EntryModel<int>> schedules = new List<EntryModel<int>>(); EntryModel<int> temp = new EntryModel<int>(); temp.Id = scheduleId; schedules.Add(temp); e.Schedules = schedules; result.Add(e); } if(result.Count == 0) { Console.WriteLine("Keine Übungen zu dem Trainingsplan vorhanden"); return null; } return result; } catch (Exception exc) { System.Console.WriteLine("Fehler beim lokalen Abrufen eines Trainingsplans: " + exc.StackTrace); return null; } } }
/// <summary> /// Speichert oder updatet eine Übung in der lokalen DB /// </summary> /// <param name="data">Exercise</param> /// <param name="path">SQLite Connection String</param> /// <returns>0 -> Update</returns> /// <returns>1 -> Insert</returns> /// <returns>-1 -> Exception</returns> public int insertUpdateExercise(Exercise data) { SQLiteConnection db; List<Exercise> tempList; Exercise temp; try { db = new SQLiteConnection(path); tempList = db.Query<Exercise>("Select * From Exercise Where Id=?", data.Id); if (tempList.Count != 0) { temp = tempList.First<Exercise>(); //Bei temp alles updaten außer Id und LocalId und dann ein Update darauf fahren temp.Name = data.Name; temp.Url = data.Url; temp.Description = data.Description; temp.WasOffline = false; db.Update(temp); return 0; } db.Insert(data); return 1; } catch (SQLiteException ex) { Console.WriteLine(ex.Message); return -1; } }