2013-07-19 05:47:55 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-02-27 04:19:22 +01:00
|
|
|
|
using NzbDrone.Core.Qualities;
|
2013-05-31 02:12:20 +02:00
|
|
|
|
using NzbDrone.Api.Mapping;
|
2013-05-31 03:22:28 +02:00
|
|
|
|
using System.Linq;
|
2013-08-26 05:50:13 +02:00
|
|
|
|
using FluentValidation;
|
2013-01-19 02:27:30 +01:00
|
|
|
|
|
2013-05-20 02:30:02 +02:00
|
|
|
|
namespace NzbDrone.Api.Qualities
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-06-25 09:21:10 +02:00
|
|
|
|
public class QualityProfileModule : NzbDroneRestModule<QualityProfileResource>
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-06-25 09:21:10 +02:00
|
|
|
|
private readonly QualityProfileService _qualityProfileService;
|
2013-01-19 02:27:30 +01:00
|
|
|
|
|
2013-06-25 09:21:10 +02:00
|
|
|
|
public QualityProfileModule(QualityProfileService qualityProfileService)
|
|
|
|
|
: base("/qualityprofiles")
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-06-25 09:21:10 +02:00
|
|
|
|
_qualityProfileService = qualityProfileService;
|
2013-05-31 02:12:20 +02:00
|
|
|
|
|
2013-08-26 05:50:13 +02:00
|
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
|
|
|
|
SharedValidator.RuleFor(c => c.Cutoff).NotNull();
|
|
|
|
|
SharedValidator.RuleFor(c => c.Allowed).NotEmpty();
|
|
|
|
|
|
2013-05-31 02:12:20 +02:00
|
|
|
|
GetResourceAll = GetAll;
|
|
|
|
|
|
|
|
|
|
GetResourceById = GetById;
|
|
|
|
|
|
|
|
|
|
UpdateResource = Update;
|
|
|
|
|
|
|
|
|
|
CreateResource = Create;
|
|
|
|
|
|
|
|
|
|
DeleteResource = DeleteProfile;
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 07:14:55 +02:00
|
|
|
|
private int Create(QualityProfileResource resource)
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-05-31 02:12:20 +02:00
|
|
|
|
var model = resource.InjectTo<QualityProfile>();
|
2013-06-25 09:21:10 +02:00
|
|
|
|
model = _qualityProfileService.Add(model);
|
2013-08-26 07:14:55 +02:00
|
|
|
|
return model.Id;
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 02:12:20 +02:00
|
|
|
|
private void DeleteProfile(int id)
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-06-25 09:21:10 +02:00
|
|
|
|
_qualityProfileService.Delete(id);
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 07:14:55 +02:00
|
|
|
|
private void Update(QualityProfileResource resource)
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-05-31 02:12:20 +02:00
|
|
|
|
var model = resource.InjectTo<QualityProfile>();
|
2013-06-25 09:21:10 +02:00
|
|
|
|
_qualityProfileService.Update(model);
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 02:12:20 +02:00
|
|
|
|
private QualityProfileResource GetById(int id)
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-06-25 09:21:10 +02:00
|
|
|
|
return QualityToResource(_qualityProfileService.Get(id));
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 02:12:20 +02:00
|
|
|
|
private List<QualityProfileResource> GetAll()
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-06-25 09:21:10 +02:00
|
|
|
|
var allProfiles = _qualityProfileService.All();
|
2013-05-31 03:22:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var profiles = allProfiles.Select(QualityToResource).ToList();
|
|
|
|
|
|
|
|
|
|
return profiles;
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
2013-05-31 02:12:20 +02:00
|
|
|
|
|
2013-05-31 03:22:28 +02:00
|
|
|
|
private static QualityProfileResource QualityToResource(QualityProfile profile)
|
|
|
|
|
{
|
|
|
|
|
return new QualityProfileResource
|
|
|
|
|
{
|
|
|
|
|
Cutoff = profile.Cutoff.InjectTo<QualityResource>(),
|
2013-05-31 07:56:00 +02:00
|
|
|
|
Available = Quality.All()
|
|
|
|
|
.Where(c => !profile.Allowed.Any(q => c.Id == q.Id))
|
|
|
|
|
.InjectTo<List<QualityResource>>(),
|
|
|
|
|
|
2013-05-31 03:22:28 +02:00
|
|
|
|
Allowed = profile.Allowed.InjectTo<List<QualityResource>>(),
|
|
|
|
|
Name = profile.Name,
|
|
|
|
|
Id = profile.Id
|
|
|
|
|
};
|
|
|
|
|
}
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|