2018-11-23 07:03:32 +00:00
|
|
|
using System.Collections.Generic;
|
2018-09-11 12:35:15 +00:00
|
|
|
using System.Linq;
|
2014-06-08 08:22:55 +00:00
|
|
|
using FluentValidation;
|
2018-09-11 12:35:15 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Core.CustomFormats;
|
2014-06-08 08:22:55 +00:00
|
|
|
using NzbDrone.Core.Profiles;
|
2018-11-23 07:03:32 +00:00
|
|
|
using Radarr.Http;
|
2014-06-08 08:22:55 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.Profiles
|
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
public class ProfileModule : RadarrRestModule<ProfileResource>
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
private readonly IProfileService _profileService;
|
2018-09-11 12:35:15 +00:00
|
|
|
private readonly ICustomFormatService _formatService;
|
2014-06-08 08:22:55 +00:00
|
|
|
|
2018-09-11 12:35:15 +00:00
|
|
|
public ProfileModule(IProfileService profileService, ICustomFormatService formatService)
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
_profileService = profileService;
|
2018-09-11 12:35:15 +00:00
|
|
|
_formatService = formatService;
|
2014-06-08 08:22:55 +00:00
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
|
|
|
SharedValidator.RuleFor(c => c.Cutoff).NotNull();
|
|
|
|
SharedValidator.RuleFor(c => c.Items).MustHaveAllowedQuality();
|
2018-09-11 12:35:15 +00:00
|
|
|
SharedValidator.RuleFor(c => c.FormatItems).Must(items =>
|
|
|
|
{
|
|
|
|
var all = _formatService.All().Select(f => f.Id).ToList();
|
2018-09-11 21:52:07 +00:00
|
|
|
var ids = items.Select(i => i.Format.Id);
|
2018-09-11 12:35:15 +00:00
|
|
|
|
|
|
|
return all.Except(ids).Empty();
|
|
|
|
}).WithMessage("All Custom Formats and no extra ones need to be present inside your Profile! Try refreshing your browser.");
|
2014-06-08 08:22:55 +00:00
|
|
|
|
|
|
|
GetResourceAll = GetAll;
|
|
|
|
GetResourceById = GetById;
|
|
|
|
UpdateResource = Update;
|
|
|
|
CreateResource = Create;
|
|
|
|
DeleteResource = DeleteProfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int Create(ProfileResource resource)
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
var model = resource.ToModel();
|
|
|
|
|
|
|
|
return _profileService.Add(model).Id;
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteProfile(int id)
|
|
|
|
{
|
|
|
|
_profileService.Delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Update(ProfileResource resource)
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
var model = resource.ToModel();
|
2014-06-08 08:22:55 +00:00
|
|
|
|
|
|
|
_profileService.Update(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ProfileResource GetById(int id)
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
return _profileService.Get(id).ToResource();
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<ProfileResource> GetAll()
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
return _profileService.All().ToResource();
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 21:52:07 +00:00
|
|
|
}
|