GlobalController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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)
  67. {
  68. var user = _userHelper.FromCookies();
  69. var role = _userService.GetRoleById(roleId);
  70. user.CurrentRole = role;
  71. _userHelper.ToCookies(user);
  72. return RedirectToAction("Index", "Home");
  73. }
  74. /// <summary>
  75. /// Shows an unauthorized message
  76. /// </summary>
  77. public ActionResult NotAuthorized()
  78. {
  79. return View("~/Views/Global/NotAuthorized.cshtml");
  80. }
  81. /// <summary>
  82. /// Gets a full comment of a given entity
  83. /// </summary>
  84. /// <param name="entityType">The entity type.</param>
  85. /// <param name="id">The entity id.</param>
  86. public ActionResult GetEntityComment(string entityType, int id)
  87. {
  88. var result = new JsonResult
  89. {
  90. JsonRequestBehavior = JsonRequestBehavior.AllowGet,
  91. Data = String.Empty
  92. };
  93. if (String.IsNullOrEmpty(entityType))
  94. return result;
  95. switch (entityType)
  96. {
  97. case "appendix":
  98. var appendix = _appendixService.GetAppendixById(id);
  99. result.Data = appendix.Comment;
  100. break;
  101. case "deviation":
  102. var deviation = _deviationService.GetDeviationById(id);
  103. result.Data = deviation.Comment;
  104. break;
  105. case "site":
  106. var site = _siteService.GetSiteById(id);
  107. result.Data = site.Comment;
  108. break;
  109. default:
  110. return result;
  111. }
  112. return result;
  113. }
  114. }
  115. }