2012-11-04 01:39:29 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using AutoMapper;
|
2012-11-03 18:23:47 +00:00
|
|
|
|
using Ninject;
|
2012-11-02 07:35:49 +00:00
|
|
|
|
using NzbDrone.Api.Filters;
|
2012-11-03 18:23:47 +00:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2012-11-04 01:39:29 +00:00
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
2012-11-02 07:35:49 +00:00
|
|
|
|
using ServiceStack.ServiceInterface;
|
|
|
|
|
|
2012-11-03 18:23:47 +00:00
|
|
|
|
namespace NzbDrone.Api.QualityProfiles
|
2012-11-02 07:35:49 +00:00
|
|
|
|
{
|
|
|
|
|
[ValidApiRequest]
|
2012-11-04 01:39:29 +00:00
|
|
|
|
public class QualityProfileService : RestServiceBase<QualityProfileModel>
|
2012-11-02 07:35:49 +00:00
|
|
|
|
{
|
2012-11-03 18:23:47 +00:00
|
|
|
|
private readonly QualityProvider _qualityProvider;
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
public QualityProfileService(QualityProvider qualityProvider)
|
|
|
|
|
{
|
|
|
|
|
_qualityProvider = qualityProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public QualityProfileService()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-04 01:39:29 +00:00
|
|
|
|
public override object OnGet(QualityProfileModel request)
|
2012-11-02 07:35:49 +00:00
|
|
|
|
{
|
2012-11-03 18:23:47 +00:00
|
|
|
|
if (request.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
var profiles = _qualityProvider.All();
|
2012-11-24 08:03:38 +00:00
|
|
|
|
var models = new List<QualityProfileModel>();
|
|
|
|
|
|
|
|
|
|
profiles.ForEach(p => models.Add(ToModel(p)));
|
|
|
|
|
return models;
|
2012-11-03 18:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var profile = _qualityProvider.Get(request.Id);
|
2012-11-24 08:03:38 +00:00
|
|
|
|
return ToModel(profile);
|
2012-11-02 07:35:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-04 01:39:29 +00:00
|
|
|
|
public override object OnPost(QualityProfileModel data)
|
|
|
|
|
{
|
|
|
|
|
var profile = Mapper.Map<QualityProfileModel, QualityProfile>(data);
|
|
|
|
|
_qualityProvider.Update(profile);
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object OnPut(QualityProfileModel data)
|
|
|
|
|
{
|
|
|
|
|
var profile = Mapper.Map<QualityProfileModel, QualityProfile>(data);
|
|
|
|
|
data.Id = _qualityProvider.Add(profile);
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object OnDelete(QualityProfileModel data)
|
|
|
|
|
{
|
|
|
|
|
_qualityProvider.Delete(data.Id);
|
|
|
|
|
|
|
|
|
|
return "ok";
|
|
|
|
|
}
|
2012-11-24 08:03:38 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2012-11-02 07:35:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|