2013-05-31 01:22:28 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-27 03:19:22 +00:00
|
|
|
|
using NzbDrone.Core.Qualities;
|
2013-05-31 00:12:20 +00:00
|
|
|
|
using NzbDrone.Api.Mapping;
|
2013-05-31 01:22:28 +00:00
|
|
|
|
using System.Linq;
|
2013-01-19 01:27:30 +00:00
|
|
|
|
|
2013-05-20 00:30:02 +00:00
|
|
|
|
namespace NzbDrone.Api.Qualities
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-05-31 01:22:28 +00:00
|
|
|
|
|
|
|
|
|
public static class LazyLoadedExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IEnumerable<int> GetForeignKeys(this IEnumerable<ModelBase> models)
|
|
|
|
|
{
|
|
|
|
|
return models.Select(c => c.Id).Distinct();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
public class QualityProfilesModule : NzbDroneRestModule<QualityProfileResource>
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-02-27 03:19:22 +00:00
|
|
|
|
private readonly QualityProfileService _qualityProvider;
|
2013-01-19 01:27:30 +00:00
|
|
|
|
|
2013-02-27 03:19:22 +00:00
|
|
|
|
public QualityProfilesModule(QualityProfileService qualityProvider)
|
2013-05-31 00:12:20 +00:00
|
|
|
|
: base("/qualityProfiles")
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-01-20 00:19:27 +00:00
|
|
|
|
_qualityProvider = qualityProvider;
|
2013-05-31 00:12:20 +00:00
|
|
|
|
|
|
|
|
|
GetResourceAll = GetAll;
|
|
|
|
|
|
|
|
|
|
GetResourceById = GetById;
|
|
|
|
|
|
|
|
|
|
UpdateResource = Update;
|
|
|
|
|
|
|
|
|
|
CreateResource = Create;
|
|
|
|
|
|
|
|
|
|
DeleteResource = DeleteProfile;
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
private QualityProfileResource Create(QualityProfileResource resource)
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-05-31 00:12:20 +00:00
|
|
|
|
var model = resource.InjectTo<QualityProfile>();
|
|
|
|
|
model = _qualityProvider.Add(model);
|
|
|
|
|
return GetById(model.Id);
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
private void DeleteProfile(int id)
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-05-31 00:12:20 +00:00
|
|
|
|
_qualityProvider.Delete(id);
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
private QualityProfileResource Update(QualityProfileResource resource)
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-05-31 00:12:20 +00:00
|
|
|
|
var model = resource.InjectTo<QualityProfile>();
|
|
|
|
|
_qualityProvider.Update(model);
|
|
|
|
|
return GetById(resource.Id);
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
private QualityProfileResource GetById(int id)
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-05-31 01:22:28 +00:00
|
|
|
|
return QualityToResource(_qualityProvider.Get(id));
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
private List<QualityProfileResource> GetAll()
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-05-31 01:22:28 +00:00
|
|
|
|
var allProfiles = _qualityProvider.All();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var profiles = allProfiles.Select(QualityToResource).ToList();
|
|
|
|
|
|
|
|
|
|
return profiles;
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
2013-05-31 00:12:20 +00:00
|
|
|
|
|
2013-05-31 01:22:28 +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>>(),
|
|
|
|
|
|
2013-05-31 01:22:28 +00:00
|
|
|
|
Allowed = profile.Allowed.InjectTo<List<QualityResource>>(),
|
|
|
|
|
Name = profile.Name,
|
|
|
|
|
Id = profile.Id
|
|
|
|
|
};
|
|
|
|
|
}
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|