2014-06-08 08:22:55 +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 ;
using NzbDrone.Core.Validation ;
namespace NzbDrone.Api.Profiles
{
public class ProfileModule : NzbDroneRestModule < ProfileResource >
{
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 ( ) ;
SharedValidator . RuleFor ( c = > c . Language ) . ValidLanguage ( ) ;
2018-09-11 12:35:15 +00:00
SharedValidator . RuleFor ( c = > c . FormatItems ) . Must ( items = >
{
var all = _formatService . All ( ) . Select ( f = > f . Id ) . ToList ( ) ;
all . Add ( CustomFormat . None . Id ) ;
var ids = items . Select ( i = > i . Id ) ;
return all . Except ( ids ) . Empty ( ) ;
} ) . WithMessage ( "All Custom Formats and no extra ones need to be present inside your Profile! Try refreshing your browser." ) ;
SharedValidator . RuleFor ( c = > c . FormatCutoff )
. Must ( c = > _formatService . All ( ) . Select ( f = > f . Id ) . Contains ( c . Id ) | | c . Id = = CustomFormat . None . Id ) . WithMessage ( "The Custom Format Cutoff must be a valid Custom Format! 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
}
}
}