public async Task<List<Practice>> getAllPracticesAsync(string userId, int scheduleId, int exerciseId) { List<Practice> resultListe = new List<Practice>(); Practice pOn; if (Online) { try { List<PracticeModel> practices; practices = await mgnService.getAllPracticesAsync(scheduleId, exerciseId); foreach (var item in practices) { if (item.Id != 0) { pOn = new Practice(); pOn.UserId = item.UserId; pOn.Id = item.Id; pOn.ScheduleId = item.ScheduleId; pOn.ExerciseId = item.ExerciseId; pOn.Timestamp = item.Timestamp; pOn.Weight = item.Weight; pOn.Repetitions = item.Repetitions; pOn.NumberOfRepetitions = item.NumberOfRepetitions; resultListe.Add(pOn); int result = db.insertUpdatePracticeOnline(pOn); if (result != -1) { Console.WriteLine("Training auch lokal angelegt"); } } } return resultListe; } catch (ServerException ex) { System.Console.WriteLine("Fehler beim Holen aller Trainings: " + ex.StackTrace); throw; } } else { try { List<Practice> practices; practices = db.GetAllPracticesByUserScheduleExercise(userId, scheduleId, exerciseId); foreach (var item in practices) { if (item.Id != 0 || item.LocalId != 0) { resultListe.Add(item); } } return resultListe; } catch (Exception exc) { System.Console.WriteLine("Fehler beim offline Abrufen der Trainings: " + exc.StackTrace); } } return resultListe; }
/// <summary> /// Entsprechend des Netzwerkstatus wird lokal oder am Server angelegt /// </summary> /// <param name="id"></param> /// <param name="scheduleId"></param> /// <param name="exerciseId"></param> /// <param name="userId"></param> /// <param name="timestamp"></param> /// <param name="weight"></param> /// <param name="repetitions"></param> /// <param name="numberOfRepetitions"></param> /// <returns></returns> public async Task<bool> createPracticeAsync(int scheduleId, int exerciseId, string userId, DateTime timestamp = default(DateTime), double weight = 0, int repetitions = 0, int numberOfRepetitions = 0) { Practice pOff; Practice pOn; int id; if (Online) { try { id = await mgnService.recordPractice(scheduleId, exerciseId, userId, timestamp, weight, repetitions, numberOfRepetitions); if (id != 0) { pOn = new Practice(); pOn.UserId = userId; pOn.Id = id; pOn.ScheduleId = scheduleId; pOn.ExerciseId = exerciseId; pOn.Timestamp = timestamp; pOn.Weight = weight; pOn.Repetitions = repetitions; pOn.NumberOfRepetitions = numberOfRepetitions; int result = db.insertUpdatePracticeOnline(pOn); if (result != -1) { Console.WriteLine("Training auch lokal angelegt"); } return true; } else return false; } catch (ServerException ex) { throw; } } else { try { pOff = new Practice(); pOff.UserId = userId; pOff.ScheduleId = scheduleId; pOff.ExerciseId = exerciseId; pOff.Timestamp = timestamp; pOff.Weight = weight; pOff.Repetitions = repetitions; pOff.NumberOfRepetitions = numberOfRepetitions; pOff.WasOffline = true; int result = db.insertUpdatePracticeOffline(pOff); if (result != -1) return true; else return false; } catch (Exception exc) { System.Console.WriteLine("Fehler beim lokalen Anlegen des Trainings: " + exc.StackTrace); } } return true; }
/// <summary> /// Löscht ein Training aus der lokalen DB /// </summary> /// <param name="data">Practice</param> /// <param name="path">SQLite Connection String</param> /// <returns>1 -> Delete</returns> /// <returns>-1 -> Exception</returns> public int deletePractice(Practice data) { try { var db = new SQLiteConnection(path); db.Delete(data); return 1; } catch (SQLiteException ex) { Console.WriteLine(ex.Message); return -1; } }
/// <summary> /// Setzt den Status des Trainings auf Online /// </summary> /// <param name="data"></param> public bool setPracticeOnline(Practice data) { SQLiteConnection db; List<Practice> practice; Practice p; try { db = new SQLiteConnection(path); practice = db.Query<Practice>("Select * From Practice Where LocalId=?", data.LocalId); List<Practice> res = db.Query<Practice>("Update Practice Set WasOffline='false' Where LocalId=?", data.LocalId); p = practice.First<Practice>(); p.WasOffline = false; if (db.Update(p, typeof(Practice)) != -1) return true; else return false; } catch (SQLiteException ex) { Console.WriteLine(ex.Message); } return false; }
/// <summary> /// Speichert oder updatet ein Training in der lokalen DB vom Online-Status /// </summary> /// <param name="data">Prcatice</param> /// <param name="path">SQLite Connection String</param> /// <returns>0 -> Update</returns> /// <returns>1 -> Insert</returns> /// <returns>-1 -> Exception</returns> public int insertUpdatePracticeOnline(Practice data) { SQLiteConnection db; List<Practice> tempList; Practice temp; try { db = new SQLiteConnection(path); tempList = db.Query<Practice>("Select * From Practice Where Id=?", data.Id); if (tempList.Count != 0) { temp = tempList.First<Practice>(); //Bei temp alles updaten außer LocalId und dann ein Update darauf fahren temp.ExerciseId = data.ExerciseId; temp.NumberOfRepetitions = data.NumberOfRepetitions; temp.Repetitions = data.Repetitions; temp.ScheduleId = data.ScheduleId; temp.Timestamp = data.Timestamp; temp.Url = data.Url; temp.UserId = data.UserId; temp.Weight = data.Weight; temp.Id = data.Id; temp.WasOffline = false; db.Update(temp); return 0; } db.Insert(data); List<Practice> testListe = db.Query<Practice>("Select * From Practice Where LocalId=?", data.LocalId); Practice test = testListe.First<Practice>(); Console.WriteLine(test.LocalId); return 1; } catch (SQLiteException ex) { Console.WriteLine(ex.Message); return -1; } }