/** * Add recipe to the database * */ public static int AddRecipeAsync(Recipe r, int index = -1) { int useIndex = RecipeManager.instance.lastRecipeID; if (index > -1) { useIndex = index; } //form query int usedId = -1; String query = @"INSERT INTO recipes VALUES (" + useIndex + ", '" + MySqlHelper.EscapeString(r.title) + "', '" + MySqlHelper.EscapeString(r.description) + "' , " + RecipeManager.instance.GetCurrentUser().id + ");"; DBConnector.initAsync(); MySqlCommand cmd = new MySqlCommand(query, DBConnector.conn); cmd.ExecuteNonQuery(); //add ingredients IngredientDAO.AddIngredient((int)cmd.LastInsertedId, r); DBConnector.conn.Close(); usedId = RecipeManager.instance.lastRecipeID; RecipeManager.instance.lastRecipeID++; return(usedId); }
/** * Get a list of all recipes by given user id * */ public static List <Recipe> GetAll(int userid) { List <Recipe> r = new List <Recipe>(); //form query String query = @"SELECT * FROM recipes WHERE users_id = " + userid; DBConnector.initAsync(); MySqlCommand cmd = new MySqlCommand(query, DBConnector.conn); MySqlDataReader reader = cmd.ExecuteReader(); //add results to the collection while (reader.Read()) { r.Add(new Recipe(reader["description"] + "", reader["title"] + "")); r.ElementAt(r.Count - 1).id = int.Parse(reader["idrecipes"] + ""); r.ElementAt(r.Count - 1).ingredients = IngredientDAO.GetAll((r.ElementAt(r.Count - 1).id)); } DBConnector.conn.Close(); String queryMax = @"SELECT MAX(idRecipes) AS RINDEX FROM recipes"; DBConnector.initAsync(); MySqlCommand cmdMax = new MySqlCommand(queryMax, DBConnector.conn); MySqlDataReader readerMax = cmdMax.ExecuteReader(); while (readerMax.Read()) { RecipeManager.instance.lastRecipeID = (int)readerMax["RINDEX"] + 1; } DBConnector.conn.Close(); return(r); }