From 79902194df83b593cb6cafe856053b6973261b4e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 2 Dec 2012 19:08:26 -0800 Subject: [PATCH] QualityProfiles edit via backbone --- NzbDrone.Api/Bootstrapper.cs | 9 +++- NzbDrone.Api/NzbDrone.Api.csproj | 2 + .../QualityProfiles/QualityProfileService.cs | 29 +++-------- .../Resolvers/AllowedToQualitiesResolver.cs | 25 +++++++++ .../Resolvers/QualitiesToAllowedResolver.cs | 27 ++++++++++ .../FakesAssemblies/Ninject.Web.Mvc.Fakes.dll | Bin 89088 -> 89088 bytes .../Scripts/backbone/views/qualityProfiles.js | 49 ++++++++++++++++-- NzbDrone.Web/Views/Settings/Quality.cshtml | 20 +++++-- 8 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 NzbDrone.Api/Resolvers/AllowedToQualitiesResolver.cs create mode 100644 NzbDrone.Api/Resolvers/QualitiesToAllowedResolver.cs diff --git a/NzbDrone.Api/Bootstrapper.cs b/NzbDrone.Api/Bootstrapper.cs index d065673fb..1b2bc6328 100644 --- a/NzbDrone.Api/Bootstrapper.cs +++ b/NzbDrone.Api/Bootstrapper.cs @@ -22,8 +22,13 @@ namespace NzbDrone.Api //Mapper.CreateMap() // .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId)); - //Mapper.CreateMap() - // .ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id)); + Mapper.CreateMap() + .ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id)) + .ForMember(dest => dest.Allowed, opt => opt.ResolveUsing().FromMember(src => src.Qualities)); + + Mapper.CreateMap() + .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId)) + .ForMember(dest => dest.Qualities, opt => opt.ResolveUsing().FromMember(src => src.Allowed)); Mapper.CreateMap() .ForMember(dest => dest.Allowed, opt => opt.Ignore()); diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index 27e849c94..2d5ae5007 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -104,6 +104,8 @@ + + diff --git a/NzbDrone.Api/QualityProfiles/QualityProfileService.cs b/NzbDrone.Api/QualityProfiles/QualityProfileService.cs index 61cc52244..c21f40ad2 100644 --- a/NzbDrone.Api/QualityProfiles/QualityProfileService.cs +++ b/NzbDrone.Api/QualityProfiles/QualityProfileService.cs @@ -29,28 +29,27 @@ namespace NzbDrone.Api.QualityProfiles if (request.Id == 0) { var profiles = _qualityProvider.All(); - var models = new List(); - - profiles.ForEach(p => models.Add(ToModel(p))); - return models; + return Mapper.Map, List>(profiles); } var profile = _qualityProvider.Get(request.Id); - return ToModel(profile); + return Mapper.Map(profile); } public override object OnPost(QualityProfileModel data) { + //Create var profile = Mapper.Map(data); - _qualityProvider.Update(profile); + _qualityProvider.Add(profile); return data; } public override object OnPut(QualityProfileModel data) { + //Update var profile = Mapper.Map(data); - data.Id = _qualityProvider.Add(profile); + _qualityProvider.Update(profile); return data; } @@ -61,21 +60,5 @@ namespace NzbDrone.Api.QualityProfiles return "ok"; } - - public QualityProfileModel ToModel(QualityProfile profile) - { - var model = new QualityProfileModel(); - model.Id = profile.QualityProfileId; - model.Name = profile.Name; - model.Cutoff = (int)profile.Cutoff; - model.Qualities = Mapper.Map, List>(QualityTypes.All()); - - model.Qualities.ForEach(quality => - { - quality.Allowed = profile.Allowed.SingleOrDefault(q => q.Id == quality.Id) != null; - }); - - return model; - } } } \ No newline at end of file diff --git a/NzbDrone.Api/Resolvers/AllowedToQualitiesResolver.cs b/NzbDrone.Api/Resolvers/AllowedToQualitiesResolver.cs new file mode 100644 index 000000000..2205c0759 --- /dev/null +++ b/NzbDrone.Api/Resolvers/AllowedToQualitiesResolver.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using AutoMapper; +using NzbDrone.Api.QualityProfiles; +using NzbDrone.Core.Repository.Quality; + +namespace NzbDrone.Api.Resolvers +{ + public class AllowedToQualitiesResolver : ValueResolver, List> + { + protected override List ResolveCore(List source) + { + var qualities = Mapper.Map, List>(QualityTypes.All()); + + qualities.ForEach(quality => + { + quality.Allowed = source.SingleOrDefault(q => q.Id == quality.Id) != null; + }); + + return qualities; + } + } +} diff --git a/NzbDrone.Api/Resolvers/QualitiesToAllowedResolver.cs b/NzbDrone.Api/Resolvers/QualitiesToAllowedResolver.cs new file mode 100644 index 000000000..901644223 --- /dev/null +++ b/NzbDrone.Api/Resolvers/QualitiesToAllowedResolver.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using AutoMapper; +using NzbDrone.Api.QualityProfiles; +using NzbDrone.Core.Repository.Quality; + +namespace NzbDrone.Api.Resolvers +{ + public class QualitiesToAllowedResolver : ValueResolver, List> + { + protected override List ResolveCore(List source) + { + var ids = source.Where(s => s.Allowed).Select(s => s.Id).ToList(); + + var qualityTypes = new List(); + + ids.ForEach(id => + { + qualityTypes.Add(QualityTypes.FindById(id)); + }); + + return qualityTypes; + } + } +} diff --git a/NzbDrone.Web/FakesAssemblies/Ninject.Web.Mvc.Fakes.dll b/NzbDrone.Web/FakesAssemblies/Ninject.Web.Mvc.Fakes.dll index 3f0b67053a43cade51377574a9545216c834c53c..3111e0dcbe69fed62a587c15855122c25f5915fd 100644 GIT binary patch delta 235 zcmVqa3-||vz9W}b!^ut+1{QR z)h-XLapMvt8e+7&!9h_}d5^lN^=kOyb6RL^%Xmm-MzbJdGswBzlDI~`CQ|YRT`UCm zBLR4W&;qy60s%M^5IGXxiZu?AOS7#QWhL)pa<>>30Z2V8oDQ^5000000ssI29033T lcyR#$cvArYQd2}z&OkBx&S;@anUgjl)xK6Nw~SK(bO=f8XhQ%1 delta 235 zcmV;{>}0$;ZVy`sgI_j7+o&;qy60s%M^5DasMWw%}&MzNw~SK(bO_q-XQKcB diff --git a/NzbDrone.Web/Scripts/backbone/views/qualityProfiles.js b/NzbDrone.Web/Scripts/backbone/views/qualityProfiles.js index e4d8a9f69..c8219238b 100644 --- a/NzbDrone.Web/Scripts/backbone/views/qualityProfiles.js +++ b/NzbDrone.Web/Scripts/backbone/views/qualityProfiles.js @@ -3,14 +3,57 @@ className: "quality-profile", template: "#QualityProfileTemplate", events: { - 'click .quality-selectee': 'toggleAllowed' + 'click .quality-selectee': 'toggleAllowed', + 'change .cutoff': 'changeCutoff', + 'change .name': 'changeName' }, toggleAllowed: function (e) { //Add to cutoff //Update model + var target = $(e.target); - var checked = $(e.target).attr('checked') != undefined; - this.model.set({ }); + var checked = $(target).attr('checked') != undefined; + var id = this.model.get("Id"); + + var qualities = _.clone(this.model.get("Qualities")); + _.each(qualities, function (qualityType) { + var qualityId = parseInt($(target).attr('data-quality-id')); + + if (qualityType.Id == qualityId) { + qualityType.Allowed = checked; + + //Todo: Add/Remove from cutoff + //Find cutoff dropdown + var cutoff = ('select#' + id); + + if (checked) { + $('').val(qualityId).appendTo(cutoff); + } + + else { + $(cutoff).find('option[value="' + qualityId + '"]').remove(); + } + } + }); + + this.model.set({ "Qualities": qualities }); + this.model.save(); + }, + changeCutoff: function(e) { + //Todo: save change + var cutoff = $(e.target).val(); + + this.model.set({ "Cutoff": cutoff }); + this.model.save(); + }, + changeName: function(e) { + var name = $(e.target).val(); + + //Todo: update default quality dropdown + $('#DefaultQualityProfileId option[value="' + this.model.get("Id") + '"]').html(name); + + this.model.set({ "Name": name }); + this.model.save(); } }); diff --git a/NzbDrone.Web/Views/Settings/Quality.cshtml b/NzbDrone.Web/Views/Settings/Quality.cshtml index 7a3070992..f8fde4e8b 100644 --- a/NzbDrone.Web/Views/Settings/Quality.cshtml +++ b/NzbDrone.Web/Views/Settings/Quality.cshtml @@ -120,8 +120,22 @@ @*@Html.IncludeScript("backbone/bootstrapper.js")*@