using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Sleis.Service; using Sleis.Infrastructure; using Sleis.Models; using Sleis.ViewModels; using Sleis.Utility; using Sleis.Validation.Attribute; namespace Sleis.Controllers { [HandleError] public class NewsItemController : BaseController { public PublicNewsItemService NewsService { get; set; } public override void Init() { base.Init(); ArgumentValidationUtility.ThrowOnNull(NewsService, "NewsService"); } [HttpGet, Authorize] public ActionResult List() { return View(); } [Authorize, HttpGet] public JsonResult Get() { ListView model = new ListView(); model.List = NewsService.NewsData.GetAll().OrderByDescending(n => n.StartDate).ToList(); return Json(model, JsonRequestBehavior.AllowGet); } // // GET: /NewsItem/View/5 public ActionResult View(int id) { ArgumentValidationUtility.ThrowOnDefault(id, "id"); TempData[Constants.ReturnUrlKey] = HttpContext.Request.UrlReferrer; NewsView model = new NewsView(); model.HistoryNewsItems = NewsService.NewsData.GetActiveList(id); model.NewsItem = NewsService.NewsData.GetById(id); Log.DebugFormat("Model: {0}", model); return View(model); } // // GET: /NewsItem/Manage/5 // GET: /NewsItem/Manage/ [Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin)] public ActionResult Manage(int id = 0) { NewsItemModel model = null; if (id > 0) { model = NewsService.NewsData.GetById(id); } else { model = new NewsItemModel(); } Log.DebugFormat("Model: {0}", model); return View(model); } // // POST: /NewsItem/Manage/5 [HttpPost, Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin)] public ActionResult Manage(NewsItemModel item) { ActionResult resultView = View(); if (ModelState.IsValid) { if (item.Id > 0) { NewsService.NewsData.Update(item); } else { NewsService.NewsData.SaveOrUpdate(item); } TempData[Constants.GlobalMessageKey] = new SimpleMessage("The news item was successfully saved."); resultView = RedirectToAction("List", "NewsItem", new { id = item.Id }); } return resultView; } // // GET: /NewsItem/Delete/5 [Authorize,AppRoleValidation(AppUserRoleType.AgencyAdmin)] public ActionResult Delete(int id) { NewsService.NewsData.Delete(id); TempData[Constants.GlobalMessageKey] = new SimpleMessage("The news item was successfully deleted."); return RedirectToAction("List", "NewsItem"); } } }