using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using EnVisage; using EnVisage.App_Start; using EnVisage.Code; using EnVisage.Code.BLL; using EnVisage.Code.HtmlHelpers; using EnVisage.Models; using Microsoft.AspNet.Identity; using jQuery.DataTables.Mvc; using System.Collections.ObjectModel; namespace EnVisage.Controllers { [Authorize] public class UserQuickLinkController : BaseController { /// /// GET: /Types/ /// /// Empty view // GET: /Type/Edit/5 [HttpGet] [AreaSecurityAttribute(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Edit(Guid? id) { var model = new UserQuickLinkModel(); try { if (id != null && id != Guid.Empty) { var dbUQL = DbContext.UserQuickLinks.AsNoTracking().Where(x => x.Id == id).FirstOrDefault(); model = (UserQuickLinkModel)dbUQL; } } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return PartialView("_editQuickLink", model); } // POST: /Type/Edit/5 [HttpPost] [ValidateAntiForgeryToken] [AreaSecurityAttribute(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Edit(UserQuickLinkModel model) { model.TrimStringProperties(); if (ModelState.IsValid) { try { UserQuickLink userQuickLink; if (model.Id == Guid.Empty) userQuickLink = new UserQuickLink(); else userQuickLink = DbContext.UserQuickLinks.Where(x => x.Id == model.Id).FirstOrDefault(); model.CopyTo(userQuickLink); if (model.Id == Guid.Empty) { userQuickLink.Id = Guid.NewGuid(); DbContext.UserQuickLinks.Add(userQuickLink); } DbContext.SaveChanges(); var res = new { Status = "OK", Id = userQuickLink.Id.ToString(), Name = userQuickLink.Name, UserId = userQuickLink.UserId.ToString(), Url = userQuickLink.Url, Data = "" }; return Json(res, JsonRequestBehavior.AllowGet); } catch (BLLException blEx) // handle any system specific error { // display error message if required if (blEx.DisplayError) ModelState.AddModelError(string.Empty, blEx.Message); else // if display not requried then display modal form with general error message { LogException(blEx); SetErrorScript(); } } catch (Exception exception) // handle any unexpected error { LogException(exception); SetErrorScript(); } } // return empty model with validation messages (if any) var result = new { Status = "OK", Data = "" }; return Json(result, JsonRequestBehavior.AllowGet); } [AreaSecurityAttribute(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Delete(Guid? id) { UserQuickLinkModel model = null; if (id.HasValue && id != Guid.Empty) { var dbUQL = DbContext.UserQuickLinks.AsNoTracking().Where(x => x.Id == id).FirstOrDefault(); model = (UserQuickLinkModel)dbUQL; } else model = new UserQuickLinkModel(); return PartialView("_deleteQuickLink", model); } // POST: /Type/Delete/5 [HttpPost] [ValidateAntiForgeryToken] [AreaSecurityAttribute(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Delete(UserQuickLinkModel model) { try { var dbObj = DbContext.UserQuickLinks.Where(x => x.Id == model.Id).FirstOrDefault(); if (dbObj == null) return HttpNotFound(); else { DbContext.UserQuickLinks.Remove(dbObj); DbContext.SaveChanges(); } } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } var result = new { Status = "OK", Data = "" }; return Json(result, JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult GetPreferences(Guid linkId) { string userIdAsText = User.Identity.GetID(); try { // Validate user var user = DbContext.AspNetUsers.FirstOrDefault(t => t.Id == userIdAsText); Guid userId = new Guid(userIdAsText); if (user == null) throw new Exception(String.Format("Unknown user id ({0})", userIdAsText)); string prefData = String.Empty; var prefRecords = DbContext.UserQuickLinks.Where(x => x.UserId.Equals(userId) && x.Id.Equals(linkId)); if (prefRecords.Count() > 0) prefData = prefRecords.First().PageState; var pagePreferences = new { Status = "OK", Data = prefData }; return Json(pagePreferences, JsonRequestBehavior.AllowGet); } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } } }