From 429b96fbbe44aa4206557b62d63f023519d58d7d Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Fri, 6 Jun 2014 21:23:36 +0800 Subject: [PATCH] Added edit bindings --- .../Controllers/BindingController.cs | 74 ++++++++++++++++--- .../ControlPanel/Views/Binding/Create.cshtml | 4 +- .../ControlPanel/Views/Binding/Edit.cshtml | 22 ++++++ .../ControlPanel/Views/WebSite/Detail.cshtml | 9 ++- .../AzureWebFarm.ControlPanel.csproj | 3 + 5 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Edit.cshtml diff --git a/AzureWebFarm.ControlPanel/Areas/ControlPanel/Controllers/BindingController.cs b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Controllers/BindingController.cs index 672fe22..3ba733c 100644 --- a/AzureWebFarm.ControlPanel/Areas/ControlPanel/Controllers/BindingController.cs +++ b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Controllers/BindingController.cs @@ -29,6 +29,9 @@ public ActionResult Create(Guid websiteId) [HttpPost] public ActionResult Create(CreateBindingViewModel vm) { + if (!ModelState.IsValid) + return View(vm); + var binding = new Binding { HostName = vm.HostName, @@ -39,7 +42,36 @@ public ActionResult Create(CreateBindingViewModel vm) }; _webSiteRepository.AddBindingToWebSite(vm.WebsiteId, binding); - return RedirectToAction("Detail", "WebSite", new {Id = vm.WebsiteId}); + return RedirectToAction("Detail", "WebSite", new { area = ControlPanelAreaRegistration.Name, Id = vm.WebsiteId }); + } + + public ActionResult Edit(Guid id) + { + var binding = _webSiteRepository.RetrieveBinding(id); + if (binding == null) + return HttpNotFound(); + + return View(new EditBindingViewModel(binding)); + } + + [HttpPost] + public ActionResult Edit(EditBindingViewModel vm) + { + var binding = _webSiteRepository.RetrieveBinding(vm.Id); + if (binding == null) + return HttpNotFound(); + + if (!ModelState.IsValid) + return View(vm); + + binding.HostName = vm.HostName; + binding.Port = vm.Port; + binding.Protocol = vm.Protocol.ToString().ToLower(); + binding.IpAddress = vm.IpAddress; + binding.CertificateThumbprint = vm.CertificateThumbprint; + _webSiteRepository.UpdateBinding(binding); + + return RedirectToAction("Detail", "WebSite", new { area = ControlPanelAreaRegistration.Name, Id = binding.WebSiteId }); } [HttpPost] @@ -50,14 +82,41 @@ public ActionResult Delete(Guid id) return HttpNotFound(); _webSiteRepository.RemoveBinding(id); - return RedirectToAction("Detail", "WebSite", new {Id = binding.WebSiteId}); + return RedirectToAction("Detail", "WebSite", new { area = ControlPanelAreaRegistration.Name, Id = binding.WebSiteId }); } } - public class CreateBindingViewModel + public class CreateBindingViewModel : BindingViewModel + { + public CreateBindingViewModel() + { + Port = 80; + IpAddress = "*"; + } + + public Guid WebsiteId { get; set; } + } + + public class EditBindingViewModel : BindingViewModel + { + public EditBindingViewModel() {} + + public EditBindingViewModel(Binding binding) + { + HostName = binding.HostName; + Port = binding.Port; + Protocol = (Protocol) Enum.Parse(typeof (Protocol), binding.Protocol, true); + IpAddress = binding.IpAddress; + CertificateThumbprint = binding.CertificateThumbprint; + } + + public Guid Id { get; set; } + } + + public class BindingViewModel { private static readonly List CertificatesCache = new List(); - static CreateBindingViewModel() + static BindingViewModel() { var store = new X509Store(StoreName.My, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadWrite); @@ -72,12 +131,6 @@ static CreateBindingViewModel() store.Close(); } - public CreateBindingViewModel() - { - Port = 80; - IpAddress = "*"; - } - [Required] public string HostName { get; set; } [Required] @@ -89,7 +142,6 @@ public CreateBindingViewModel() public string IpAddress { get; set; } [ExistsIn("Certificates", "Thumbprint", "Name")] public string CertificateThumbprint { get; set; } - public Guid WebsiteId { get; set; } public IList Certificates { get { return CertificatesCache; } } } diff --git a/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Create.cshtml b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Create.cshtml index fd9d076..8cdbb84 100644 --- a/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Create.cshtml +++ b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Create.cshtml @@ -2,7 +2,7 @@ @using ChameleonForms.Templates.TwitterBootstrap3 @model CreateBindingViewModel -

