using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace JRCookbookBusiness { public class RecipeTip: ICloneable { public Guid? recipeTipID = null; public Guid? recipeID = null; public String tipText = String.Empty; public Int32 tipIndex = 0; public bool isHeading = false; public RecipeTip() { } public RecipeTip(Recipe parentRecipe) { recipeID = parentRecipe.recipeID; } public RecipeTip(Guid recipeTipID) { PopulateByID(recipeTipID); } public void PasteIntoChapter(Guid newRecipeID) { this.recipeTipID = null; this.recipeID = newRecipeID; } public void Save() { if (this.recipeTipID == null) { this.recipeTipID = clsDatabaseLayer.GetDatabaseLayer().GetNewIDRecipeTip(); clsDatabaseLayer.GetDatabaseLayer().InsertRecipeTip(this); } else { clsDatabaseLayer.GetDatabaseLayer().UpdateRecipeTip(this); } } public void Delete() { clsDatabaseLayer.GetDatabaseLayer().DeleteRecipeTipByID(recipeTipID.Value); } public static void DeleteAllByRecipeID(Guid recipeID) { clsDatabaseLayer.GetDatabaseLayer().DeleteRecipeTipsByRecipeID(recipeID); } public static RecipeTip GetRecipeTipByID(Guid recipeTipID) { return new RecipeTip(recipeTipID); } public static RecipeTip GetRecipeTipByDataRow(DataRow row) { var newTip = new RecipeTip(); newTip.PopulateFromDataRow(row); return newTip; } private void PopulateByID(Guid recipeTipID) { DataSet recordSet; recordSet = clsDatabaseLayer.GetDatabaseLayer().GetRecipeTipByID(recipeTipID); if (recordSet.Tables[0].Rows.Count > 0) { DataRow ldbrwRow; ldbrwRow = recordSet.Tables[0].Rows[0]; PopulateFromDataRow(ldbrwRow); } } private void PopulateFromDataRow(DataRow dataRow) { InitializeAllFields(); if (dataRow.IsNull("recipeTipID")) recipeTipID = null; else recipeTipID = (Guid)dataRow["recipeTipID"]; if (dataRow.IsNull("recipeID")) recipeID = null; else recipeID = (Guid) dataRow["recipeID"]; if (dataRow.IsNull("tipText")) tipText = String.Empty; else tipText = (String)dataRow["tipText"]; if (dataRow.IsNull("tipIndex")) tipIndex = 0; else tipIndex = (Int32) dataRow["tipIndex"]; if (dataRow.IsNull("Heading")) isHeading = false; else isHeading = (((String)dataRow["Heading"]).ToUpper() == "Y"); } private void InitializeAllFields() { recipeTipID = null; recipeID = null; tipText = String.Empty; tipIndex = 0; isHeading = false; } public object Clone() { return this.MemberwiseClone(); } } }