public ActionResult NotificationHome(int page) { if (Session["LibrarianName"] == null) { return(RedirectToAction("LibrarianLogin", "Login")); } var noticeListFromDb = _dbContext.NOTICE_STORE.OrderByDescending(t => t.CreateTime).ToList(); var noticeList = new List <Notification>(); int size = 10; int count = 0; if (TempData.Peek("SearchNoticeList") != null) { noticeList = (List <Notification>)TempData.Peek("SearchNoticeList"); } else { foreach (var item in noticeListFromDb) { Notification notification = new Notification() { ID = item.ID, Title = item.Title, Content = item.Content, CreateTime = item.CreateTime.ToString("HH:mm:ss dd/MM/yyyy"), TypeName = HomeDao.GetTypeNotice(item.TypeID), LibraryName = HomeDao.GetLibraryCode(item.LibID) }; noticeList.Add(notification); } } foreach (var item in noticeList) { count++; item.Count = count; } ViewBag.TypeNoticeList = _dbContext.TYPE_NOTICE.ToList(); ViewBag.LibraryList = _dbContext.HOLDING_LIBRARY.Where(t => t.Name != null).ToList(); ViewBag.FirstIndex = noticeList.ToPagedList(page, size).FirstItemOnPage; ViewBag.LastIndex = noticeList.ToPagedList(page, size).LastItemOnPage; ViewBag.Total = noticeList.ToPagedList(page, size).TotalItemCount; return(View(noticeList.ToPagedList(page, size))); }
public ActionResult CreateNotification(Notice data) { if (data.TypeID == 0 && data.LibID == 0) { TempData["ErrorMessage"] = "Vui lòng chọn mục đăng bài và thư viện!"; return(RedirectToAction("CreateNotification")); } if (data.TypeID == 0) { TempData["ErrorMessage"] = "Vui lòng chọn mục đăng bài!"; return(RedirectToAction("CreateNotification")); } if (data.LibID == 0) { TempData["ErrorMessage"] = "Vui lòng chọn thư viện!"; return(RedirectToAction("CreateNotification")); } if (string.IsNullOrWhiteSpace(data.Title)) { TempData["ErrorMessage"] = "Vui lòng nhập tiêu đề!"; return(RedirectToAction("CreateNotification")); } if (string.IsNullOrWhiteSpace(data.Content)) { TempData["ErrorMessage"] = "Vui lòng thêm nội dung bài viết!"; return(RedirectToAction("CreateNotification")); } var isExisted = _dbContext.NOTICE_STORE.ToList().Any(t => t.TypeID == data.TypeID); if (isExisted && data.TypeID == 3 || isExisted && data.TypeID == 4 || isExisted && data.TypeID == 5 || isExisted && data.TypeID == 6) { TempData["ErrorMessage"] = "Mục " + HomeDao.GetTypeNotice(data.TypeID) + " chỉ được đăng 1 bài viết!"; return(RedirectToAction("CreateNotification")); } if (ModelState.IsValid) { NOTICE_STORE obj = new NOTICE_STORE { Title = data.Title, Content = data.Content, CreateTime = DateTime.Now, TypeID = data.TypeID, LibID = data.LibID }; _dbContext.NOTICE_STORE.Add(obj); _dbContext.SaveChanges(); } TempData["SuccessfulMessage"] = "Đăng bài thành công!!"; return(RedirectToAction("NotificationHome", new { page = 1 })); }
public ActionResult SearchNotification(string searchByTitle, int searchByType, int searchByLibrary, string startDate, string endDate) { var noticeListFromDb = _dbContext.NOTICE_STORE.AsQueryable(); var theStartTime = ConvertToDate(startDate); var theEndTime = ConvertToDate(endDate); //Item1: year, Item2: month, Item3: day var startDateTime = new DateTime(theStartTime.Item1, theStartTime.Item2, theStartTime.Item3); var endDateTime = new DateTime(theEndTime.Item1, theEndTime.Item2, theEndTime.Item3); string title = Regex.Replace(searchByTitle, @"\s+", " ").Trim(); if (string.IsNullOrEmpty(title) && searchByLibrary == 0 && searchByType == 0 && CheckNullDateTime(startDateTime) && CheckNullDateTime(endDateTime)) { TempData["SearchNoticeList"] = null; } else { if (!string.IsNullOrEmpty(title)) { noticeListFromDb = noticeListFromDb.Where(t => t.Title.Contains(title) || t.Title.Equals(title)); } if (searchByType != 0) { noticeListFromDb = noticeListFromDb.Where(t => t.TypeID == searchByType); } if (searchByLibrary != 0) { noticeListFromDb = noticeListFromDb.Where(t => t.LibID == searchByLibrary); } if (!CheckNullDateTime(startDateTime) && !CheckNullDateTime(endDateTime)) { noticeListFromDb = noticeListFromDb.Where(t => t.CreateTime >= startDateTime && t.CreateTime <= endDateTime); } if (!CheckNullDateTime(startDateTime)) { noticeListFromDb = noticeListFromDb.Where(t => t.CreateTime >= startDateTime); } if (!CheckNullDateTime(endDateTime)) { noticeListFromDb = noticeListFromDb.Where(t => t.CreateTime <= endDateTime); } var noticeStoreList = noticeListFromDb.OrderByDescending(t => t.CreateTime).ToList(); var noticeList = (from t in noticeStoreList select new Notification() { ID = t.ID, Title = t.Title, Content = t.Content, CreateTime = t.CreateTime.ToString("HH:mm:ss dd/MM/yyyy"), TypeName = HomeDao.GetTypeNotice(t.TypeID), LibraryName = HomeDao.GetLibraryCode(t.LibID) }).ToList(); TempData["SearchNoticeList"] = noticeList; } return(RedirectToAction("NotificationHome", new { page = 1 })); }