public async Task <JsonResult> Delete(int?id) { var returnUrl = "/Keywords"; if (id == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var keyword = await _unitOfWork.Keywords.Get(id.Value); if (keyword == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } _unitOfWork.Keywords.Remove(keyword); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "This keyword has been removed successfully!", returnUrl: returnUrl) , JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> Edit([Bind(Include = "Id, Name")] Keyword model) { var returnUrl = "/Keywords"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var keyword = await _unitOfWork.Keywords.Get(model.Id); if (keyword == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl) , JsonRequestBehavior.AllowGet)); } keyword.Name = model.Name; _unitOfWork.Keywords.Update(keyword); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "This keyword has been updated successfully!", returnUrl: returnUrl) , JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> Delete(int?id) { var returnUrl = "/Users"; if (id == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var user = await _unitOfWork.Users.Get(id.Value); if (user == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } _unitOfWork.Users.Remove(user); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "That user has been deleted successfully!", returnUrl: returnUrl) , JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> Edit( [Bind(Include = "Id, Title, Description, PublishedDate, Author")] Blog model) { var returnUrl = "/Blogs"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var blog = await _unitOfWork.Blogs.Get(model.Id); if (blog == null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.BadRequest, returnUrl))); } blog.Description = model.Description; blog.PublishedDate = model.PublishedDate; blog.Title = model.Title; blog.Author = model.Author; _unitOfWork.Blogs.Update(blog); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This blog has been updated successfully!", returnUrl: $"/Blogs/Detail/{blog.Id}"))); }
public async Task <JsonResult> Delete(int?id) { var returnUrl = "/Advertisements"; if (id == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var advertisement = await _unitOfWork.Advertisements.Get(id.Value); if (advertisement == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl) , JsonRequestBehavior.AllowGet)); } var filePath = Server.MapPath($@"~/{advertisement.ImageUrl}"); if (!System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } _unitOfWork.Advertisements.Remove(advertisement); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "That advertisement has been removed successfully!", returnUrl: returnUrl))); }
public async Task <JsonResult> DeleteDish(int?keywordId, int?dishId) { var returnUrl = "/Keywords"; if (keywordId == null || dishId == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var keyword = await _unitOfWork.Keywords.Get(keywordId.Value); var dish = await _unitOfWork.Dishes.GetDishWithKeywords(dishId.Value); if (keyword == null && dish == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } dish.Keywords.Remove(keyword); _unitOfWork.Dishes.Update(dish); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "That dish has been removed from this keyword successfully!") , JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> Edit([Bind(Include = "Id, Name, Icon")] Category model) { var returnUrl = "/Categories"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var category = await _unitOfWork.Categories.Get(model.Id); if (category == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl) , JsonRequestBehavior.AllowGet)); } category.Name = model.Name; category.Icon = model.Icon; _unitOfWork.Categories.Update(category); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "This category has been updated successfully!", returnUrl: $"/Categories/Detail/{category.Id}") , JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> Create([Bind(Include = "Title, Description, PublishedDate, Author")] Blog model) { if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } _unitOfWork.Blogs.Add(model); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This blog has been added successfully!", returnUrl: $"/Blogs/Detail/{model.Id}"))); }
public async Task <JsonResult> Edit( [Bind(Include = "Id, Name, Description, Price, CategoryId, RestaurantId, KeywordIds")] DishModel model) { var returnUrl = "/Dishes"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var categoryId = await _unitOfWork.Categories.SingleOrDefault(c => c.Id == model.CategoryId); var restaurantId = await _unitOfWork.Restaurants.SingleOrDefault(r => r.Id == model.RestaurantId); var dish = await _unitOfWork.Dishes.Get(model.Id); if (categoryId == null || restaurantId == null || dish == null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.BadRequest, returnUrl))); } dish.Keywords = new List <Keyword>(); if (model.KeywordIds.Any()) { foreach (var kw in model.KeywordIds) { var findKeyword = await _unitOfWork.Keywords.SingleOrDefault(k => kw == k.Id); dish.Keywords.Add(findKeyword); } } dish.CategoryId = model.CategoryId; dish.RestaurantId = model.RestaurantId; dish.Name = model.Name; dish.Price = model.Price; dish.Description = model.Description; _unitOfWork.Dishes.Update(dish); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This dish has been updated successfully!", returnUrl: $"/Dishes/Detail/{dish.Id}"))); }
public async Task <JsonResult> Create( [Bind(Include = "Name, Address, Description, PhoneNumber, OpenTime, CloseTime, IsHalal")] Restaurant model) { if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var imageUpload = Request.Files["FileUpload"]; if (imageUpload != null) { var validateFile = Utilities.HttpPostedFileBaseFilter(imageUpload); if (validateFile >= 0) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: Utilities.HttpPostedFileBaseErrorMessage(validateFile)))); } var originalDirectory = new DirectoryInfo(Server.MapPath("~/Content/Restaurants")); if (!Directory.Exists(originalDirectory.ToString())) { Directory.CreateDirectory(originalDirectory.ToString()); } try { var fileName = Guid.NewGuid() + ".jpg"; imageUpload.SaveAs(Server.MapPath("~/Content/Restaurants/" + fileName)); model.ImageUrl = "Content/Restaurants/" + fileName; } catch (Exception) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.ServerError, "/Restaurants/Create"))); } } model.CloseTime = new TimeSpan(model.CloseTime.Hours, model.CloseTime.Minutes, 0); model.OpenTime = new TimeSpan(model.OpenTime.Hours, model.OpenTime.Minutes, 0); _unitOfWork.Restaurants.Add(model); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This restaurant has been added successfully!", returnUrl: $"/Restaurants/Detail/{model.Id}"))); }
public async Task <JsonResult> Delete(int?id) { var returnUrl = "/Categories"; if (id == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var category = await _unitOfWork.Categories.GetCategoryWithDishes(id.Value); if (category == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl) , JsonRequestBehavior.AllowGet)); } foreach (var dish in category.Dishes) { foreach (var image in dish.Images) { var filePath = Path.Combine(Server.MapPath("~/"), image.Url); if (System.IO.File.Exists(filePath)) { Task.Run(() => { System.IO.File.Delete(filePath); }).Wait(); } } } _unitOfWork.Categories.Remove(category); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "That category has been deleted successfully!", returnUrl: returnUrl) , JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> Create ([Bind(Include = "Order, Title, SubTitle, ActionUrl, IsDisplay, Description")] Advertisement model) { var imageUpload = Request.Files["FileUpload"]; if (!ModelState.IsValid || imageUpload == null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var validateFile = Utilities.HttpPostedFileBaseFilter(imageUpload); if (validateFile >= 0) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: Utilities.HttpPostedFileBaseErrorMessage(validateFile)))); } var originalDirectory = new DirectoryInfo(Server.MapPath("~/Content/Advertisements")); if (!Directory.Exists(originalDirectory.ToString())) { Directory.CreateDirectory(originalDirectory.ToString()); } try { var fileName = Guid.NewGuid() + ".jpg"; imageUpload.SaveAs(Server.MapPath("~/Content/Advertisements/" + fileName)); model.ImageUrl = "Content/Advertisements/" + fileName; } catch (Exception) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.ServerError, "/Advertisements/Create"))); } _unitOfWork.Advertisements.Add(model); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This advertisement has been added successfully!", returnUrl: $"/Advertisements/Detail/{model.Id}"))); }
public async Task <JsonResult> Delete(int?id) { var returnUrl = "/Restaurants"; if (id == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var restaurant = await _unitOfWork.Restaurants.SingleOrDefault(r => r.Id == id, "Dishes"); if (restaurant == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl) , JsonRequestBehavior.AllowGet)); } foreach (var dish in restaurant.Dishes) { foreach (var image in dish.Images) { var filePath = Path.Combine(Server.MapPath("~/"), image.Url); if (System.IO.File.Exists(filePath)) { Task.Run(() => { System.IO.File.Delete(filePath); }).Wait(); } } } _unitOfWork.Restaurants.Remove(restaurant); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "That restaurant has been deleted successfully!", returnUrl: returnUrl) , JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> Create( [Bind(Include = "Name, Description, Price, CategoryId, RestaurantId, KeywordIds")] DishModel model) { var returnUrl = "/Dishes/Create"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var categoryId = await _unitOfWork.Categories.SingleOrDefault(c => c.Id == model.CategoryId); var restaurantId = await _unitOfWork.Restaurants.SingleOrDefault(r => r.Id == model.RestaurantId); if (categoryId == null || restaurantId == null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.BadRequest, returnUrl))); } var dish = Mapper.Map <DishModel, Dish>(model); if (model.KeywordIds.Any()) { foreach (var kw in model.KeywordIds) { var findKeyword = await _unitOfWork.Keywords.SingleOrDefault(k => kw == k.Id); dish.Keywords.Add(findKeyword); } } _unitOfWork.Dishes.Add(dish); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This dish has been added successfully! You will be moved to next page to add some images.", returnUrl: $"/Images/Create/{dish.Id}"))); }
public async Task <JsonResult> Delete(int?id) { var returnUrl = "/Images"; if (id == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var image = await _unitOfWork.Images.Get(id.Value); if (image == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, message: "That image is not found!", returnUrl: returnUrl) , JsonRequestBehavior.AllowGet)); } var filePath = Path.Combine(Server.MapPath("~/"), image.Url); _unitOfWork.Images.Remove(image); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "That image has been removed!", returnUrl: returnUrl))); }
public async Task <JsonResult> Delete(int?id) { var returnUrl = "/Dishes"; if (id == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var dish = await _unitOfWork.Dishes.GetDishWithImagesAndKeywords(id.Value); if (dish == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl) , JsonRequestBehavior.AllowGet)); } foreach (var item in dish.Images) { var filePath = Path.Combine(Server.MapPath("~/"), item.Url); if (System.IO.File.Exists(filePath)) { Task.Run(() => { System.IO.File.Delete(filePath); }).Wait(); } } _unitOfWork.Dishes.Remove(dish); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "That dish has been removed successfully!", returnUrl: returnUrl))); }
public async Task <JsonResult> Create([Bind(Include = "Name, Icon")] Category model) { if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } if (await _unitOfWork.Categories.SingleOrDefault(c => c.Name.ToLower() == model.Name.ToLower()) != null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "This category name is already exist, try again!"))); } _unitOfWork.Categories.Add(model); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "This category name has been added successfully!", returnUrl: $"/Categories/Detail/{model.Id}"))); }
public async Task <JsonResult> Create([Bind(Include = "Email, Password, Name, PhoneNumber, GroupId")] User model) { var returnUrl = "/Users"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } if (await _unitOfWork.Users.SingleOrDefault(u => u.Email == model.Email) != null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "This email is already exist! Choose another one"))); } model.Password = Utilities.EncryptStringToMd5(model.Email + model.Password); _unitOfWork.Users.Add(model); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.Success, message: "This user has been added successfully!", returnUrl: returnUrl))); }
public async Task <JsonResult> Create( [Bind(Include = "DishId, Name, IsDisplay")] Image model) { var returnUrl = "/Images"; var filesKey = Request.Files.AllKeys; if (!ModelState.IsValid || !filesKey.Any()) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: @"Please enter all fields are required!"), JsonRequestBehavior.AllowGet)); } var dish = await _unitOfWork.Dishes.Get(model.DishId); if (dish == null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.BadRequest, "/Images/Create"), JsonRequestBehavior.AllowGet)); } var httpPostedFileBases = new HttpPostedFileBase[filesKey.Length]; for (var i = 0; i < filesKey.Length; i++) { var imageUpload = Request.Files[filesKey[i]]; var validateFile = Utilities.HttpPostedFileBaseFilter(imageUpload); if (validateFile >= 0) { var error = Utilities.HttpPostedFileBaseErrorMessage(validateFile); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: error), JsonRequestBehavior.AllowGet)); } httpPostedFileBases[i] = Request.Files[filesKey[i]]; } var originalDirectory = new DirectoryInfo(Server.MapPath("~/Content/Dishes")); var directoryPath = Path.Combine(originalDirectory.ToString(), dish.Name); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } foreach (var imageUpload in httpPostedFileBases) { try { var fileName = Guid.NewGuid() + ".jpg"; imageUpload.SaveAs(Path.Combine(directoryPath, fileName)); var image = new Image { DishId = model.DishId, Name = model.Name, IsDisplay = model.IsDisplay, Url = $"Content/Dishes/{dish.Name}/{fileName}" }; _unitOfWork.Images.Add(image); } catch (Exception) { foreach (var image in httpPostedFileBases) { var filePath = Path.Combine(directoryPath, image.FileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.ServerError, "/Images/Create"), JsonRequestBehavior.AllowGet)); } } await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: @"These images added to this dish successfully!", returnUrl: returnUrl), JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> Edit( [Bind(Include = "Id, Order, Title, SubTitle, ActionUrl, IsDisplay, Description")] Advertisement model) { var returnUrl = "/Advertisements"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var advertisement = await _unitOfWork.Advertisements.Get(model.Id); if (advertisement == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var imageUpload = Request.Files["FileUpload"]; if (imageUpload != null) { var validateFile = Utilities.HttpPostedFileBaseFilter(imageUpload); if (validateFile >= 0) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: Utilities.HttpPostedFileBaseErrorMessage(validateFile)))); } try { var fileName = Guid.NewGuid() + ".jpg"; imageUpload.SaveAs(Server.MapPath("~/Content/Advertisements/" + fileName)); if (advertisement.ImageUrl != null) { var oldFilePath = Server.MapPath($@"~/{advertisement.ImageUrl}"); if (System.IO.File.Exists(oldFilePath)) { Task.Run(() => { System.IO.File.Delete(oldFilePath); }).Wait(); } } advertisement.ImageUrl = "Content/Advertisements/" + fileName; } catch (Exception) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.ServerError, $"/Advertisements/Edit/{advertisement.Id}"))); } } advertisement.ActionUrl = model.ActionUrl; advertisement.IsDisplay = model.IsDisplay; advertisement.Order = model.Order; advertisement.SubTitle = model.SubTitle; advertisement.Title = model.Title; advertisement.Description = model.Description; _unitOfWork.Advertisements.Update(advertisement); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This advertisement has been updated successfully!", returnUrl: $"/Advertisements/Detail/{advertisement.Id}"))); }
public async Task <JsonResult> Edit( [Bind(Include = "Id, Name, Address, Description, PhoneNumber, OpenTime, CloseTime, IsHalal")] Restaurant model) { var returnUrl = "/Restaurants"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: "Please enter all fields are required!"))); } var restaurant = await _unitOfWork.Restaurants.Get(model.Id); if (restaurant == null) { return(Json(MessageAlertCenter.GetMessageAlert( MessageAlertType.BadRequest, returnUrl) , JsonRequestBehavior.AllowGet)); } var imageUpload = Request.Files["FileUpload"]; if (imageUpload != null) { var validateFile = Utilities.HttpPostedFileBaseFilter(imageUpload); if (validateFile >= 0) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: Utilities.HttpPostedFileBaseErrorMessage(validateFile)))); } try { var fileName = Guid.NewGuid() + ".jpg"; imageUpload.SaveAs(Server.MapPath("~/Content/Restaurants/" + fileName)); if (restaurant.ImageUrl != null) { var oldFilePath = Server.MapPath($@"~/{restaurant.ImageUrl}"); if (System.IO.File.Exists(oldFilePath)) { Task.Run(() => { System.IO.File.Delete(oldFilePath); }).Wait(); } } restaurant.ImageUrl = "Content/Restaurants/" + fileName; } catch (Exception) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.ServerError, $"/Restaurants/Edit/{restaurant.Id}"))); } } restaurant.Name = model.Name; restaurant.Description = model.Description; restaurant.CloseTime = new TimeSpan(model.CloseTime.Hours, model.CloseTime.Minutes, 0); restaurant.OpenTime = new TimeSpan(model.OpenTime.Hours, model.OpenTime.Minutes, 0); restaurant.Address = model.Address; restaurant.IsHalal = model.IsHalal; restaurant.PhoneNumber = model.PhoneNumber; _unitOfWork.Restaurants.Update(restaurant); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: "This restaurant has been updated successfully!", returnUrl: $"/Restaurants/Detail/{restaurant.Id}"))); }
public async Task <JsonResult> Edit([Bind(Include = "Id, Name, IsDisplay, DishId")] Image model) { var returnUrl = "/Images"; if (!ModelState.IsValid) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: @"Please enter all fields are required!"), JsonRequestBehavior.AllowGet)); } var image = await _unitOfWork.Images.Get(model.Id); var dish = await _unitOfWork.Dishes.Get(model.DishId); if (dish == null || image == null) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.BadRequest, returnUrl), JsonRequestBehavior.AllowGet)); } var imageUpload = Request.Files["FileEdit"]; if (imageUpload != null) { var validateFile = Utilities.HttpPostedFileBaseFilter(imageUpload); if (validateFile >= 0) { var error = Utilities.HttpPostedFileBaseErrorMessage(validateFile); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Invalid, message: error), JsonRequestBehavior.AllowGet)); } var originalDirectory = new DirectoryInfo(Server.MapPath("~/Content/Dishes")); var directoryPath = Path.Combine(originalDirectory.ToString(), dish.Name); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } try { var fileName = Guid.NewGuid() + ".jpg"; imageUpload.SaveAs(Path.Combine(directoryPath, fileName)); var oldFilePath = Path.Combine(Server.MapPath("~/"), image.Url); if (System.IO.File.Exists(oldFilePath)) { Task.Run(() => { System.IO.File.Delete(oldFilePath); }).Wait(); } image.Url = $"Content/Dishes/{dish.Name}/{fileName}"; } catch (Exception) { return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.ServerError, $"/Restaurants/Edit/{image.Id}"), JsonRequestBehavior.AllowGet)); } } image.Name = model.Name; image.DishId = model.DishId; image.IsDisplay = model.IsDisplay; _unitOfWork.Images.Update(image); await _unitOfWork.Completed(); return(Json(MessageAlertCenter.GetMessageAlert(MessageAlertType.Success, message: @"This image has been updated successfully", returnUrl: returnUrl), JsonRequestBehavior.AllowGet)); }