| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using GreenTree.Nachtragsmanagement.Core.Authentication;
- using GreenTree.Nachtragsmanagement.Services.Appendix;
- using GreenTree.Nachtragsmanagement.Services.Deviation;
- using GreenTree.Nachtragsmanagement.Services.Logging;
- using GreenTree.Nachtragsmanagement.Services.Site;
- using GreenTree.Nachtragsmanagement.Services.User;
- using GreenTree.Nachtragsmanagement.Web.Models.Global;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace GreenTree.Nachtragsmanagement.Web.Controllers
- {
- public class GlobalController : Controller
- {
- private readonly IUserHelper _userHelper;
- private readonly IUserService _userService;
- private readonly IAppendixService _appendixService;
- private readonly IDeviationService _deviationService;
- private readonly ISiteService _siteService;
- private readonly ILogger _logger;
- public GlobalController(
- IUserHelper userHelper,
- IUserService userService,
- IAppendixService appendixService,
- IDeviationService deviationService,
- ISiteService siteService,
- ILogger logger)
- {
- _userHelper = userHelper;
- _userService = userService;
- _appendixService = appendixService;
- _deviationService = deviationService;
- _siteService = siteService;
- _logger = logger;
- }
- /// <summary>
- /// Represents the global Footer
- /// </summary>
- public ActionResult Footer()
- {
- var cookieUser = _userHelper.FromCookies();
- if (cookieUser == null)
- return View("~/Views/Shared/_Footer.cshtml", null);
- var dbUser = _userService.GetUserById(cookieUser.Id);
- if (dbUser == null)
- return View("~/Views/Shared/_Footer.cshtml", null);
- var footerModel = new FooterModel
- {
- CustomNumber = dbUser.CustomNumber,
- Forename = dbUser.Forename,
- Lastname = dbUser.Lastname,
- RoleDescription = dbUser.CurrentRole.Description
- };
- ViewData["Roles"] =
- dbUser.Roles
- .Select(r => new
- {
- r.Id,
- r.Description
- });
- return View("~/Views/Shared/_Footer.cshtml", footerModel);
- }
- /// <summary>
- /// Sets the current role of the logged in user and redirects to the home page
- /// </summary>
- /// <param name="roleId">The id of the new role.</param>
- public ActionResult SetRole(int roleId = -1)
- {
- try
- {
- if (roleId == -1)
- return RedirectToAction("Index", "Home");
- var user = _userHelper.FromCookies();
- var role = _userService.GetRoleById(roleId);
- user.CurrentRole = role;
- _userHelper.ToCookies(user);
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Wechsel einer Rolle.", ex, _userHelper.FromCookies());
- }
- return RedirectToAction("Index", "Home");
- }
- /// <summary>
- /// Shows an unauthorized message
- /// </summary>
- public ActionResult NotAuthorized()
- {
- return View("~/Views/Global/NotAuthorized.cshtml");
- }
- #region Comments
- /// <summary>
- /// Gets a full comment of a given entity
- /// </summary>
- /// <param name="entityType">The entity type.</param>
- /// <param name="id">The entity id.</param>
- public ActionResult GetEntityComment(string entityType, int id)
- {
- var result = new JsonResult
- {
- JsonRequestBehavior = JsonRequestBehavior.AllowGet,
- Data = String.Empty
- };
- if (String.IsNullOrEmpty(entityType))
- return result;
- switch (entityType)
- {
- case "appendix":
- var appendix = _appendixService.GetAppendixById(id);
- result.Data = appendix.Comment;
- break;
- case "deviation":
- var deviation = _deviationService.GetDeviationById(id);
- result.Data = deviation.Comment;
- break;
- case "site":
- var site = _siteService.GetSiteById(id);
- result.Data = site.Comment;
- break;
- default:
- return result;
- }
- return result;
- }
- /// <summary>
- /// Gets a full comment of a given entity and provides an edit form
- /// </summary>
- /// <param name="entityType">The entity type.</param>
- /// <param name="id">The entity id.</param>
- public ActionResult EditEntityComment(string entityType, int id)
- {
- var editCommentModel = new EditEntityCommentModel
- {
- EntityType = entityType,
- EntityId = id
- };
- switch (entityType)
- {
- case "appendix":
- var appendix = _appendixService.GetAppendixById(id);
- editCommentModel.Comment = appendix.Comment;
- break;
- case "deviation":
- var deviation = _deviationService.GetDeviationById(id);
- editCommentModel.Comment = deviation.Comment;
- break;
- case "site":
- var site = _siteService.GetSiteById(id);
- editCommentModel.Comment = site.Comment;
- break;
- default:
- return new EmptyResult();
- }
- return PartialView("~/Views/Shared/_EditCommentPartial.cshtml", editCommentModel);
- }
- /// <summary>
- /// Sets a comment for a given entity
- /// </summary>
- /// <param name="entityType">The entity type.</param>
- /// <param name="id">The entity id.</param>
- [HttpPost, ValidateInput(false)]
- public ActionResult EditEntityComment(EditEntityCommentModel model)
- {
- if (model == null)
- return new EmptyResult();
- switch (model.EntityType)
- {
- case "appendix":
- var appendix = _appendixService.GetAppendixById(model.EntityId);
- appendix.Comment = model.Comment;
- _appendixService.UpdateAppendix(appendix);
- _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- break;
- case "deviation":
- var deviation = _deviationService.GetDeviationById(model.EntityId);
- deviation.Comment = model.Comment;
- _deviationService.UpdateDeviation(deviation);
- _logger.Entity(deviation, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- break;
- case "site":
- var site = _siteService.GetSiteById(model.EntityId);
- site.Comment = model.Comment;
- _siteService.UpdateSite(site);
- _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- break;
- default:
- return new EmptyResult();
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- #endregion
- }
- }
|