sonarr-repo-only/NzbDrone.Api/Qualities/QualityProfileModule.cs

81 lines
2.4 KiB
C#
Raw Normal View History

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