public static Meals GetMealWithID(int MealID) { string str = DataAccess.CnnVal(DataAccess.currentDBname); using (IDbConnection connection = new SqlConnection(str)) { var toReturn = connection.QueryFirst <Meals>("SELECT * FROM dbo.Dishes WHERE id=@id", new { id = MealID }); Meals meal = Meals.GetMealClassWithDishType(toReturn.Dishtype, toReturn); if (meal.GetType() == typeof(Burger)) { dynamic burgerData = connection.QuerySingle("SELECT * FROM dbo.Dishes WHERE Id=@dishid", new { dishid = meal.Id }); (meal as Burger).Bun = burgerData.Bun; (meal as Burger).Cheese = burgerData.Cheese; (meal as Burger).Patty = burgerData.Patty; } if (meal.GetType() == typeof(Pizza)) { dynamic pizzaData = connection.QuerySingle("SELECT * FROM dbo.Dishes WHERE Id=@dishid", new { dishid = meal.Id }); (meal as Pizza).Toppings = pizzaData.Toppings; } if (meal.GetType() == typeof(Pasta)) { dynamic pastaData = connection.QuerySingle("SELECT * FROM dbo.Dishes WHERE Id=@dishid", new { dishid = meal.Id }); (meal as Pasta).Meat = pastaData.Meat; (meal as Pasta).Pastatype = pastaData.Pastatype; } if (meal.GetType() == typeof(Steak)) { dynamic steakData = connection.QuerySingle("SELECT * FROM dbo.Dishes WHERE Id=@dishid", new { dishid = meal.Id }); (meal as Steak).Steaktype = steakData.Steaktype; (meal as Steak).Side = steakData.Side; } if (meal.GetType() == typeof(Drinks)) { dynamic drinkData = connection.QuerySingle("SELECT * FROM dbo.Dishes WHERE Id=@dishid", new { dishid = meal.Id }); (meal as Drinks).Amount = (int)drinkData.Amount; (meal as Drinks).isAlcoholic = drinkData.isAlcoholic; } var allergens = connection.Query <Allergen.AllergenType>("SELECT AllergenId FROM AllergensInDishes WHERE DishId=@id", new { id = MealID }).ToArray(); foreach (var allergenAsInt in allergens) { meal.AddAllergen(allergenAsInt); } return(meal); } }
public static int InsertMealToDB(Meals meal) { string str = DataAccess.CnnVal(DataAccess.currentDBname); int mealId; using (IDbConnection connection = new SqlConnection(str)) { mealId = connection.QuerySingle <int>("INSERT dbo.Dishes (Name, Description, Sauce, Price, Dishtype) OUTPUT INSERTED.ID " + "VALUES (@name, @description, @sauce, @price, @dishtype)", new { name = meal.Name, description = meal.Description, sauce = meal.Sauce, price = meal.Price, dishtype = meal.Dishtype }); if (meal.GetType() == typeof(Burger)) { mealId = connection.QuerySingle <int>("UPDATE dbo.Dishes SET Bun=@bun, Patty=@patty, Cheese=@cheese OUTPUT INSERTED.ID WHERE Id=@id", new { bun = (meal as Burger).Bun, patty = (meal as Burger).Patty, cheese = (meal as Burger).Cheese, id = mealId }); } else if (meal.GetType() == typeof(Pizza)) { mealId = connection.QuerySingle <int>("UPDATE dbo.Dishes SET Toppings=@toppings OUTPUT INSERTED.ID WHERE Id=@id)", new { toppings = (meal as Pizza).Toppings, id = mealId }); } else if (meal.GetType() == typeof(Pasta)) { mealId = connection.QuerySingle <int>("UPDATE dbo.Dishes SET Pastatype=@pastatype, Meat=@meat OUTPUT INSERTED.ID WHERE Id=@id", new { meat = (meal as Pasta).Meat, pastatype = (meal as Pasta).Pastatype, id = mealId }); } else if (meal.GetType() == typeof(Steak)) { mealId = connection.QuerySingle <int>("UPDATE dbo.Dishes SET Steaktype=@steaktype, Side=@side OUTPUT INSERTED.ID WHERE Id=@id", new { steaktype = (meal as Steak).Steaktype, side = (meal as Steak).Side, id = mealId }); } else if (meal.GetType() == typeof(Drinks)) { mealId = connection.QuerySingle <int>("UPDATE dbo.Dishes SET Amount=@amount, isAlcoholic=@isalcoholic OUTPUT INSERTED.ID WHERE Id=@id", new { amount = (meal as Drinks).Amount, isalcoholic = (meal as Drinks).isAlcoholic, id = mealId }); } } if (mealId > 0 && meal.GetAllergens().Count > 0) { using (IDbConnection connection = new SqlConnection(str)) { foreach (var allergen in meal.GetAllergens()) { connection.Execute("INSERT dbo.AllergensInDishes (AllergenId, DishId) VALUES (@Allergenid, @Dishid)", new { Allergenid = (int)allergen, Dishid = mealId }); } } } return(mealId); }
public void ShowMealData(Meals meal) { Console.WriteLine(); Console.WriteLine($"Id: {meal.Id}"); Console.WriteLine($"{meal.Name}"); if (meal.Description != null) { Console.WriteLine($"{meal.Description}"); } Type type = meal.GetType(); if (type == typeof(Burger)) { Burger burger = (Burger)meal; Console.WriteLine($"Patty: {burger.Patty}"); Console.WriteLine($"Cheese: {burger.Cheese}"); Console.WriteLine($"Bun: {burger.Bun}"); } if (type == typeof(Pizza)) { Pizza pizza = (Pizza)meal; Console.WriteLine($"Toppings: {pizza.Toppings}"); } if (type == typeof(Pasta)) { Pasta pasta = (Pasta)meal; Console.WriteLine($"Type of pasta: {pasta.Pastatype}"); Console.WriteLine($"Meat: {pasta.Meat}"); } if (type == typeof(Steak)) { Steak steak = (Steak)meal; Console.WriteLine($"Type: {steak.Steaktype}"); Console.WriteLine($"Side: {steak.Side}"); } if (type == typeof(Drinks)) { Drinks drink = (Drinks)meal; Console.WriteLine($"Amout: {drink.Amount}cl"); if (drink.isAlcoholic == true) { Console.WriteLine("Alcoholic"); } else { Console.WriteLine("Non-Alcoholic"); } } if (meal.Sauce != null) { Console.WriteLine($"Sauce: {meal.Sauce}"); } foreach (var allergen in meal.Allergentypes) { if (allergen == Allergen.AllergenType.glutenFree) { Console.WriteLine("This dish is gluten free, " + Allergen.allergenChars[allergen]); } else if (allergen == Allergen.AllergenType.lactoseFree) { Console.WriteLine("This dish is lactose free, " + Allergen.allergenChars[allergen]); } else if (allergen == Allergen.AllergenType.milkFree) { Console.WriteLine("This dish is milk free, " + Allergen.allergenChars[allergen]); } else if (allergen == Allergen.AllergenType.lowLactose) { Console.WriteLine("This dish is low lactose, " + Allergen.allergenChars[allergen]); } } Console.WriteLine($"Price: {meal.Price}€"); }