GlobalController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using GreenTree.Nachtragsmanagement.Core.Authentication;
  2. using GreenTree.Nachtragsmanagement.Services.Appendix;
  3. using GreenTree.Nachtragsmanagement.Services.Deviation;
  4. using GreenTree.Nachtragsmanagement.Services.Logging;
  5. using GreenTree.Nachtragsmanagement.Services.Site;
  6. using GreenTree.Nachtragsmanagement.Services.User;
  7. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  8. using Newtonsoft.Json;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using System.Web.Mvc;
  14. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  15. {
  16. public class GlobalController : Controller
  17. {
  18. private readonly IUserHelper _userHelper;
  19. private readonly IUserService _userService;
  20. private readonly IAppendixService _appendixService;
  21. private readonly IDeviationService _deviationService;
  22. private readonly ISiteService _siteService;
  23. private readonly ILogger _logger;
  24. public GlobalController(
  25. IUserHelper userHelper,
  26. IUserService userService,
  27. IAppendixService appendixService,
  28. IDeviationService deviationService,
  29. ISiteService siteService,
  30. ILogger logger)
  31. {
  32. _userHelper = userHelper;
  33. _userService = userService;
  34. _appendixService = appendixService;
  35. _deviationService = deviationService;
  36. _siteService = siteService;
  37. _logger = logger;
  38. }
  39. /// <summary>
  40. /// Represents the global Footer
  41. /// </summary>
  42. public ActionResult Footer()
  43. {
  44. var cookieUser = _userHelper.FromCookies();
  45. if (cookieUser == null)
  46. return View("~/Views/Shared/_Footer.cshtml", null);
  47. var dbUser = _userService.GetUserById(cookieUser.Id);
  48. if (dbUser == null)
  49. return View("~/Views/Shared/_Footer.cshtml", null);
  50. var footerModel = new FooterModel
  51. {
  52. CustomNumber = dbUser.CustomNumber,
  53. Forename = dbUser.Forename,
  54. Lastname = dbUser.Lastname,
  55. RoleDescription = dbUser.CurrentRole.Description
  56. };
  57. ViewData["Roles"] =
  58. dbUser.Roles
  59. .Select(r => new
  60. {
  61. r.Id,
  62. r.Description
  63. });
  64. return View("~/Views/Shared/_Footer.cshtml", footerModel);
  65. }
  66. /// <summary>
  67. /// Sets the current role of the logged in user and redirects to the home page
  68. /// </summary>
  69. /// <param name="roleId">The id of the new role.</param>
  70. public ActionResult SetRole(int roleId = -1)
  71. {
  72. try
  73. {
  74. if (roleId == -1)
  75. return RedirectToAction("Index", "Home");
  76. var user = _userHelper.FromCookies();
  77. var role = _userService.GetRoleById(roleId);
  78. user.CurrentRole = role;
  79. _userHelper.ToCookies(user);
  80. }
  81. catch (Exception ex)
  82. {
  83. _logger.Error("Fehler bei Wechsel einer Rolle.", ex, _userHelper.FromCookies());
  84. }
  85. return RedirectToAction("Index", "Home");
  86. }
  87. /// <summary>
  88. /// Shows an unauthorized message
  89. /// </summary>
  90. public ActionResult NotAuthorized()
  91. {
  92. return View("~/Views/Global/NotAuthorized.cshtml");
  93. }
  94. #region Comments
  95. /// <summary>
  96. /// Gets a full comment of a given entity
  97. /// </summary>
  98. /// <param name="entityType">The entity type.</param>
  99. /// <param name="id">The entity id.</param>
  100. public ActionResult GetEntityComment(string entityType, int id)
  101. {
  102. var result = new JsonResult
  103. {
  104. JsonRequestBehavior = JsonRequestBehavior.AllowGet,
  105. Data = String.Empty
  106. };
  107. if (String.IsNullOrEmpty(entityType))
  108. return result;
  109. switch (entityType)
  110. {
  111. case "appendix":
  112. var appendix = _appendixService.GetAppendixById(id);
  113. result.Data = appendix.Comment;
  114. break;
  115. case "deviation":
  116. var deviation = _deviationService.GetDeviationById(id);
  117. result.Data = deviation.Comment;
  118. break;
  119. case "site":
  120. var site = _siteService.GetSiteById(id);
  121. result.Data = site.Comment;
  122. break;
  123. default:
  124. return result;
  125. }
  126. return result;
  127. }
  128. /// <summary>
  129. /// Gets a full comment of a given entity and provides an edit form
  130. /// </summary>
  131. /// <param name="entityType">The entity type.</param>
  132. /// <param name="id">The entity id.</param>
  133. public ActionResult EditEntityComment(string entityType, int id)
  134. {
  135. var editCommentModel = new EditEntityCommentModel
  136. {
  137. EntityType = entityType,
  138. EntityId = id
  139. };
  140. switch (entityType)
  141. {
  142. case "appendix":
  143. var appendix = _appendixService.GetAppendixById(id);
  144. editCommentModel.Comment = appendix.Comment;
  145. break;
  146. case "deviation":
  147. var deviation = _deviationService.GetDeviationById(id);
  148. editCommentModel.Comment = deviation.Comment;
  149. break;
  150. case "site":
  151. var site = _siteService.GetSiteById(id);
  152. editCommentModel.Comment = site.Comment;
  153. break;
  154. default:
  155. return new EmptyResult();
  156. }
  157. return PartialView("~/Views/Shared/_EditCommentPartial.cshtml", editCommentModel);
  158. }
  159. /// <summary>
  160. /// Sets a comment for a given entity
  161. /// </summary>
  162. /// <param name="entityType">The entity type.</param>
  163. /// <param name="id">The entity id.</param>
  164. [HttpPost, ValidateInput(false)]
  165. public ActionResult EditEntityComment(EditEntityCommentModel model)
  166. {
  167. if (model == null)
  168. return new EmptyResult();
  169. switch (model.EntityType)
  170. {
  171. case "appendix":
  172. var appendix = _appendixService.GetAppendixById(model.EntityId);
  173. appendix.Comment = model.Comment;
  174. _appendixService.UpdateAppendix(appendix);
  175. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  176. break;
  177. case "deviation":
  178. var deviation = _deviationService.GetDeviationById(model.EntityId);
  179. deviation.Comment = model.Comment;
  180. _deviationService.UpdateDeviation(deviation);
  181. _logger.Entity(deviation, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  182. break;
  183. case "site":
  184. var site = _siteService.GetSiteById(model.EntityId);
  185. site.Comment = model.Comment;
  186. _siteService.UpdateSite(site);
  187. _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  188. break;
  189. default:
  190. return new EmptyResult();
  191. }
  192. return new JsonResult
  193. {
  194. Data = "success"
  195. };
  196. }
  197. #endregion
  198. }
  199. }