1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Api/Qualities/QualityProfileModule.cs

87 lines
2.5 KiB
C#
Raw Normal View History

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