Example #1
0
        public JsonResult AddFoodItem(HttpPostedFileBase file, string providerPhone, string price, string name, string description)
        {
            try
            {
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(price))
                    return Json(new { Error = "Invalid input", status = 405 }, JsonRequestBehavior.AllowGet);

                int foodPrice;
                if (!int.TryParse(price, out foodPrice))
                    return Json(new { Error = "Invalid price", status = 405 }, JsonRequestBehavior.AllowGet);

                var foodProvider = (FoodProviderModel)Session["user"];

                var menuModel = new FoodModel()
                {
                    ProviderPhone = foodProvider.Id,
                    Price = foodPrice,
                    Description = description,
                    Image = AddImage(file),
                    Name = name
                };

                IHomeFoodRepository repo = new HomeFoodRepository();
                repo.AddFoodItem(menuModel);

                return Json(new { Message = "Food item added successfully", status = 200 }, JsonRequestBehavior.AllowGet);

            }

            catch (Exception ex)
            {
                return Json(new { Error = ex.Message, status = 400 }, JsonRequestBehavior.AllowGet);
            }
        }