GlobalController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using GreenTree.Nachtragsmanagement.Core.Authentication;
  2. using GreenTree.Nachtragsmanagement.Services.Appendix;
  3. using GreenTree.Nachtragsmanagement.Services.Deviation;
  4. using GreenTree.Nachtragsmanagement.Services.Site;
  5. using GreenTree.Nachtragsmanagement.Services.User;
  6. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  14. {
  15. public class GlobalController : Controller
  16. {
  17. private readonly IUserHelper _userHelper;
  18. private readonly IUserService _userService;
  19. private readonly IAppendixService _appendixService;
  20. private readonly IDeviationService _deviationService;
  21. private readonly ISiteService _siteService;
  22. public GlobalController(
  23. IUserHelper userHelper,
  24. IUserService userService,
  25. IAppendixService appendixService,
  26. IDeviationService deviationService,
  27. ISiteService siteService)
  28. {
  29. _userHelper = userHelper;
  30. _userService = userService;
  31. _appendixService = appendixService;
  32. _deviationService = deviationService;
  33. _siteService = siteService;
  34. }
  35. /// <summary>
  36. /// Represents the global Footer
  37. /// </summary>
  38. public ActionResult Footer()
  39. {
  40. var cookieUser = _userHelper.FromCookies();
  41. if (cookieUser == null)
  42. return View("~/Views/Shared/_Footer.cshtml", null);
  43. var dbUser = _userService.GetUserById(cookieUser.Id);
  44. if (dbUser == null)
  45. return View("~/Views/Shared/_Footer.cshtml", null);
  46. var footerModel = new FooterModel
  47. {
  48. CustomNumber = dbUser.CustomNumber,
  49. Forename = dbUser.Forename,
  50. Lastname = dbUser.Lastname,
  51. RoleDescription = dbUser.CurrentRole.Description
  52. };
  53. ViewData["Roles"] =
  54. dbUser.Roles
  55. .Select(r => new
  56. {
  57. r.Id,
  58. r.Description
  59. });
  60. return View("~/Views/Shared/_Footer.cshtml", footerModel);
  61. }
  62. /// <summary>
  63. /// Sets the current role of the logged in user and redirects to the home page
  64. /// </summary>
  65. /// <param name="roleId">The id of the new role.</param>
  66. public ActionResult SetRole(int roleId = -1)
  67. {
  68. if (roleId == -1)
  69. return RedirectToAction("Index", "Home");
  70. var user = _userHelper.FromCookies();
  71. var role = _userService.GetRoleById(roleId);
  72. user.CurrentRole = role;
  73. _userHelper.ToCookies(user);
  74. return RedirectToAction("Index", "Home");
  75. }
  76. /// <summary>
  77. /// Shows an unauthorized message
  78. /// </summary>
  79. public ActionResult NotAuthorized()
  80. {
  81. return View("~/Views/Global/NotAuthorized.cshtml");
  82. }
  83. /// <summary>
  84. /// Gets a full comment of a given entity
  85. /// </summary>
  86. /// <param name="entityType">The entity type.</param>
  87. /// <param name="id">The entity id.</param>
  88. public ActionResult GetEntityComment(string entityType, int id)
  89. {
  90. var result = new JsonResult
  91. {
  92. JsonRequestBehavior = JsonRequestBehavior.AllowGet,
  93. Data = String.Empty
  94. };
  95. if (String.IsNullOrEmpty(entityType))
  96. return result;
  97. switch (entityType)
  98. {
  99. case "appendix":
  100. var appendix = _appendixService.GetAppendixById(id);
  101. result.Data = appendix.Comment;
  102. break;
  103. case "deviation":
  104. var deviation = _deviationService.GetDeviationById(id);
  105. result.Data = deviation.Comment;
  106. break;
  107. case "site":
  108. var site = _siteService.GetSiteById(id);
  109. result.Data = site.Comment;
  110. break;
  111. default:
  112. return result;
  113. }
  114. return result;
  115. }
  116. }
  117. }