From 3a6cf56db1fa8ffd5b801398d9f67079db6d8575 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 27 Apr 2011 08:34:53 -0700 Subject: [PATCH] RootDir Adds/Deletes update the database when the action occurs, no more awkward saving tactic. --- NzbDrone.Core/Providers/RootDirProvider.cs | 7 +- .../Controllers/SettingsController.cs | 65 +++++++++++-------- NzbDrone.Web/Views/Settings/General.cshtml | 34 +++++----- NzbDrone.Web/Views/Settings/RootDir.cshtml | 5 +- 4 files changed, 62 insertions(+), 49 deletions(-) diff --git a/NzbDrone.Core/Providers/RootDirProvider.cs b/NzbDrone.Core/Providers/RootDirProvider.cs index e8e351f0a..49b214391 100644 --- a/NzbDrone.Core/Providers/RootDirProvider.cs +++ b/NzbDrone.Core/Providers/RootDirProvider.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using NzbDrone.Core.Repository; using SubSonic.Repository; @@ -21,9 +22,9 @@ namespace NzbDrone.Core.Providers return _sonioRepo.All().ToList(); } - public virtual void Add(RootDir rootDir) + public virtual int Add(RootDir rootDir) { - _sonioRepo.Add(rootDir); + return Convert.ToInt32(_sonioRepo.Add(rootDir)); } public virtual void Remove(int rootDirId) diff --git a/NzbDrone.Web/Controllers/SettingsController.cs b/NzbDrone.Web/Controllers/SettingsController.cs index a327d17f4..5924ea7fd 100644 --- a/NzbDrone.Web/Controllers/SettingsController.cs +++ b/NzbDrone.Web/Controllers/SettingsController.cs @@ -234,7 +234,36 @@ namespace NzbDrone.Web.Controllers public ViewResult AddRootDir() { - return View("RootDir", new RootDir()); + var rootDir = new RootDir { Path = String.Empty }; + + var id = _rootDirProvider.Add(rootDir); + rootDir.Id = id; + + ViewData["RootDirId"] = id; + + return View("RootDir", rootDir); + } + + public ActionResult GetRootDirView(RootDir rootDir) + { + ViewData["RootDirId"] = rootDir.Id; + + return PartialView("RootDir", rootDir); + } + + public JsonResult DeleteRootDir(int rootDirId) + { + try + { + _rootDirProvider.Remove(rootDirId); + } + + catch (Exception) + { + return new JsonResult { Data = "failed" }; + } + + return new JsonResult { Data = "ok" }; } public ActionResult SubMenu() @@ -292,37 +321,21 @@ namespace NzbDrone.Web.Controllers [HttpPost] public ActionResult SaveGeneral(SettingsModel data) { - if (data.Directories != null && data.Directories.Count > 0) + try { - //If the Javascript was beaten we need to return an error - - /* - * Kay.one: I can't see what its doing, all it does it dooesn't let me s. - * if (!data.Directories.Exists(d => d.Default)) - return Content(SETTINGS_FAILED);*/ - - var currentRootDirs = _rootDirProvider.GetAll(); - - foreach (var currentRootDir in currentRootDirs) - { - var closureRootDir = currentRootDir; - if (!data.Directories.Exists(d => d.Id == closureRootDir.Id)) - _rootDirProvider.Remove(closureRootDir.Id); - } - foreach (var dir in data.Directories) { - if (dir.Id == 0) - _rootDirProvider.Add(dir); - - else - _rootDirProvider.Update(dir); + _rootDirProvider.Update(dir); } - - return Content(SETTINGS_SAVED); + } + catch (Exception ex) + { + Logger.Debug("Failed to save Root Dirs"); + Logger.DebugException(ex.Message, ex); + return Content(SETTINGS_FAILED); } - return Content(SETTINGS_FAILED); + return Content(SETTINGS_SAVED); } [HttpPost] diff --git a/NzbDrone.Web/Views/Settings/General.cshtml b/NzbDrone.Web/Views/Settings/General.cshtml index 9b49f1e2c..da6d17287 100644 --- a/NzbDrone.Web/Views/Settings/General.cshtml +++ b/NzbDrone.Web/Views/Settings/General.cshtml @@ -40,7 +40,7 @@
@foreach (var item in Model.Directories) { - Html.RenderPartial("RootDir", item); + Html.RenderAction("GetRootDirView", item); }
@@ -63,23 +63,21 @@ return false; }); - $("a.deleteRow").live("click", function () { - $(this).parents("div.rootDirSection:first").remove(); - return false; - }); + var deleteRootDirUrl = '@Url.Action("DeleteRootDir", "Settings")'; - $(".defaultCheckbox").live("change", function () { - var checked = $(this).attr('checked'); + function deleteRootDir(id) { + sendToServer(id); + $("#div_" + id).remove(); + } - if (checked) { - var thisOne = this; - $(".defaultCheckbox").attr('checked', false); - $(this).attr('checked', true); - } - - //Don't let the user uncheck a checkbox (Like a radio button) - else { - $(this).attr('checked', true); - } - }); + function sendToServer(id) { + $.ajax({ + type: "POST", + url: deleteRootDirUrl, + data: jQuery.param({ rootDirId: id }), + error: function (req, status, error) { + alert("Sorry! We could not delete your Root Directory at this time. " + error); + } + }); + } \ No newline at end of file diff --git a/NzbDrone.Web/Views/Settings/RootDir.cshtml b/NzbDrone.Web/Views/Settings/RootDir.cshtml index 5ebe8ed1f..cd5ccee22 100644 --- a/NzbDrone.Web/Views/Settings/RootDir.cshtml +++ b/NzbDrone.Web/Views/Settings/RootDir.cshtml @@ -16,11 +16,12 @@ { var idClean = ViewData.TemplateInfo.HtmlFieldPrefix.Replace('[', '_').Replace(']', '_'); -
+ +
@Html.TextBoxFor(m => m.Path, new { @class = "root_dir_text" }) - + Delete