QualityProfiles edit via backbone

This commit is contained in:
Mark McDowall 2012-12-02 19:08:26 -08:00
parent bc424709af
commit 79902194df
8 changed files with 130 additions and 31 deletions

View File

@ -22,8 +22,13 @@ namespace NzbDrone.Api
//Mapper.CreateMap<QualityProfile, QualityProfileModel>()
// .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId));
//Mapper.CreateMap<QualityProfileModel, QualityProfile>()
// .ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id));
Mapper.CreateMap<QualityProfileModel, QualityProfile>()
.ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Allowed, opt => opt.ResolveUsing<QualitiesToAllowedResolver>().FromMember(src => src.Qualities));
Mapper.CreateMap<QualityProfile, QualityProfileModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId))
.ForMember(dest => dest.Qualities, opt => opt.ResolveUsing<AllowedToQualitiesResolver>().FromMember(src => src.Allowed));
Mapper.CreateMap<QualityTypes, QualityProfileType>()
.ForMember(dest => dest.Allowed, opt => opt.Ignore());

View File

@ -104,6 +104,8 @@
<Compile Include="QualityProfiles\QualityProfileModel.cs" />
<Compile Include="QualityProfiles\QualityProfileService.cs" />
<Compile Include="QualityProfiles\QualityProfileType.cs" />
<Compile Include="Resolvers\AllowedToQualitiesResolver.cs" />
<Compile Include="Resolvers\QualitiesToAllowedResolver.cs" />
<Compile Include="Resolvers\QualityTypesToIntResolver.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -29,28 +29,27 @@ namespace NzbDrone.Api.QualityProfiles
if (request.Id == 0)
{
var profiles = _qualityProvider.All();
var models = new List<QualityProfileModel>();
profiles.ForEach(p => models.Add(ToModel(p)));
return models;
return Mapper.Map<List<QualityProfile>, List<QualityProfileModel>>(profiles);
}
var profile = _qualityProvider.Get(request.Id);
return ToModel(profile);
return Mapper.Map<QualityProfile, QualityProfileModel>(profile);
}
public override object OnPost(QualityProfileModel data)
{
//Create
var profile = Mapper.Map<QualityProfileModel, QualityProfile>(data);
_qualityProvider.Update(profile);
_qualityProvider.Add(profile);
return data;
}
public override object OnPut(QualityProfileModel data)
{
//Update
var profile = Mapper.Map<QualityProfileModel, QualityProfile>(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>, List<QualityProfileType>>(QualityTypes.All());
model.Qualities.ForEach(quality =>
{
quality.Allowed = profile.Allowed.SingleOrDefault(q => q.Id == quality.Id) != null;
});
return model;
}
}
}

View File

@ -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<QualityTypes>, List<QualityProfileType>>
{
protected override List<QualityProfileType> ResolveCore(List<QualityTypes> source)
{
var qualities = Mapper.Map<List<QualityTypes>, List<QualityProfileType>>(QualityTypes.All());
qualities.ForEach(quality =>
{
quality.Allowed = source.SingleOrDefault(q => q.Id == quality.Id) != null;
});
return qualities;
}
}
}

View File

@ -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<QualityProfileType>, List<QualityTypes>>
{
protected override List<QualityTypes> ResolveCore(List<QualityProfileType> source)
{
var ids = source.Where(s => s.Allowed).Select(s => s.Id).ToList();
var qualityTypes = new List<QualityTypes>();
ids.ForEach(id =>
{
qualityTypes.Add(QualityTypes.FindById(id));
});
return qualityTypes;
}
}
}

View File

@ -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) {
$('<option>' + qualityType.Name + '</option>').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();
}
});

View File

@ -120,8 +120,22 @@
@*@Html.IncludeScript("backbone/bootstrapper.js")*@
<script id="QualityProfileTemplate" type="text/template">
<%= Name %>
<%= Cutoff %>
<div>
<strong>Name</strong>
<input type="text" value="<%= Name %>" class="name" />
</div>
<div>
<strong>Cutoff</strong>
<select id="<%= Id %>" class="cutoff">
<% _.each(Qualities, function(quality) { %>
<% if (quality.Allowed === true) { %>
<option value="<%= quality.Id %>" <%= quality.Id === Cutoff ? 'selected="selected"' : '' %>><%= quality.Name %></option>
<% } %>
<% }); %>
</select>
</div>
<% _.each(Qualities, function(quality) { %>
<input id="<%= Id %>_<%= quality.Id %>"
class="quality-selectee"
@ -138,7 +152,7 @@
</script>
<script>
$(document).ready(function () {
$(document).ready(function() {
QualityProfileApp.App.start();
});
</script>