//public ContentResult UploadFile(HttpPostedFileBase hpf,List<vm_FileUploadInfo> vm_Info) public JsonResult PublishPost(vmPublishPost input) //string Title, int CategoryID, string Content, string IsActive, bool FlagHaveFile,string Tagsinput) { //, string Labels try { string NewImageID; //InfoUser AppUser = new InfoUser(); var TodayDateShamsi = DateConvertor.DateToNumber(DateConvertor.TodayDate()); //var NewNewsCode = HelpOperation.NewsCode(Convert.ToInt32(TodayDateShamsi)); if (input.FlagHaveFile == true) { HelpOperation.CreateArchiveFolderOnTheServer(); HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase; var FileSize = HelpOperation.ToFileSize(hpf.ContentLength); var GuidID = HelpOperation.NewGuidID(); var FileNameOnServer = GuidID + Path.GetExtension(hpf.FileName); var FilePath = @"~\MediaFiles\Image\" + FileNameOnServer; var FilePathOnServer = Server.MapPath(FilePath); var FileUrl = HelpOperation.MapToUrl(FilePath); Request.Files[0].SaveAs(FilePathOnServer); using (var _ContextImage = new ApplicationDbContext()) { var _objEntityImage = new RepositoryPattern <Image>(_ContextImage); var NewItemImage = new Image { ID = GuidID, TitleUrl = input.Title, FileName = FileNameOnServer, FileSize = FileSize, FileUrl = FileUrl, FilePathOnServer = FilePath }; NewImageID = GuidID; _objEntityImage.Insert(NewItemImage); _objEntityImage.Save(); _objEntityImage.Dispose(); } using (var _ContextPost = new ApplicationDbContext()) { var objEntityPost = new RepositoryPattern <Post>(_ContextPost); var newItemPost = new Post { Title = input.Title.Trim(), ImageID = NewImageID, CategoryID = input.CategoryID, //Categories = new List<Category>() { new Category() {ID = CategoryID, } }, Content = input.Content, IsActive = input.IsActive == "true" ? "0" : "1", Labels = input.Tagsinput.Trim(), SeoMetaDescription = input.SeoMetaDescription.Trim(), PostDate = DateConvertor.DateToNumber(DateConvertor.TodayDate()), PostTime = DateConvertor.TimeNowShort() }; objEntityPost.Insert(newItemPost); objEntityPost.Save(); objEntityPost.Dispose(); } } return(Json("OK")); } catch (Exception) { return(Json("Faild")); } }
public JsonResult EditPost(vmPublishPost input) { try { //delete image of Post To Insert New Image For Post (Update) if (input.FlagHaveFile == true) { DatabaseOperation objDatabaseOperation = new DatabaseOperation(); using (var _Context1 = new ApplicationDbContext()) { var objEntityPost = new RepositoryPattern <Post>(_Context1); var CurrentItem = objEntityPost.GetByPredicate(x => x.ID == input.PostID); if (objDatabaseOperation.DeleteImageOfPost(CurrentItem.ImageID)) { //InfoUser AppUser = new InfoUser(); var TodayDateShamsi = DateConvertor.DateToNumber(DateConvertor.TodayDate()); //var NewNewsCode = HelpOperation.NewsCode(Convert.ToInt32(TodayDateShamsi)); HelpOperation.CreateArchiveFolderOnTheServer(); HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase; var FileSize = HelpOperation.ToFileSize(hpf.ContentLength); var GuidID = CurrentItem.ImageID; var FileNameOnServer = GuidID + Path.GetExtension(hpf.FileName); var FilePath = @"~\MediaFiles\Image\" + FileNameOnServer; var FilePathOnServer = Server.MapPath(FilePath); var FileUrl = HelpOperation.MapToUrl(FilePath); Request.Files[0].SaveAs(FilePathOnServer); using (var _ContextImage = new ApplicationDbContext()) { var _objEntityImage = new RepositoryPattern <Image>(_ContextImage); var NewItemImage = new Image { ID = GuidID, TitleUrl = input.Title, FileName = FileNameOnServer, FileSize = FileSize, FileUrl = FileUrl, FilePathOnServer = FilePath }; _objEntityImage.Insert(NewItemImage); _objEntityImage.Save(); _objEntityImage.Dispose(); } } } } using (var _context = new ApplicationDbContext()) { var objEntityPost = new RepositoryPattern <Post>(_context); var CurrentItem = objEntityPost.GetByPredicate(x => x.ID == input.PostID); CurrentItem.Title = input.Title.Trim(); CurrentItem.CategoryID = input.CategoryID; //Categories = new List<Category>() { new Category() {ID = CategoryID, } }, CurrentItem.Content = input.Content; CurrentItem.IsActive = input.IsActive == "true" ? "0" : "1"; CurrentItem.Labels = input.Tagsinput.Trim(); CurrentItem.SeoMetaDescription = input.SeoMetaDescription.Trim(); //CurrentItem.PostDate = DateConvertor.DateToNumber(DateConvertor.TodayDate()); //CurrentItem.PostTime = DateConvertor.TimeNowShort(); objEntityPost.Update(CurrentItem); objEntityPost.Save(); objEntityPost.Dispose(); } return(Json("OK")); } catch (Exception) { return(Json("Faild")); } }