/// <summary> /// Refreshes days total values, /// </summary> /// <param name="day">Day that has been queryed from db before</param> public void reduceDayTotals(Day day, IntakeViewModel intake) { using (var db = new SQLite.SQLiteConnection(App.DBPath)) { day.Total -= intake.Calories; day.Carbohydrates -= intake.Carbohydrates; day.Protein -= intake.Protein; day.Fat -= intake.Fat; day.Fibre -= intake.Fibre; db.Update(day); } }
/** public string GetCustomerName(int customerId) { string customerName = "Unknown"; using (var db = new SQLite.SQLiteConnection(app.DBPath)) { var customer = (db.Table<Customer>().Where( c => c.Id == customerId)).Single(); customerName = customer.Name; } return customerName; }**/ ///Saves an intake WARNING: must have dayid which exists on db! public string SaveIntake(IntakeViewModel intake) { string result = string.Empty; using (var db = new SQLite.SQLiteConnection(App.DBPath)) { string change = string.Empty; var day = (db.Table<Day>().Where(d => d.Id == intake.DayId)).SingleOrDefault(); try { var existingIntake = (db.Table<Intake>().Where( i => i.Id == intake.Id)).SingleOrDefault(); if (existingIntake != null) { existingIntake.DayId = intake.DayId; //not needed? existingIntake.Name = intake.Name; //not needed? existingIntake.Grams = intake.Grams; existingIntake.Carbohydrates = intake.Carbohydrates; existingIntake.Protein = intake.Protein; existingIntake.Fat = intake.Fat; existingIntake.Fibre = intake.Fibre; existingIntake.Calories = intake.Calories; //Refresh the total values int success = db.Update(existingIntake); } else { int success = db.Insert(new Intake() { Id = this.GetNewDayId(), DayId = intake.DayId, Name = intake.Name, Grams = intake.Grams, Carbohydrates = intake.Carbohydrates, Protein = intake.Protein, Fat = intake.Fat, Fibre = intake.Fibre, Calories = intake.Calories }); } result = "Success"; } catch (Exception) { result = "This intake was not saved."; } // refreshDayTotals(day); } return result; }
//private CaloriesCounter.App app = (Application.Current as App); public IntakeViewModel GetIntake(int intakeId) { var intake = new IntakeViewModel(); using (var db = new SQLite.SQLiteConnection(App.DBPath)) { var _intake = (db.Table<Intake>().Where( i => i.Id == intakeId)).Single(); intake.Id = _intake.Id; intake.Name = _intake.Name; intake.DayId = _intake.DayId; intake.Calories = _intake.Calories; intake.Carbohydrates = _intake.Carbohydrates; intake.Protein = _intake.Protein; intake.Fat = _intake.Fat; intake.Fibre = _intake.Fibre; } return intake; }
public void DeleteIntake(IntakeViewModel intake) { using (var db = new SQLite.SQLiteConnection(App.DBPath)) { Intake delete = db.Table<Intake>().Where(i => i.Id == intake.Id).SingleOrDefault(); var day = (db.Table<Day>().Where(d => d.Id == intake.DayId)).SingleOrDefault(); reduceDayTotals(day, intake); db.Delete(delete); } }
public bool CreateIntake(IntakeViewModel intake) { bool result = false; using (var db = new SQLite.SQLiteConnection(App.DBPath)) { string change = string.Empty; var day = (db.Table<Day>().Where(d => d.Id == intake.DayId)).SingleOrDefault(); try { int success = db.Insert(new Intake() { Id = this.GetNewDayId(), DayId = intake.DayId, Name = intake.Name, Grams = intake.Grams, Carbohydrates = intake.Carbohydrates, Protein = intake.Protein, Fat = intake.Fat, Fibre = intake.Fibre, Calories = intake.Calories }); result = true; } catch (Exception) { result = false; } addUpDayTotals(day, intake); } return result; }
private void ButtonAddToDiary_Click(object sender, RoutedEventArgs e) { IntakeViewModel intake = new IntakeViewModel(); Item item = ListViewItems.SelectedItem as Item; intake.Grams = add.getCounterClass().Grams; intake.Carbohydrates = item.Carbohydrates / 100F * intake.Grams; intake.Protein = item.Protein / 100F * intake.Grams; intake.Fat = item.Fat / 100F * intake.Grams; intake.Fibre = item.Fibre / 100F * intake.Grams; intake.Id = 0; intake.DayId = App.CurrentDay.Id; intake.Name = item.Name; intake.Calories = (int)add.getCounterClass().CountedCalories; var loader = new Windows.ApplicationModel.Resources.ResourceLoader(); if (intake.CreateIntake(intake)) { textBlock.Text = loader.GetString("AddingSuccess"); textBlock.Visibility = Visibility.Visible; var f = this.Resources["Storyboard1"] as Storyboard; if (f != null) f.Begin(); } else { ShowError(loader.GetString("AddingError")); } }
// private CaloriesCounter.App app = (Application.Current as App); /* public ObservableCollection<IntakesViewModel> GetIntakes() { intakes = new ObservableCollection<IntakesViewModel>(); using (var db = new SQLite.SQLiteConnection(app.DBPath)) { var query = db.Table<Intake>(); foreach (var _intake in query) { var intake = new IntakeViewModel() { Id = _intake.Id, DayId = _intake.DayId //TODO: other properties too }; intakes.Add(intake); } } return intakes; } */ /* /// <summary> /// Gets intakes to collection by dayId /// </summary> /// <param name="dayId"></param> /// <returns></returns> public ObservableCollection<IntakeViewModel> GetIntakes(int dayId) { intakes = new ObservableCollection<IntakeViewModel>(); using (var db = new SQLite.SQLiteConnection(App.DBPath)) { var query = (db.Table<Intake>().Where( i => i.DayId == dayId)); foreach (var _intake in query) { var intake = new IntakeViewModel() { Id = _intake.Id, DayId = _intake.DayId, Name = _intake.Name //TODO: other properties too }; intakes.Add(intake); } } return intakes; }*/ /// <summary> /// Gets intakes to collection by dayId /// </summary> /// <param name="dayId"></param> /// <returns></returns> public List<IntakeViewModel> GetIntakesList(int dayId) { List<IntakeViewModel> list = new List<IntakeViewModel>(); using (var db = new SQLite.SQLiteConnection(App.DBPath)) { var query = (db.Table<Intake>().Where( i => i.DayId == dayId)); foreach (var _intake in query) { var intake = new IntakeViewModel() { Id = _intake.Id, DayId = _intake.DayId, Name = _intake.Name, Carbohydrates = _intake.Carbohydrates, Protein = _intake.Protein, Fat = _intake.Fat, Fibre = _intake.Fibre, Calories = _intake.Calories, Grams = _intake.Grams }; list.Add(intake); } } return list; }