// Function returns the Dish_Ingredient with the specified dish_id and ing_name from the database public async Task <Dish_Ingredient> GetById(int dish_id, string ing_name) { using (NpgsqlConnection sql = new NpgsqlConnection(_connectionString)) // Specifying database context { using (NpgsqlCommand cmd = new NpgsqlCommand("\"spDish_Ingredient_GetById\"", sql)) // Specifying stored procedure { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new NpgsqlParameter("dish_id", NpgsqlDbType.Integer)); cmd.Parameters.Add(new NpgsqlParameter("ing_name", NpgsqlDbType.Varchar)); cmd.Parameters[0].Value = dish_id; cmd.Parameters[1].Value = ing_name; Dish_Ingredient response = null; await sql.OpenAsync(); // Parsing the data retrieved from the database using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { response = MapToValue(reader); } } return(response); } } }
public async Task <ActionResult> Post([FromBody] Dish_Ingredient dish_ingredient) { dish_ingredient.Ing_Name = textInfo.ToTitleCase(dish_ingredient.Ing_Name.ToLower()); try { // Making sure that the referred dish and ingredient exist await _ingredientRepository.GetByName(dish_ingredient.Ing_Name); await _dishRepository.GetById(dish_ingredient.Dish_ID); // Inserting record in the Dish_Ingredient table await _repository.Insert(dish_ingredient); return(Ok("Dish_Ingredient record inserted successfully\n")); } catch (Npgsql.PostgresException ex) { // Postgres threw an exception return(BadRequest(ex.Message.ToString())); } catch { // Unknown error return(BadRequest("Error: Dish_Ingredient record was not inserted\n")); } }
// Function inserts a Dish_Ingredient record in the database public async Task Insert(Dish_Ingredient dish_ingredient) { using (NpgsqlConnection sql = new NpgsqlConnection(_connectionString)) // Specifying database context { using (NpgsqlCommand cmd = new NpgsqlCommand("\"spDish_Ingredient_InsertValue\"", sql)) // Specifying stored procedure { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new NpgsqlParameter("dish_id", NpgsqlDbType.Integer)); cmd.Parameters.Add(new NpgsqlParameter("ing_name", NpgsqlDbType.Varchar)); cmd.Parameters[0].Value = dish_ingredient.Dish_ID; cmd.Parameters[1].Value = dish_ingredient.Ing_Name; await sql.OpenAsync(); await cmd.ExecuteNonQueryAsync(); return; } } }
public ActionResult AddDishAction(string dish_title, string dish_image, string[] dish_ingredient, int[] dish_ingredient_amount , string[] dish_step, string[] dish_tag, int dish_timecooking, string dish_category, string dish_description) { var db = new IFood(); Dish dish = new Dish(); try { dish.Id = Guid.NewGuid(); dish.Name = dish_title; dish.ImageLink = dish_image; dish.AuthorId = db.Users.FirstOrDefault().Id; dish.IsActive = true; dish.IsDelete = false; dish.Rate = 0; dish.TimeCooking = dish_timecooking + " minutes"; dish.CreateOn = DateTime.Now; dish.Description = dish_description; for (int i = 0; i < dish_ingredient.Length; i++) { Dish_Ingredient dish_Ingredient = new Dish_Ingredient(); dish_Ingredient.IngredientId = Guid.Parse(dish_ingredient[i]); dish_Ingredient.Amount = dish_ingredient_amount[i]; var ingredient = dish_ingredient[i].ToString(); dish_Ingredient.UnitId = int.Parse(db.Ingredients.Where(ing => ing.Id.ToString().Equals(ingredient)).First().UnitId.Trim()); dish_Ingredient.DishId = dish.Id; dish_Ingredient.Description = ""; dish.Dish_Ingredient.Add(dish_Ingredient); } for (int i = 0; i < dish_step.Length; i++) { StepByStep step = new StepByStep(); step.DishId = dish.Id; step.Title = "Step" + (i + 1); step.Description = dish_step[i]; dish.StepBySteps.Add(step); } for (int i = 0; i < dish_tag.Length; i++) { Course_Dish course = new Course_Dish(); course.CourseId = Guid.Parse(dish_tag[i]); course.DishId = dish.Id; dish.Course_Dish.Add(course); } Category_Dish category = new Category_Dish(); category.CategoryId = Guid.Parse(dish_category); category.DishId = dish.Id; category.Description = dish_description; dish.Category_Dish.Add(category); db.Dishes.Add(dish); db.SaveChanges(); } catch (Exception e) { } return(RedirectToAction("AdminIndex")); }
public ActionResult EditDishAction(string dish_id, string dish_title, string dish_image, string[] dish_ingredient, int[] dish_ingredient_amount , string[] dish_step, string[] dish_tag, int dish_timecooking, string dish_category, string dish_description) { var db = new IFood(); Dish dish = db.Dishes.Where(d => d.Id.ToString().Equals(dish_id)).FirstOrDefault <Dish>(); try { dish.Name = dish_title; dish.ImageLink = dish_image; dish.Description = dish_description; dish.TimeCooking = dish_timecooking + " minutes"; for (int i = 0; i < dish_ingredient.Length; i++) { var ingredient = dish_ingredient[i].ToString(); if (i >= dish.Dish_Ingredient.Count()) { Dish_Ingredient dish_Ingredient = new Dish_Ingredient(); dish_Ingredient.IngredientId = Guid.Parse(dish_ingredient[i]); dish_Ingredient.Amount = dish_ingredient_amount[i]; dish_Ingredient.UnitId = int.Parse(db.Ingredients.Where(ing => ing.Id.ToString().Equals(ingredient)).First().UnitId.Trim()); dish_Ingredient.DishId = dish.Id; dish_Ingredient.Description = ""; dish.Dish_Ingredient.Add(dish_Ingredient); } else { dish.Dish_Ingredient.ElementAt(i).IngredientId = Guid.Parse(dish_ingredient[i]); dish.Dish_Ingredient.ElementAt(i).Amount = dish_ingredient_amount[i]; dish.Dish_Ingredient.ElementAt(i).UnitId = int.Parse(db.Ingredients.Where(ing => ing.Id.ToString().Equals(ingredient)).First().UnitId.Trim()); } } for (int i = 0; i < dish_step.Length; i++) { if (i >= dish.StepBySteps.Count()) { StepByStep step = new StepByStep(); step.DishId = dish.Id; step.Title = "Step" + (i + 1); step.Description = dish_step[i]; dish.StepBySteps.Add(step); } else { dish.StepBySteps.ElementAt(i).Description = dish_step[i]; } } for (int i = 0; i < dish_tag.Length; i++) { if (i >= dish.Course_Dish.Count()) { Course_Dish course = new Course_Dish(); course.CourseId = Guid.Parse(dish_tag[i]); course.DishId = dish.Id; dish.Course_Dish.Add(course); } else { dish.Course_Dish.ElementAt(i).CourseId = Guid.Parse(dish_tag[i]); } } dish.Category_Dish.First().Description = dish_description; db.SaveChanges(); } catch (Exception e) { } return(RedirectToAction("AdminIndex")); }
private IHttpActionResult Post([FromBody] JObject json) { try { using (var db = new RestaurantContext()) { IHttpActionResult result; DishCE d = JsonConvert.DeserializeObject <DishCE>(json.ToString()); if (db.Categories.Find(d.Category.ID) != null) { Dish d1 = new Dish(); d1.ID = d.ID; d1.Name = d.Name; d1.ImageURL = d.ImageURL; d1.Area = d.Area; d1.Cooking = d.Cooking; d1.ID_Category = d.Category.ID; db.Dishes.Add(d1); foreach (var tag in d.Tags) { if (db.Tags.FirstOrDefault(t => t.TagName == tag) == default) { Tag tag1 = new Tag(); tag1.TagName = tag; db.Tags.Add(tag1); } } db.SaveChanges(); foreach (var tag in d.Tags) { Tag tag1 = db.Tags.First(t => t.TagName == tag); Dish_Tag dt = new Dish_Tag(); dt.ID_Dish = d1.ID; dt.ID_Tag = tag1.ID; db.Dish_Tag.Add(dt); } foreach (var ingt in d.Ingredients) { string ingtQty = ingt.Substring(0, ingt.IndexOf('*')); string ingtID = ingt.Substring(ingt.IndexOf('*') + 1); Dish_Ingredient DI = new Dish_Ingredient(); DI.ID_Dish = d1.ID; DI.ID_Ingredient = Convert.ToInt32(ingtID); DI.Quantity = ingtQty; db.Dish_Ingredient.Add(DI); } db.SaveChanges(); d.ID = d1.ID; result = Ok(d); } else { result = Content(HttpStatusCode.Conflict, "Error"); } return(result); } } catch (Exception ex) { return(Content(HttpStatusCode.Conflict, ex.Message)); } }
private IHttpActionResult Put([FromBody] JObject json) { try { using (var db = new RestaurantContext()) { IHttpActionResult result; DishCE d = JsonConvert.DeserializeObject <DishCE>(json.ToString()); Dish d1 = db.Dishes.Find(d.ID); if (d1 != null) { d1.Name = !string.IsNullOrEmpty(d.Name) ? d.Name : d1.Name; d1.ImageURL = !string.IsNullOrEmpty(d.ImageURL) ? d.ImageURL : d1.ImageURL; d1.Area = !string.IsNullOrEmpty(d.Area) ? d.Area : d1.Area; d1.Cooking = !string.IsNullOrEmpty(d.Cooking) ? d.Cooking : d1.Cooking; d1.ID_Category = !string.IsNullOrEmpty(d.Category.ID.ToString()) ? d.Category.ID : d1.Category.ID; if (d.Tags.Count() != 0) { var dish_Tags = db.Dish_Tag.Where(dt => dt.ID_Dish == d.ID).ToList <Dish_Tag>(); foreach (Dish_Tag item in dish_Tags) { db.Dish_Tag.Remove(item); } db.SaveChanges(); foreach (var tag in d.Tags) { if (db.Tags.FirstOrDefault(t => t.TagName == tag) == default) { Tag tag1 = new Tag(); tag1.TagName = tag; db.Tags.Add(tag1); } } db.SaveChanges(); foreach (var tag in d.Tags) { Tag tag1 = db.Tags.First(t => t.TagName == tag); Dish_Tag dt1 = new Dish_Tag(); dt1.ID_Dish = d1.ID; dt1.ID_Tag = tag1.ID; db.Dish_Tag.Add(dt1); } } if (d.Ingredients.Count() != 0) { var dish_ingt = db.Dish_Ingredient.Where(dt => dt.ID_Dish == d.ID).ToList <Dish_Ingredient>(); foreach (Dish_Ingredient item in dish_ingt) { db.Dish_Ingredient.Remove(item); } db.SaveChanges(); foreach (var ingt in d.Ingredients) { string ingtQty = ingt.Substring(0, ingt.IndexOf('*')); string ingtID = ingt.Substring(ingt.IndexOf('*') + 1); Dish_Ingredient DI = new Dish_Ingredient(); DI.ID_Dish = d1.ID; DI.ID_Ingredient = Convert.ToInt32(ingtID); DI.Quantity = ingtQty; db.Dish_Ingredient.Add(DI); } } db.SaveChanges(); result = Content(HttpStatusCode.OK, "Row updated"); } else { result = Content(HttpStatusCode.NotFound, "Dish not found"); } return(result); } } catch (Exception ex) { return(Content(HttpStatusCode.Conflict, ex.Message)); } }