public ActionResult UpdateMeal(int id, MealViewModel Model, IFormFile file) { MealsInMenu meal = new MealsInMenu(); meal.IsMeal = Model.IsMeal; meal.Name = Model.Name; meal.Price = Model.Price; meal.Description = Model.Description; try { string uniqueFileName = null; if (Model.Logo != null) { string uploadsFolder = Path.Combine(_appEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Model.Logo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); Model.Logo.CopyTo(new FileStream(filePath, FileMode.Create)); } meal.Logo = uniqueFileName; _MealsService.Update(id, meal); return(RedirectToAction("Index")); } catch { return(View(Model)); } }
public MealsInMenu GetMealsInMenuById(int mimId) { MealsInMenu result = new MealsInMenu(); //Create the SQL Query for returning an mim category based on its primary key string sqlQuery = String.Format("select * from MealsInMenues where MealsInMenueID={0}", mimId); //Create and open a connection to SQL Server SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sports_db"].ConnectionString); connection.Open(); SqlCommand command = new SqlCommand(sqlQuery, connection); SqlDataReader dataReader = command.ExecuteReader(); //load into the result object the returned row from the database if (dataReader.HasRows) { while (dataReader.Read()) { result.MealTypeID = Convert.ToInt32(dataReader["MealTypeID"]); result.MenuID = Convert.ToInt32(dataReader["MenuID"]); result.MealsInMenueID = Convert.ToInt32(dataReader["MealsInMenueID"]); } } //Close and dispose CloseAndDispose(command, connection); return(result); }
public int InsertOrUpdateMealsInMenu(MealsInMenu mim) { //Create the SQL Query for inserting an mim string createQuery = String.Format("Insert into MealsInMenues (MenuID, MealTypeID) Values({0}, {1});" + "Select @@Identity", mim.MenuID, mim.MealTypeID); string updateQuery = String.Format("Update MealsInMenues SET MenuID='{0}', MealTypeID={1} Where MealsInMenueID = {2};", mim.MenuID, mim.MealTypeID, mim.MealsInMenueID); //Create and open a connection to SQL Server SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sports_db"].ConnectionString); connection.Open(); //Create a Command object SqlCommand command = null; // new SqlCommand(createQuery, connection); if (mim.MealsInMenueID != 0) { command = new SqlCommand(updateQuery, connection); } else { command = new SqlCommand(createQuery, connection); } int savedMimID = 0; try { //Execute the command to SQL Server and return the newly created ID var commandResult = command.ExecuteScalar(); if (commandResult != null) { savedMimID = Convert.ToInt32(commandResult); } else { //the update SQL query will not return the primary key but if doesn't throw exception //then we will take it from the already provided data savedMimID = mim.MealsInMenueID; } } catch (Exception) { //there was a problem executing the script } //Close and dispose CloseAndDispose(command, connection); // Set return value return(savedMimID); }
public IActionResult Details(int id) { MealsInMenu meal = _MealsService.GetDetails(id); var card = SessionHelper.GetObjectAsJson <List <MealModelView> >(HttpContext.Session, "card"); if (card == null) { card = new List <MealModelView>(); card.Add(new MealModelView { ID = meal.ID, Price = meal.Price, Name = meal.Name, Logo = meal.Logo, Description = meal.Description, IsMeal = meal.IsMeal, //Cat =meal.Cat, QuantityNeeded = 1, TotalPrice = meal.Price }); SessionHelper.SetObjectAsJson(HttpContext.Session, "card", card); } else { int index = Exists(card, id); if (index == -1) { card.Add(new MealModelView { ID = meal.ID, Price = meal.Price, Name = meal.Name, Logo = meal.Logo, Description = meal.Description, IsMeal = meal.IsMeal, //Cat =meal.Cat, QuantityNeeded = 1, TotalPrice = meal.Price }); } else { card[index].QuantityNeeded++; card[index].TotalPrice = card[index].QuantityNeeded * card[index].Price; } SessionHelper.SetObjectAsJson(HttpContext.Session, "card", card); } int iid = meal.RestID; return(RedirectToAction("ShowMenu", new { id = iid })); }
public IActionResult AddMeal(MealViewModel Model, IFormFile file) { string UserId = _userManager.GetUserId(HttpContext.User); ApplicationUser User = context.Users.Where(u => u.Id == UserId).FirstOrDefault(); MealsInMenu Meal = new MealsInMenu(); //Meal.Cat = Model.Cat; Meal.Description = Model.Description; Meal.IsMeal = Model.IsMeal; Meal.Name = Model.Name; Meal.Price = Model.Price; if (!ModelState.IsValid) { return(View(Meal)); } try { string uniqueFileName = null; if (Model.Logo != null) { string uploadsFolder = Path.Combine(_appEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Model.Logo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); Model.Logo.CopyTo(new FileStream(filePath, FileMode.Create)); } if (User.RestID != null) { Meal.RestID = User.RestID.Value; } Meal.Logo = uniqueFileName; _MealsService.Add(Meal); return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); return(View(Meal)); } }
public List <MealsInMenu> GetMealsInMenues() { List <MealsInMenu> result = new List <MealsInMenu>(); //Create the SQL Query for returning all the mim string sqlQuery = String.Format("select * from MealsInMenues"); //Create and open a connection to SQL Server SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sports_db"].ConnectionString); connection.Open(); SqlCommand command = new SqlCommand(sqlQuery, connection); //Create DataReader for storing the returning table into server memory SqlDataReader dataReader = command.ExecuteReader(); MealsInMenu mim = null; //load into the result object the returned row from the database if (dataReader.HasRows) { while (dataReader.Read()) { mim = new MealsInMenu(); mim.MealTypeID = Convert.ToInt32(dataReader["MealTypeID"]); mim.MenuID = Convert.ToInt32(dataReader["MenuID"]); mim.MealsInMenueID = Convert.ToInt32(dataReader["MealsInMenueID"]); result.Add(mim); } } // Close and dispose CloseAndDispose(command, connection); return(result); }
public int InsertOrUpdateMealsInMenu(MealsInMenu value) { return(dal.InsertOrUpdateMealsInMenu(value)); }
// POST values public int Post([FromBody] MealsInMenu value) { return(bl.InsertOrUpdateMealsInMenu(value)); }