Create Website

+

Create Binding

@using (var f = Html.BeginChameleonForm()) { @@ -19,6 +19,6 @@ using (var n = f.BeginNavigation()) { - @n.Submit("Create").WithStyle(EmphasisStyle.Primary).WithIcon("globe") + @n.Submit("Create").WithStyle(EmphasisStyle.Primary).WithIcon("cloud-upload") } } diff --git a/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Edit.cshtml b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Edit.cshtml new file mode 100644 index 0000000..1a74790 --- /dev/null +++ b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/Binding/Edit.cshtml @@ -0,0 +1,22 @@ +@using AzureWebFarm.ControlPanel.Areas.ControlPanel.Controllers +@using ChameleonForms.Templates.TwitterBootstrap3 +@model EditBindingViewModel + +

Create Binding

+ +@using (var f = Html.BeginChameleonForm()) +{ + using (var s = f.BeginSection()) + { + @s.FieldFor(m => m.HostName) + @s.FieldFor(m => m.IpAddress) + @s.FieldFor(m => m.Port) + @s.FieldFor(m => m.Protocol) + @s.FieldFor(m => m.CertificateThumbprint) + } + + using (var n = f.BeginNavigation()) + { + @n.Submit("Edit").WithStyle(EmphasisStyle.Primary).WithIcon("cloud-upload") + } +} diff --git a/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/WebSite/Detail.cshtml b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/WebSite/Detail.cshtml index b6521df..23cc405 100644 --- a/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/WebSite/Detail.cshtml +++ b/AzureWebFarm.ControlPanel/Areas/ControlPanel/Views/WebSite/Detail.cshtml @@ -1,8 +1,15 @@ @using AzureWebFarm.ControlPanel.Areas.ControlPanel.Views +@using AzureWebFarm.Entities @using MvcContrib.UI.Grid @using AzureWebFarm.ControlPanel.Areas.ControlPanel.Controllers @model WebSiteDetailViewModel +@helper BindingButtons(Binding b) +{ + Edit binding +
+} +

Website: @Model.Site.Name

@@ -16,7 +23,7 @@ c.For(b => b.Port); c.For(b => b.IpAddress); c.For(b => new HtmlString(!string.IsNullOrEmpty(b.CertificateThumbprint) ? HttpUtility.HtmlEncode(b.CertificateThumbprint) : "None")).Named("Certificate Thumbprint"); - c.For(b => new HtmlString(string.Format("
", Url.Action("Delete", "Binding", new {b.Id}), b.Protocol, b.HostName, b.Port))).Named("Delete Binding"); + c.For(b => BindingButtons(b)).Named("Actions"); })
diff --git a/AzureWebFarm.ControlPanel/AzureWebFarm.ControlPanel.csproj b/AzureWebFarm.ControlPanel/AzureWebFarm.ControlPanel.csproj index d0415a2..316ed01 100644 --- a/AzureWebFarm.ControlPanel/AzureWebFarm.ControlPanel.csproj +++ b/AzureWebFarm.ControlPanel/AzureWebFarm.ControlPanel.csproj @@ -237,6 +237,9 @@ + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)