HomeController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using GreenTree.Nachtragsmanagement.Services.Configuration;
  7. using GreenTree.Nachtragsmanagement.Services.Test;
  8. using GreenTree.Nachtragsmanagement.Services.User;
  9. using GreenTree.Nachtragsmanagement.Web.Framework;
  10. using GreenTree.Nachtragsmanagement.Web.Models.Test;
  11. using GreenTree.Nachtragsmanagement.Web.Models.User;
  12. using Newtonsoft.Json;
  13. using GreenTree.Nachtragsmanagement.Core.Plugins;
  14. using GreenTree.Nachtragsmanagement.Core;
  15. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  16. {
  17. public class HomeController : Controller
  18. {
  19. private readonly IDbRelationService _dbRelationService;
  20. private readonly IConfigurationService _configurationService;
  21. private readonly IUserService _userService;
  22. private readonly IPluginFinder _pluginFinder;
  23. private readonly IWebHelper _webHelper;
  24. public HomeController(
  25. IDbRelationService dbRelationService,
  26. IConfigurationService configurationService,
  27. IUserService userService,
  28. IPluginFinder pluginFinder,
  29. IWebHelper webHelper)
  30. {
  31. _dbRelationService = dbRelationService;
  32. _configurationService = configurationService;
  33. _userService = userService;
  34. _pluginFinder = pluginFinder;
  35. _webHelper = webHelper;
  36. }
  37. // GET: Home
  38. public ActionResult Index()
  39. {
  40. return View("~/Views/Home/Index.cshtml");
  41. }
  42. public ActionResult Relations()
  43. {
  44. var users = _userService.GetAllUsers();
  45. var relations = _dbRelationService.GetRelations(3, DbRelationFormat.Json);
  46. var model = new DbRelationModel
  47. {
  48. UserJson = relations[0],
  49. DeviationJson = relations[1],
  50. SiteJson = relations[2],
  51. AppendixJson = relations[3]
  52. };
  53. var configSection = _configurationService.GetCurrentConfiguration();
  54. return View("~/Views/Home/Relations.cshtml", model);
  55. }
  56. public ActionResult Plugins()
  57. {
  58. var model = new PluginModel
  59. {
  60. PluginNames = new List<string[]>()
  61. };
  62. var uninstalledPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.NotInstalledOnly);
  63. var installedPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.InstalledOnly);
  64. if (installedPlugins.Any())
  65. model.PluginNames.AddRange(new List<string[]>()
  66. {
  67. new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" }
  68. });
  69. if (uninstalledPlugins.Any())
  70. model.PluginNames.AddRange(new List<string[]>()
  71. {
  72. new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" }
  73. });
  74. return View("~/Views/Home/Plugins.cshtml", model);
  75. }
  76. [HttpPost]
  77. public ActionResult InstallPlugin(string pluginName)
  78. {
  79. var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  80. if (pluginDescriptor == null)
  81. return RedirectToAction("Plugins");
  82. if (pluginDescriptor.Installed)
  83. return RedirectToAction("Plugins");
  84. pluginDescriptor.Instance().Install();
  85. _webHelper.RestartAppDomain();
  86. return RedirectToAction("Plugins");
  87. }
  88. [HttpPost]
  89. public ActionResult UninstallPlugin(string pluginName)
  90. {
  91. var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  92. if (pluginDescriptor == null)
  93. return RedirectToAction("Plugins");
  94. if (!pluginDescriptor.Installed)
  95. return RedirectToAction("Plugins");
  96. pluginDescriptor.Instance().Uninstall();
  97. _webHelper.RestartAppDomain();
  98. return RedirectToAction("Plugins");
  99. }
  100. }
  101. }