using System; using System.Linq; using System.Net; using System.Web.Mvc; using EnVisage.Code; using EnVisage.Code.BLL; using EnVisage.Models; using EnVisage.Code.Validation; namespace EnVisage.Controllers { [Authorize] public class UserQuickLinkController : BaseController { #region Private private const string CannotSave = @"Cannot save link. Try again later."; private const string CannotDelete = @"Cannot delete link. Try again later."; #endregion // GET: /Type/Edit/5 [HttpGet] [AreaSecurity(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Edit(Guid? id) { try { var manager = new UserQuickLinkManager(DbContext); var model = manager.GetUserQuickLinkById(id) ?? new UserQuickLinkModel(); return PartialView("_editQuickLink", model); } catch (Exception exception) { LogException(exception); } return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } // POST: /Type/Edit/5 [HttpPost] [ValidateAjax] [ValidateAntiForgeryToken] [AreaSecurity(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Edit(UserQuickLinkModel model) { try { if (model == null) throw new ArgumentNullException("model"); model.TrimStringProperties(); var userQuickLink = model.Id != Guid.Empty ? DbContext.UserQuickLinks.FirstOrDefault(x => x.Id == model.Id) : new UserQuickLink(); model.CopyTo(userQuickLink); if (userQuickLink != null) { if (model.Id == Guid.Empty) { userQuickLink.Id = Guid.NewGuid(); DbContext.UserQuickLinks.Add(userQuickLink); } DbContext.SaveChanges(); var res = new { Id = userQuickLink.Id.ToString(), userQuickLink.Name, UserId = userQuickLink.UserId.ToString(), userQuickLink.Url, }; return new SuccessContentJsonResult(res); } } 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); ModelState.AddModelError(string.Empty, CannotSave); } } catch (Exception exception) // handle any unexpected error { LogException(exception); ModelState.AddModelError(string.Empty, CannotSave); } return new FailedJsonResult(ModelState); } [AreaSecurity(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Delete(Guid? id) { if (!id.HasValue || id == Guid.Empty) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); try { var manager = (new UserQuickLinkManager(DbContext)); var model = manager.GetUserQuickLinkById(id); if (model == null) return HttpNotFound(); return PartialView("_deleteQuickLink", model); } catch (Exception exception) { LogException(exception); } return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } // POST: /Type/Delete/5 [HttpPost] [ValidateAntiForgeryToken] [AreaSecurity(area = Areas.UserQuickLinks, level = AccessLevel.Write)] public ActionResult Delete(Guid id) { try { var dbObj = DbContext.UserQuickLinks.FirstOrDefault(x => x.Id == id); if (dbObj == null) throw new InvalidOperationException(string.Format("System cannot delete quick link {0} because it does not exist", id)); DbContext.UserQuickLinks.Remove(dbObj); DbContext.SaveChanges(); return new SuccessContentJsonResult(id); } catch (BLLException blEx) { if (blEx.DisplayError) ModelState.AddModelError(string.Empty, blEx.Message); else { LogException(blEx); ModelState.AddModelError(string.Empty, CannotDelete); } } catch (Exception exception) { LogException(exception); ModelState.AddModelError(string.Empty, CannotDelete); } return new FailedJsonResult(ModelState); } [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.Any()) prefData = prefRecords.First().PageState; if (prefData == null) prefData = ""; 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); } } }