public static FlowDocument PrintRecipe(Recipe r) { // Before printing check that all fields are valid if (r.Name == String.Empty || r.Time == String.Empty || r.Instructions == String.Empty || r.Writer == String.Empty) { throw new Exception("Reseptissä ei voi olla tyhjiä kenttiä!"); } // If all fields are valid print the recipe else { // Create a FlowDocument FlowDocument doc = new FlowDocument(); // Create a Section Section sec = new Section(); Section sec2 = new Section(); // Create first Paragraph Paragraph p1 = new Paragraph(); Paragraph p2 = new Paragraph(); // Create and add a new Bold, Italic and Underline Bold bld = new Bold(); bld.Inlines.Add(new Run(r.Name)); Italic italicBld = new Italic(); italicBld.Inlines.Add(bld); Underline underlineItalicBld = new Underline(); underlineItalicBld.Inlines.Add(italicBld); // Add Bold, Italic, Underline to Paragraph p1.Inlines.Add(underlineItalicBld); p1.Inlines.Add(new Run("\n" + r.Time)); p1.Inlines.Add(new Run("\n" + r.Writer)); p2.Inlines.Add(new Run(r.Instructions)); // Add Paragraph to Section sec.Blocks.Add(p1); sec2.Blocks.Add(p2); // Add Section to FlowDocument doc.Blocks.Add(sec); doc.Blocks.Add(sec2); return doc; } }
public static int SaveRecipe(Recipe recipe, IList types) { // Send information to database layer try { // Before printing check that all fields are valid if (recipe.Name == String.Empty || recipe.Time == String.Empty || recipe.Instructions == String.Empty || recipe.Writer == String.Empty) { throw new Exception("Reseptissä ei voi olla tyhjiä kenttiä!"); } return DBRecipes.SaveRecipe(recipe, types); } catch (Exception) { throw; } }
public static List<Recipe> GetAllRecipes(string searchWord, IList types) { List<Recipe> recipes = new List<Recipe>(); try { DataTable dt = DBRecipes.GetAll(searchWord, types); Recipe recipe; foreach (DataRow dr in dt.Rows) { recipe = new Recipe(Convert.ToInt32(dr["id"]), Convert.ToString(dr["name"]), Convert.ToString(dr["time"]), Convert.ToString(dr["instructions"]), Convert.ToString(dr["writer"])); recipes.Add(recipe); } return recipes; } catch (Exception) { throw; } }
public static int SaveRecipe(Recipe r, IList types) { string connStr = Mysql.GetConnStr(); MySqlConnection conn = null; MySqlTransaction tr = null; try { int index = 0; using (conn = new MySqlConnection(connStr)) { string sql = null; // Recipe has id. Update existing row if (r.Id > 0) { sql = "UPDATE recipe SET `name`='" + r.Name.ToString() + "', `time`='" + r.Time.ToString() + "', `instructions`='" + r.Instructions.ToString() + "', `writer`='" + r.Writer.ToString() + "' WHERE `id`='" + r.Id + "'"; // Add types if they are defined foreach (string s in types) { // TODO: Implement in next version } } // Recipe has no id. Create new row in database else { sql = "INSERT INTO recipe (`name`, `time`, `instructions`, `writer`) VALUES('"+r.Name+"', '"+r.Time+"', '"+r.Instructions+"', '"+r.Writer+"')"; // Add types if they are defined foreach (string s in types) { // TODO: Implement in next version } } conn.Open(); tr = conn.BeginTransaction(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.Transaction = tr; cmd.CommandText = sql; cmd.ExecuteNonQuery(); tr.Commit(); // Get last inserted id index = (int)cmd.LastInsertedId; // Return row id return index; } } catch (Exception) { tr.Rollback(); throw; } }
public static void DeleteRecipe(Recipe r) { // Remove recipe from database string connStr = Mysql.GetConnStr(); // TODO: Ensure that connection close is handled properly try { using (MySqlConnection conn = new MySqlConnection(connStr)) { conn.Open(); string sql = "DELETE FROM recipe WHERE id='" + r.Id + "'"; MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; // Delete rows from recipe cmd.CommandText = sql; cmd.ExecuteNonQuery(); } } catch (Exception) { throw; } }
private void btnSave_Click(object sender, RoutedEventArgs e) { // Save recipe into database try { // DataContext exists, user had clicked existing recipe if (dpList.DataContext != null) { Recipe r = (Recipe)dpList.DataContext; r.Name = tbRecipeName.Text; r.Time = tbRecipeTime.Text; r.Instructions = tbInstructions.Text; r.Writer = tbRecipeWriter.Text; // Save to database BLRecipes.SaveRecipe(r, lbRecipeType.SelectedItems); // Update datagrid //dgRecipes.ItemsSource = null; Recipe temp = recipes.Single(s => s.Id == r.Id); int index = recipes.IndexOf(temp); recipes[index] = r; dgRecipes.ItemsSource = recipes; // Set feedback string tbFeedBack.Text = "Resepti " + r.Name + " päivitetty."; } // If DataContext is set to null, that means that recipe is brand new else { Recipe r = new Recipe(tbRecipeName.Text, tbRecipeTime.Text, tbInstructions.Text, tbRecipeWriter.Text); // Save to database, Get id int index = BLRecipes.SaveRecipe(r, lbRecipeType.SelectedItems); // Set id r.Id = index; // Update datagrid //dgRecipes.ItemsSource = null; recipes.Add(r); dgRecipes.ItemsSource = recipes; // Set feedback string tbFeedBack.Text = "Resepti " + r.Name + " lisätty."; } StateMachine(States.NoSelection); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static void RemoveRecipe(Recipe recipe) { try { DBRecipes.DeleteRecipe(recipe); } catch (Exception) { throw; } }