PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Kudu.Web/Controllers/ApplicationController.cs

https://github.com/moacap/kudu
C# | 130 lines | 108 code | 22 blank | 0 comment | 6 complexity | 5873d95f24668ecb48c9290d951580df MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Threading.Tasks;
  5. using System.Web.Mvc;
  6. using Kudu.Client.Infrastructure;
  7. using Kudu.Contracts.Infrastructure;
  8. using Kudu.Web.Infrastructure;
  9. using Kudu.Web.Models;
  10. using Mvc.Async;
  11. namespace Kudu.Web.Controllers
  12. {
  13. public class ApplicationController : TaskAsyncController
  14. {
  15. private readonly IApplicationService _applicationService;
  16. private readonly KuduEnvironment _environment;
  17. private readonly ICredentialProvider _credentialProvider;
  18. public ApplicationController(IApplicationService applicationService,
  19. ICredentialProvider credentialProvider,
  20. KuduEnvironment environment)
  21. {
  22. _applicationService = applicationService;
  23. _credentialProvider = credentialProvider;
  24. _environment = environment;
  25. }
  26. protected override void OnActionExecuting(ActionExecutingContext filterContext)
  27. {
  28. ViewBag.showAdmingWarning = !_environment.IsAdmin && _environment.RunningAgainstLocalKuduService;
  29. base.OnActionExecuting(filterContext);
  30. }
  31. public ViewResult Index()
  32. {
  33. var applications = (from name in _applicationService.GetApplications()
  34. orderby name
  35. select name).ToList();
  36. return View(applications);
  37. }
  38. public Task<ActionResult> Details(string slug)
  39. {
  40. IApplication application = _applicationService.GetApplication(slug);
  41. if (application == null)
  42. {
  43. return HttpNotFoundAsync();
  44. }
  45. ICredentials credentials = _credentialProvider.GetCredentials();
  46. return application.GetRepositoryInfo(credentials).Then(repositoryInfo =>
  47. {
  48. var appViewModel = new ApplicationViewModel(application);
  49. appViewModel.RepositoryInfo = repositoryInfo;
  50. ViewBag.slug = slug;
  51. ViewBag.tab = "settings";
  52. ViewBag.appName = appViewModel.Name;
  53. return (ActionResult)View(appViewModel);
  54. });
  55. }
  56. [HttpGet]
  57. public ActionResult Create()
  58. {
  59. return View();
  60. }
  61. [HttpPost]
  62. public ActionResult Create(string name)
  63. {
  64. string slug = name.GenerateSlug();
  65. try
  66. {
  67. _applicationService.AddApplication(slug);
  68. return RedirectToAction("Details", new { slug });
  69. }
  70. catch (SiteExistsException)
  71. {
  72. ModelState.AddModelError("Name", "Site already exists");
  73. }
  74. catch (Exception ex)
  75. {
  76. ModelState.AddModelError("", ex.Message);
  77. }
  78. return View("Create");
  79. }
  80. [HttpPost]
  81. public ActionResult Delete(string slug)
  82. {
  83. if (_applicationService.DeleteApplication(slug))
  84. {
  85. return RedirectToAction("Index");
  86. }
  87. return HttpNotFound();
  88. }
  89. public Task<ActionResult> Trace(string slug)
  90. {
  91. IApplication application = _applicationService.GetApplication(slug);
  92. if (application == null)
  93. {
  94. return HttpNotFoundAsync();
  95. }
  96. ICredentials credentials = _credentialProvider.GetCredentials();
  97. return application.DownloadTrace(credentials).Then(document =>
  98. {
  99. return (ActionResult)View(document);
  100. });
  101. }
  102. public ActionResult Develop(string slug)
  103. {
  104. _applicationService.CreateDevelopmentSite(slug);
  105. return RedirectToAction("Details", new { slug });
  106. }
  107. }
  108. }