private void btnDodaj_Click(object sender, RoutedEventArgs e) { double grami = 0; if (chckDorucak.IsChecked == false && chckRucak.IsChecked == false && chckVecera.IsChecked == false) { MessageBox.Show("Selektujte Obrok"); return; } if (txtGrami.Text.Trim().Length == 0) { MessageBox.Show("Unesite Visinu"); return; } else if (!double.TryParse(txtGrami.Text, out grami)) { MessageBox.Show("Unesite Visinu u pravilnom formatu"); return; } foreach (Namirnice n in listaNamirnica) { if (n.foodName.Equals(selektovanaNamirnica)) { n.kj = ((double.Parse(txtGrami.Text) / 100) * n.kj); n.kcal = ((double.Parse(txtGrami.Text) / 100) * n.kcal); n.protein = ((double.Parse(txtGrami.Text) / 100) * n.protein); n.uh = ((double.Parse(txtGrami.Text) / 100) * n.uh); n.masti = ((double.Parse(txtGrami.Text) / 100) * n.masti); Obroci obrok = new Obroci(0, korisnik.id, n.foodName, n.kj, n.kcal, n.protein, n.uh, n.masti, selektovanObrok, double.Parse(txtGrami.Text), DateTime.Now); sql.UserMealInsert(obrok); break; } } lbDorucak.Items.Clear(); lbRucak.Items.Clear(); lbVecera.Items.Clear(); Osvezi(); }
//UserNutritionInfo public List <Obroci> UserNutritionInfo(int userid) { string upit = "SELECT * FROM usermeals WHERE userid = @userid"; List <Obroci> listaNamirnica = new List <Obroci>(); MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand command = new MySqlCommand(upit, connection); MySqlDataAdapter adapter = new MySqlDataAdapter(command); DataTable dataTable = new DataTable(); connection.Open(); command.Parameters.AddWithValue("@userid", userid); adapter.Fill(dataTable); connection.Close(); for (int i = 0; i < dataTable.Rows.Count; i++) { int id = Int32.Parse(dataTable.Rows[i]["id"].ToString()); int uid = Int32.Parse(dataTable.Rows[i]["userid"].ToString()); string foodName = dataTable.Rows[i]["food_name"].ToString(); double kj = double.Parse(dataTable.Rows[i]["kj"].ToString()); double kcal = double.Parse(dataTable.Rows[i]["kcal"].ToString()); double protein = double.Parse(dataTable.Rows[i]["protein"].ToString()); double uh = double.Parse(dataTable.Rows[i]["uh"].ToString()); double masti = double.Parse(dataTable.Rows[i]["masti"].ToString()); double kolicina = double.Parse(dataTable.Rows[i]["kolicina"].ToString()); string obrok = dataTable.Rows[i]["obrok"].ToString(); DateTime vremeUnosa = (DateTime)dataTable.Rows[i]["vremeunosa"]; Obroci namirnica = new Obroci(id, uid, foodName, kj, kcal, protein, uh, masti, obrok, kolicina, vremeUnosa); listaNamirnica.Add(namirnica); } return(listaNamirnica); }
//UserMealsInsert public void UserMealInsert(Obroci obroci) { string upit = "INSERT INTO usermeals (userid, food_name, kj, kcal, protein, uh, masti, kolicina, obrok)" + "VALUES (@userid, @food_name, @kj, @kcal, @protein, @uh, @masti, @kolicina, @obrok)"; MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand command = new MySqlCommand(upit, connection); connection.Open(); command.Parameters.AddWithValue("@userid", obroci.userId); command.Parameters.AddWithValue("@food_name", obroci.foodName); command.Parameters.AddWithValue("@kj", obroci.kj); command.Parameters.AddWithValue("@kcal", obroci.kcal); command.Parameters.AddWithValue("@protein", obroci.protein); command.Parameters.AddWithValue("@uh", obroci.uh); command.Parameters.AddWithValue("@masti", obroci.masti); command.Parameters.AddWithValue("@kolicina", obroci.kolicina); command.Parameters.AddWithValue("@obrok", obroci.obrok); command.ExecuteNonQuery(); Console.WriteLine("==== Upisano ===="); connection.Close(); }