//Add new article----------------------------------------------------------------------------------------------------- public JsonResult addNewArticle(Article article) { DhofarDb db = new DhofarDb(); artOrder artorder = new artOrder(); string msg = string.Empty; if (article != null) { //article.post_adate = DateTime.Parse(DateTime.Now.ToShortTimeString()); db.Articles.Add(article); db.SaveChanges(); // return Json(new { status = "تمت عملية الإضافة بنجاح" }, JsonRequestBehavior.AllowGet); msg = "تمت عملية الإضافة بنجاح"; //get the current added article, by getting the max articleid, then using that id to get the whole article // int maxArtId = db.Articles.Max(x => x.ArticleId); //int artid = article.ArticleId; //var maxOrder = db.Articles.Where(x=>x.Location == article.Location).Max(x => x.order ); //Article getArt = db.Articles.Where(x => x.ArticleId == maxArtId).FirstOrDefault(); //change the articles order based on the current article order // artorder.getOrder(getArt.ArticleId, getArt.order, getArt.Location, maxOrder); return(Json(msg, JsonRequestBehavior.AllowGet)); //return "تمت عملية الإضافة"; } else { msg = "حصل خطاء أثناء عملية الإضافة"; return(Json(msg, JsonRequestBehavior.AllowGet)); // return "حصل خطاء أثنائء عملية الإضافة"; } }
//Adding post data public JsonResult addNewPost(Post post) { string msg = string.Empty; if (post != null) { using (DhofarDb Obj = new DhofarDb()) { post.post_adate = DateTime.Parse(DateTime.Now.ToShortTimeString()); Obj.Posts.Add(post); Obj.SaveChanges(); // return Json(new { status = "تمت عملية الإضافة بنجاح" }, JsonRequestBehavior.AllowGet); msg = "تمت عملية الإضافة بنجاح"; return(Json(msg, JsonRequestBehavior.AllowGet)); //return "تمت عملية الإضافة"; } } else { msg = "حصل خطاء أثناء عملية الإضافة"; return(Json(msg, JsonRequestBehavior.AllowGet)); // return "حصل خطاء أثنائء عملية الإضافة"; } }
// Get Article by Id public JsonResult getArticleById(string id) { int aid = Convert.ToInt32(id); using (DhofarDb db = new DhofarDb()) { var art = (from a in db.Articles join b in db.Posts on a.PostId equals b.PostId where a.ArticleId == aid select new { a.ArticleId, a.Location, a.PostId, a.order, b.post_title, b.post_status, b.post_adate, b.post_img }).FirstOrDefault(); //Article art = db.Articles.Where(x => x.ArticleId == aid).FirstOrDefault(); return(Json(art, JsonRequestBehavior.AllowGet)); } }
//Delete Images public JsonResult delImgs(string id) { int imgId = Convert.ToInt32(id); string msg = string.Empty; using (DhofarDb db = new DhofarDb()) { UploadedFile up = db.UploadedFiles.Where(x => x.FileId == imgId).FirstOrDefault(); string file_name = up.FileName; db.UploadedFiles.Remove(up); db.SaveChanges(); //delete image from uploadfiles folder string path = Server.MapPath("~/UploadedFiles/" + file_name); FileInfo file = new FileInfo(path); if (file.Exists)//check file exsit or not { file.Delete(); } else { } msg = " تم حذف الصورة بنجاح"; return(Json(msg, JsonRequestBehavior.AllowGet)); } }
//Get Post By Id public JsonResult getPostById(string id) { int pid = Convert.ToInt32(id); DhofarDb db = new DhofarDb(); Post pst = db.Posts.Where(x => x.PostId == pid).FirstOrDefault(); return(Json(pst, JsonRequestBehavior.AllowGet)); }
public JsonResult getPostsLst() { DhofarDb db = new DhofarDb(); List <Post> lst = db.Posts.OrderByDescending(x => x.PostId).ToList(); int totalPosts = lst.Count(); return(Json(new { pstLst = lst, totalPst = totalPosts }, JsonRequestBehavior.AllowGet)); }
//Get all posts, which will be used inside the articles-------------------------------------------------------------- public JsonResult getAllPosts() { using (DhofarDb db = new DhofarDb()) { List <Post> lstPst = db.Posts.Select(x => x).ToList(); return(Json(lstPst, JsonRequestBehavior.AllowGet)); } }
//get all images public JsonResult getAllImg() { using (DhofarDb db = new DhofarDb()) { List <UploadedFile> uf = db.UploadedFiles.Select(x => x).ToList(); return(Json(uf, JsonRequestBehavior.AllowGet)); } }
//Delete Posts public JsonResult delPost(string id) { DhofarDb db = new DhofarDb(); int getId = Convert.ToInt32(id); Post pst = db.Posts.Where(p => p.PostId == getId).FirstOrDefault(); db.Posts.Remove(pst); db.SaveChanges(); string msg = "تم حذف المقال بنجاح"; return(Json(msg, JsonRequestBehavior.AllowGet)); }
public ActionResult SaveUploadedFile() { bool isSavedSuccessfully = true; string msg = string.Empty; string fName = ""; try { foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; fName = file.FileName; if (file != null && file.ContentLength > 0) { int size = file.ContentLength; var path = Path.Combine(Server.MapPath("~/UploadedFiles")); string pathString = System.IO.Path.Combine(path.ToString()); //var fileName1 = Path.GetFileName(file.FileName); var fileName1 = Guid.NewGuid() + Path.GetExtension(file.FileName); bool isExists = System.IO.Directory.Exists(pathString); if (!isExists) { System.IO.Directory.CreateDirectory(pathString); } var uploadpath = string.Format("{0}\\{1}", pathString, fileName1); file.SaveAs(uploadpath); using (DhofarDb db = new DhofarDb()) { UploadedFile uf = new UploadedFile(); uf.FileName = fileName1; uf.FileSize = size; db.UploadedFiles.Add(uf); db.SaveChanges(); } } } } catch (Exception ex) { isSavedSuccessfully = false; } if (isSavedSuccessfully) { msg = "تم رفعل الصورة بنجاح"; return(Json(msg, JsonRequestBehavior.AllowGet)); } else { msg = "حصل خطاء أثناء رفع الصورة"; return(Json(msg, JsonRequestBehavior.AllowGet)); } }
// Delete article public JsonResult delArticle(string id) { string msg = string.Empty; int aid = Convert.ToInt32(id); using (DhofarDb db = new DhofarDb()) { Article art = db.Articles.Where(x => x.ArticleId == aid).FirstOrDefault(); db.Articles.Remove(art); db.SaveChanges(); msg = "لقد تم حذف المفالة بنجاح"; return(Json(msg, JsonRequestBehavior.AllowGet)); } }
public JsonResult SaveFiles(string description) { string Message, fileName, actualFileName; Message = fileName = actualFileName = string.Empty; bool flag = false; if (Request.Files != null) { var file = Request.Files[0]; actualFileName = file.FileName; fileName = Guid.NewGuid() + Path.GetExtension(file.FileName); int size = file.ContentLength; try { file.SaveAs(Path.Combine(Server.MapPath("~/UploadedFiles"), fileName)); UploadedFile f = new UploadedFile { FileName = actualFileName, FilePath = fileName, Description = description, FileSize = size }; using (DhofarDb dc = new DhofarDb()) { dc.UploadedFiles.Add(f); dc.SaveChanges(); Message = "File uploaded successfully"; flag = true; } } catch (Exception) { Message = "File upload failed! Please try again"; } } return(new JsonResult { Data = new { Message = Message, Status = flag } }); }
//getting all the articles in the news category to be displyied inside the homepage public JsonResult allNewsForHome() { DhofarDb db = new DhofarDb(); var art = (from a in db.Articles join b in db.Posts on a.PostId equals b.PostId where a.Location == "قائمة الأخبار" select new { a.ArticleId, a.Location, a.PostId, a.order, b.post_title, b.post_status, b.post_adate, b.post_img }).ToList(); //Article art = db.Articles.Where(x => x.ArticleId == aid).FirstOrDefault(); return(Json(art, JsonRequestBehavior.AllowGet)); }
//get all the articles----------------------------------------------------------------------------------------------- public JsonResult getAllArtciles() { //here we are going to merge two tables articles and posts then pass the result to a view model vmartposts using (DhofarDb db = new DhofarDb()) { var getlst = db.Articles. Join(db.Posts, a => a.PostId, b => b.PostId, (a, b) => new VMArtPost { ArticleId = a.ArticleId, Location = a.Location, PostId = a.PostId, order = a.order, post_title = b.post_title, post_status = b.post_status, post_adate = b.post_adate, post_img = b.post_img }).ToList(); return(Json(getlst.OrderByDescending(x => x.ArticleId), JsonRequestBehavior.AllowGet)); } }
//edit article ----------------------------------------------------------------------------------------------------------- public JsonResult editArticle(Article article) { DhofarDb db = new DhofarDb(); artOrder artorder = new artOrder(); string msg = string.Empty; if (article != null) { int aid = Convert.ToInt32(article.ArticleId); Article art = db.Articles.Where(x => x.ArticleId == aid).FirstOrDefault(); art.Location = article.Location; art.Status = article.Status; art.SDate = article.SDate; art.EDate = article.EDate; art.PostId = article.PostId; art.order = article.order; //article.post_adate = DateTime.Parse(DateTime.Now.ToShortTimeString()); // db.Articles.Add(art); db.SaveChanges(); // return Json(new { status = "تمت عملية الإضافة بنجاح" }, JsonRequestBehavior.AllowGet); msg = "تمت عملية الإضافة بنجاح"; //get the current updated article // Article getArt = db.Articles.Where(x => x.ArticleId == aid).FirstOrDefault(); //var maxOrder = db.Articles.Where(x => x.Location == article.Location).Max(x => x.order); //change the articles order based on the current article order // artorder.getOrder(getArt.ArticleId, getArt.order, getArt.Location, maxOrder); return(Json(msg, JsonRequestBehavior.AllowGet)); } else { msg = "حصل خطاء أثناء عملية الإضافة"; return(Json(msg, JsonRequestBehavior.AllowGet)); } }
//Update post public JsonResult updatePost(Post post) { if (post != null) { using (DhofarDb db = new DhofarDb()) { int no = Convert.ToInt32(post.PostId); var postList = db.Posts.Where(x => x.PostId == no).FirstOrDefault(); postList.post_title = post.post_title; postList.post_data = post.post_data; postList.post_img = post.post_img; postList.post_img_title = post.post_img_title; postList.post_status = post.post_status; db.SaveChanges(); string msg = "حتمت عملية الإضافة"; return(Json(msg, JsonRequestBehavior.AllowGet)); } } else { string msg = "حدثت مشكلة في تعديل البيانات"; return(Json(msg, JsonRequestBehavior.AllowGet)); } }