diff --git a/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs b/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs index 95283449c..876f99a1f 100644 --- a/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs +++ b/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs @@ -76,9 +76,9 @@ namespace NzbDrone.Core.Test.ImportList .Setup(v => v.Enabled(It.IsAny())) .Returns(_importLists); - Mocker.GetMock() - .Setup(v => v.GetAllExclusions()) - .Returns(new List()); + Mocker.GetMock() + .Setup(v => v.All()) + .Returns(new List()); Mocker.GetMock() .Setup(v => v.MovieExists(It.IsAny())) @@ -167,8 +167,8 @@ namespace NzbDrone.Core.Test.ImportList Mocker.GetMock() .Verify(v => v.UpdateMovie(new List(), true), Times.Never()); - Mocker.GetMock() - .Verify(v => v.GetAllExclusions(), Times.Never); + Mocker.GetMock() + .Verify(v => v.All(), Times.Never); } [Test] @@ -427,9 +427,9 @@ namespace NzbDrone.Core.Test.ImportList GivenCleanLevel("disabled"); - Mocker.GetMock() - .Setup(v => v.GetAllExclusions()) - .Returns(new List { new ImportExclusion { TmdbId = _existingMovies[0].TmdbId } }); + Mocker.GetMock() + .Setup(v => v.All()) + .Returns(new List { new ImportListExclusion { TmdbId = _existingMovies[0].TmdbId } }); Subject.Execute(_commandAll); diff --git a/src/NzbDrone.Core/Datastore/TableMapping.cs b/src/NzbDrone.Core/Datastore/TableMapping.cs index 04e8bf792..4f179691b 100644 --- a/src/NzbDrone.Core/Datastore/TableMapping.cs +++ b/src/NzbDrone.Core/Datastore/TableMapping.cs @@ -134,7 +134,7 @@ namespace NzbDrone.Core.Datastore Mapper.Entity("Credits").RegisterModel(); - Mapper.Entity("ImportExclusions").RegisterModel(); + Mapper.Entity("ImportExclusions").RegisterModel(); Mapper.Entity("QualityDefinitions").RegisterModel() .Ignore(d => d.GroupName) diff --git a/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusionsService.cs b/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusionsService.cs deleted file mode 100644 index c94689691..000000000 --- a/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusionsService.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using NLog; -using NzbDrone.Core.Messaging.Events; -using NzbDrone.Core.Movies.Events; - -namespace NzbDrone.Core.ImportLists.ImportExclusions -{ - public interface IImportExclusionsService - { - List GetAllExclusions(); - bool IsMovieExcluded(int tmdbId); - ImportExclusion AddExclusion(ImportExclusion exclusion); - List AddExclusions(List exclusions); - void RemoveExclusion(ImportExclusion exclusion); - ImportExclusion GetById(int id); - ImportExclusion Update(ImportExclusion exclusion); - } - - public class ImportExclusionsService : IImportExclusionsService, IHandleAsync - { - private readonly IImportExclusionsRepository _exclusionRepository; - private readonly Logger _logger; - - public ImportExclusionsService(IImportExclusionsRepository exclusionRepository, - Logger logger) - { - _exclusionRepository = exclusionRepository; - _logger = logger; - } - - public ImportExclusion AddExclusion(ImportExclusion exclusion) - { - if (_exclusionRepository.IsMovieExcluded(exclusion.TmdbId)) - { - return _exclusionRepository.GetByTmdbid(exclusion.TmdbId); - } - - return _exclusionRepository.Insert(exclusion); - } - - public List AddExclusions(List exclusions) - { - _exclusionRepository.InsertMany(DeDupeExclusions(exclusions)); - - return exclusions; - } - - public List GetAllExclusions() - { - return _exclusionRepository.All().ToList(); - } - - public bool IsMovieExcluded(int tmdbId) - { - return _exclusionRepository.IsMovieExcluded(tmdbId); - } - - public void RemoveExclusion(ImportExclusion exclusion) - { - _exclusionRepository.Delete(exclusion); - } - - public ImportExclusion GetById(int id) - { - return _exclusionRepository.Get(id); - } - - public ImportExclusion Update(ImportExclusion exclusion) - { - return _exclusionRepository.Update(exclusion); - } - - public void HandleAsync(MoviesDeletedEvent message) - { - if (message.AddExclusion) - { - _logger.Debug("Adding {0} Deleted Movies to Import Exclusions", message.Movies.Count); - - var exclusions = message.Movies.Select(m => new ImportExclusion { TmdbId = m.TmdbId, MovieTitle = m.Title, MovieYear = m.Year }).ToList(); - _exclusionRepository.InsertMany(DeDupeExclusions(exclusions)); - } - } - - private List DeDupeExclusions(List exclusions) - { - var existingExclusions = _exclusionRepository.AllExcludedTmdbIds(); - - return exclusions - .DistinctBy(x => x.TmdbId) - .Where(x => !existingExclusions.Contains(x.TmdbId)) - .ToList(); - } - } -} diff --git a/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusion.cs b/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusion.cs similarity index 88% rename from src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusion.cs rename to src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusion.cs index 6ffa61a17..ffd26ee5d 100644 --- a/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusion.cs +++ b/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusion.cs @@ -2,7 +2,7 @@ namespace NzbDrone.Core.ImportLists.ImportExclusions { - public class ImportExclusion : ModelBase + public class ImportListExclusion : ModelBase { public int TmdbId { get; set; } public string MovieTitle { get; set; } diff --git a/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusionsRepository.cs b/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusionRepository.cs similarity index 59% rename from src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusionsRepository.cs rename to src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusionRepository.cs index 1241f8c0c..a6719afd6 100644 --- a/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportExclusionsRepository.cs +++ b/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusionRepository.cs @@ -6,16 +6,16 @@ using NzbDrone.Core.Messaging.Events; namespace NzbDrone.Core.ImportLists.ImportExclusions { - public interface IImportExclusionsRepository : IBasicRepository + public interface IImportListExclusionRepository : IBasicRepository { bool IsMovieExcluded(int tmdbid); - ImportExclusion GetByTmdbid(int tmdbid); + ImportListExclusion FindByTmdbid(int tmdbid); List AllExcludedTmdbIds(); } - public class ImportExclusionsRepository : BasicRepository, IImportExclusionsRepository + public class ImportListListExclusionRepository : BasicRepository, IImportListExclusionRepository { - public ImportExclusionsRepository(IMainDatabase database, IEventAggregator eventAggregator) + public ImportListListExclusionRepository(IMainDatabase database, IEventAggregator eventAggregator) : base(database, eventAggregator) { } @@ -25,9 +25,9 @@ namespace NzbDrone.Core.ImportLists.ImportExclusions return Query(x => x.TmdbId == tmdbid).Any(); } - public ImportExclusion GetByTmdbid(int tmdbid) + public ImportListExclusion FindByTmdbid(int tmdbid) { - return Query(x => x.TmdbId == tmdbid).First(); + return Query(x => x.TmdbId == tmdbid).SingleOrDefault(); } public List AllExcludedTmdbIds() diff --git a/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusionService.cs b/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusionService.cs new file mode 100644 index 000000000..54781bf48 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/ImportExclusions/ImportListExclusionService.cs @@ -0,0 +1,115 @@ +using System.Collections.Generic; +using System.Linq; +using NLog; +using NzbDrone.Core.Messaging.Events; +using NzbDrone.Core.Movies.Events; + +namespace NzbDrone.Core.ImportLists.ImportExclusions +{ + public interface IImportListExclusionService + { + ImportListExclusion Add(ImportListExclusion importListExclusion); + List Add(List importListExclusions); + List All(); + bool IsMovieExcluded(int tmdbId); + void Delete(int id); + void Delete(List ids); + ImportListExclusion Get(int id); + ImportListExclusion Update(ImportListExclusion importListExclusion); + List AllExcludedTmdbIds(); + } + + public class ImportListExclusionService : IImportListExclusionService, IHandleAsync + { + private readonly IImportListExclusionRepository _repo; + private readonly Logger _logger; + + public ImportListExclusionService(IImportListExclusionRepository repo, + Logger logger) + { + _repo = repo; + _logger = logger; + } + + public ImportListExclusion Add(ImportListExclusion importListExclusion) + { + if (_repo.IsMovieExcluded(importListExclusion.TmdbId)) + { + return _repo.FindByTmdbid(importListExclusion.TmdbId); + } + + return _repo.Insert(importListExclusion); + } + + public List Add(List importListExclusions) + { + _repo.InsertMany(DeDupeExclusions(importListExclusions)); + + return importListExclusions; + } + + public bool IsMovieExcluded(int tmdbId) + { + return _repo.IsMovieExcluded(tmdbId); + } + + public void Delete(int id) + { + _repo.Delete(id); + } + + public void Delete(List ids) + { + _repo.DeleteMany(ids); + } + + public ImportListExclusion Get(int id) + { + return _repo.Get(id); + } + + public ImportListExclusion Update(ImportListExclusion importListExclusion) + { + return _repo.Update(importListExclusion); + } + + public List All() + { + return _repo.All().ToList(); + } + + public List AllExcludedTmdbIds() + { + return _repo.AllExcludedTmdbIds(); + } + + public void HandleAsync(MoviesDeletedEvent message) + { + if (!message.AddImportListExclusion) + { + return; + } + + _logger.Debug("Adding {0} deleted movies to import list exclusions.", message.Movies.Count); + + var exclusionsToAdd = DeDupeExclusions(message.Movies.Select(m => new ImportListExclusion + { + TmdbId = m.TmdbId, + MovieTitle = m.Title, + MovieYear = m.Year + }).ToList()); + + _repo.InsertMany(exclusionsToAdd); + } + + private List DeDupeExclusions(List exclusions) + { + var existingExclusions = _repo.AllExcludedTmdbIds(); + + return exclusions + .DistinctBy(x => x.TmdbId) + .Where(x => !existingExclusions.Contains(x.TmdbId)) + .ToList(); + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs b/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs index 94c1d0792..60c118a6a 100644 --- a/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs +++ b/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs @@ -19,7 +19,7 @@ namespace NzbDrone.Core.ImportLists private readonly IMovieService _movieService; private readonly IAddMovieService _addMovieService; private readonly IConfigService _configService; - private readonly IImportExclusionsService _exclusionService; + private readonly IImportListExclusionService _listExclusionService; private readonly IImportListMovieService _listMovieService; public ImportListSyncService(IImportListFactory importListFactory, @@ -27,7 +27,7 @@ namespace NzbDrone.Core.ImportLists IMovieService movieService, IAddMovieService addMovieService, IConfigService configService, - IImportExclusionsService exclusionService, + IImportListExclusionService listExclusionService, IImportListMovieService listMovieService, Logger logger) { @@ -35,7 +35,7 @@ namespace NzbDrone.Core.ImportLists _listFetcherAndParser = listFetcherAndParser; _movieService = movieService; _addMovieService = addMovieService; - _exclusionService = exclusionService; + _listExclusionService = listExclusionService; _listMovieService = listMovieService; _logger = logger; _configService = configService; @@ -74,7 +74,7 @@ namespace NzbDrone.Core.ImportLists ProcessListItems(listItemsResult); } - private void ProcessMovieReport(ImportListDefinition importList, ImportListMovie report, List listExclusions, List dbMovies, List moviesToAdd) + private void ProcessMovieReport(ImportListDefinition importList, ImportListMovie report, List listExclusions, List dbMovies, List moviesToAdd) { if (report.TmdbId == 0 || !importList.EnableAuto) { @@ -142,7 +142,7 @@ namespace NzbDrone.Core.ImportLists var listedMovies = listFetchResult.Movies.ToList(); - var importExclusions = _exclusionService.GetAllExclusions(); + var importExclusions = _listExclusionService.All(); var dbMovies = _movieService.AllMovieTmdbIds(); var moviesToAdd = new List(); diff --git a/src/NzbDrone.Core/Movies/Events/MoviesDeletedEvent.cs b/src/NzbDrone.Core/Movies/Events/MoviesDeletedEvent.cs index 2f3c6dfc5..911311bc3 100644 --- a/src/NzbDrone.Core/Movies/Events/MoviesDeletedEvent.cs +++ b/src/NzbDrone.Core/Movies/Events/MoviesDeletedEvent.cs @@ -7,13 +7,13 @@ namespace NzbDrone.Core.Movies.Events { public List Movies { get; private set; } public bool DeleteFiles { get; private set; } - public bool AddExclusion { get; private set; } + public bool AddImportListExclusion { get; private set; } - public MoviesDeletedEvent(List movies, bool deleteFiles, bool addExclusion) + public MoviesDeletedEvent(List movies, bool deleteFiles, bool addImportListExclusion) { Movies = movies; DeleteFiles = deleteFiles; - AddExclusion = addExclusion; + AddImportListExclusion = addImportListExclusion; } } } diff --git a/src/NzbDrone.Core/Movies/MovieService.cs b/src/NzbDrone.Core/Movies/MovieService.cs index f109f2fcb..c2df5bed7 100644 --- a/src/NzbDrone.Core/Movies/MovieService.cs +++ b/src/NzbDrone.Core/Movies/MovieService.cs @@ -36,8 +36,8 @@ namespace NzbDrone.Core.Movies List GetMoviesByCollectionTmdbId(int collectionId); List GetMoviesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored); PagingSpec MoviesWithoutFiles(PagingSpec pagingSpec); - void DeleteMovie(int movieId, bool deleteFiles, bool addExclusion = false); - void DeleteMovies(List movieIds, bool deleteFiles, bool addExclusion = false); + void DeleteMovie(int movieId, bool deleteFiles, bool addImportListExclusion = false); + void DeleteMovies(List movieIds, bool deleteFiles, bool addImportListExclusion = false); List GetAllMovies(); Dictionary> AllMovieTags(); Movie UpdateMovie(Movie movie); @@ -210,22 +210,22 @@ namespace NzbDrone.Core.Movies return _movieRepository.AllMovieTmdbIds(); } - public void DeleteMovie(int movieId, bool deleteFiles, bool addExclusion = false) + public void DeleteMovie(int movieId, bool deleteFiles, bool addImportListExclusion = false) { var movie = _movieRepository.Get(movieId); _movieRepository.Delete(movieId); - _eventAggregator.PublishEvent(new MoviesDeletedEvent(new List { movie }, deleteFiles, addExclusion)); + _eventAggregator.PublishEvent(new MoviesDeletedEvent(new List { movie }, deleteFiles, addImportListExclusion)); _logger.Info("Deleted movie {0}", movie); } - public void DeleteMovies(List movieIds, bool deleteFiles, bool addExclusion = false) + public void DeleteMovies(List movieIds, bool deleteFiles, bool addImportListExclusion = false) { var moviesToDelete = _movieRepository.Get(movieIds).ToList(); _movieRepository.DeleteMany(movieIds); - _eventAggregator.PublishEvent(new MoviesDeletedEvent(moviesToDelete, deleteFiles, addExclusion)); + _eventAggregator.PublishEvent(new MoviesDeletedEvent(moviesToDelete, deleteFiles, addImportListExclusion)); foreach (var movie in moviesToDelete) { diff --git a/src/NzbDrone.Core/Movies/RefreshCollectionService.cs b/src/NzbDrone.Core/Movies/RefreshCollectionService.cs index c8b9d1803..4608225a4 100644 --- a/src/NzbDrone.Core/Movies/RefreshCollectionService.cs +++ b/src/NzbDrone.Core/Movies/RefreshCollectionService.cs @@ -21,7 +21,7 @@ namespace NzbDrone.Core.Movies private readonly IMovieService _movieService; private readonly IMovieMetadataService _movieMetadataService; private readonly IAddMovieService _addMovieService; - private readonly IImportExclusionsService _importExclusionService; + private readonly IImportListExclusionService _importListExclusionService; private readonly IEventAggregator _eventAggregator; private readonly Logger _logger; @@ -31,7 +31,7 @@ namespace NzbDrone.Core.Movies IMovieService movieService, IMovieMetadataService movieMetadataService, IAddMovieService addMovieService, - IImportExclusionsService importExclusionsService, + IImportListExclusionService importListExclusionService, IEventAggregator eventAggregator, Logger logger) { @@ -40,7 +40,7 @@ namespace NzbDrone.Core.Movies _movieService = movieService; _movieMetadataService = movieMetadataService; _addMovieService = addMovieService; - _importExclusionService = importExclusionsService; + _importListExclusionService = importListExclusionService; _eventAggregator = eventAggregator; _logger = logger; } @@ -127,7 +127,7 @@ namespace NzbDrone.Core.Movies { var existingMovies = _movieService.AllMovieTmdbIds(); var collectionMovies = _movieMetadataService.GetMoviesByCollectionTmdbId(collection.TmdbId); - var excludedMovies = _importExclusionService.GetAllExclusions().Select(e => e.TmdbId); + var excludedMovies = _importListExclusionService.All().Select(e => e.TmdbId); var moviesToAdd = collectionMovies.Where(m => !existingMovies.Contains(m.TmdbId)).Where(m => !excludedMovies.Contains(m.TmdbId)); if (moviesToAdd.Any()) diff --git a/src/Radarr.Api.V3/Collections/CollectionController.cs b/src/Radarr.Api.V3/Collections/CollectionController.cs index 98f71d1da..3478d6dd0 100644 --- a/src/Radarr.Api.V3/Collections/CollectionController.cs +++ b/src/Radarr.Api.V3/Collections/CollectionController.cs @@ -31,7 +31,7 @@ namespace Radarr.Api.V3.Collections private readonly IMovieService _movieService; private readonly IMovieMetadataService _movieMetadataService; private readonly IMovieTranslationService _movieTranslationService; - private readonly IImportExclusionsService _importExclusionService; + private readonly IImportListExclusionService _importListExclusionService; private readonly IConfigService _configService; private readonly IBuildFileNames _fileNameBuilder; private readonly INamingConfigService _namingService; @@ -42,7 +42,7 @@ namespace Radarr.Api.V3.Collections IMovieService movieService, IMovieMetadataService movieMetadataService, IMovieTranslationService movieTranslationService, - IImportExclusionsService importExclusionsService, + IImportListExclusionService importListExclusionService, IConfigService configService, IBuildFileNames fileNameBuilder, INamingConfigService namingService, @@ -53,7 +53,7 @@ namespace Radarr.Api.V3.Collections _movieService = movieService; _movieMetadataService = movieMetadataService; _movieTranslationService = movieTranslationService; - _importExclusionService = importExclusionsService; + _importListExclusionService = importListExclusionService; _configService = configService; _fileNameBuilder = fileNameBuilder; _namingService = namingService; @@ -158,7 +158,7 @@ namespace Radarr.Api.V3.Collections var configLanguage = (Language)_configService.MovieInfoLanguage; var existingMoviesTmdbIds = _movieService.AllMovieWithCollectionsTmdbIds(); - var listExclusions = _importExclusionService.GetAllExclusions(); + var listExclusions = _importListExclusionService.All(); var allCollectionMovies = _movieMetadataService.GetMoviesWithCollections() .GroupBy(x => x.CollectionTmdbId) @@ -209,7 +209,7 @@ namespace Radarr.Api.V3.Collections var configLanguage = (Language)_configService.MovieInfoLanguage; var existingMoviesTmdbIds = _movieService.AllMovieWithCollectionsTmdbIds(); - var listExclusions = _importExclusionService.GetAllExclusions(); + var listExclusions = _importListExclusionService.All(); foreach (var movie in _movieMetadataService.GetMoviesByCollectionTmdbId(collection.TmdbId)) { diff --git a/src/Radarr.Api.V3/ImportLists/ImportExclusionsController.cs b/src/Radarr.Api.V3/ImportLists/ImportExclusionsController.cs deleted file mode 100644 index e36cb6cdb..000000000 --- a/src/Radarr.Api.V3/ImportLists/ImportExclusionsController.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.Collections.Generic; -using FluentValidation; -using Microsoft.AspNetCore.Mvc; -using NzbDrone.Core.ImportLists.ImportExclusions; -using Radarr.Http; -using Radarr.Http.REST; -using Radarr.Http.REST.Attributes; - -namespace Radarr.Api.V3.ImportLists -{ - [V3ApiController("exclusions")] - public class ImportExclusionsController : RestController - { - private readonly IImportExclusionsService _exclusionService; - - public ImportExclusionsController(IImportExclusionsService exclusionService) - { - _exclusionService = exclusionService; - - SharedValidator.RuleFor(c => c.TmdbId).GreaterThan(0); - SharedValidator.RuleFor(c => c.MovieTitle).NotEmpty(); - SharedValidator.RuleFor(c => c.MovieYear).GreaterThan(0); - } - - [HttpGet] - public List GetAll() - { - return _exclusionService.GetAllExclusions().ToResource(); - } - - protected override ImportExclusionsResource GetResourceById(int id) - { - return _exclusionService.GetById(id).ToResource(); - } - - [RestPutById] - [Consumes("application/json")] - public ActionResult UpdateExclusion([FromBody] ImportExclusionsResource exclusionResource) - { - var model = exclusionResource.ToModel(); - return Accepted(_exclusionService.Update(model)); - } - - [RestPostById] - [Consumes("application/json")] - public ActionResult AddExclusion([FromBody] ImportExclusionsResource exclusionResource) - { - var model = exclusionResource.ToModel(); - - return Created(_exclusionService.AddExclusion(model).Id); - } - - [HttpPost("bulk")] - public object AddExclusions([FromBody] List resource) - { - var newMovies = resource.ToModel(); - - return _exclusionService.AddExclusions(newMovies).ToResource(); - } - - [RestDeleteById] - public void RemoveExclusion(int id) - { - _exclusionService.RemoveExclusion(new ImportExclusion { Id = id }); - } - } -} diff --git a/src/Radarr.Api.V3/ImportLists/ImportListExclusionBulkResource.cs b/src/Radarr.Api.V3/ImportLists/ImportListExclusionBulkResource.cs new file mode 100644 index 000000000..1f50c9da6 --- /dev/null +++ b/src/Radarr.Api.V3/ImportLists/ImportListExclusionBulkResource.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Radarr.Api.V3.ImportLists +{ + public class ImportListExclusionBulkResource + { + public HashSet Ids { get; set; } + } +} diff --git a/src/Radarr.Api.V3/ImportLists/ImportListExclusionController.cs b/src/Radarr.Api.V3/ImportLists/ImportListExclusionController.cs new file mode 100644 index 000000000..409480831 --- /dev/null +++ b/src/Radarr.Api.V3/ImportLists/ImportListExclusionController.cs @@ -0,0 +1,78 @@ +using System.Collections.Generic; +using System.Linq; +using FluentValidation; +using Microsoft.AspNetCore.Mvc; +using NzbDrone.Core.ImportLists.ImportExclusions; +using Radarr.Http; +using Radarr.Http.REST; +using Radarr.Http.REST.Attributes; + +namespace Radarr.Api.V3.ImportLists +{ + [V3ApiController("exclusions")] + public class ImportListExclusionController : RestController + { + private readonly IImportListExclusionService _importListExclusionService; + + public ImportListExclusionController(IImportListExclusionService importListExclusionService) + { + _importListExclusionService = importListExclusionService; + + SharedValidator.RuleFor(c => c.TmdbId).GreaterThan(0); + SharedValidator.RuleFor(c => c.MovieTitle).NotEmpty(); + SharedValidator.RuleFor(c => c.MovieYear).GreaterThan(0); + } + + [HttpGet] + [Produces("application/json")] + public List GetImportListExclusions() + { + return _importListExclusionService.All().ToResource(); + } + + protected override ImportListExclusionResource GetResourceById(int id) + { + return _importListExclusionService.Get(id).ToResource(); + } + + [RestPostById] + [Consumes("application/json")] + public ActionResult AddImportListExclusion([FromBody] ImportListExclusionResource resource) + { + var importListExclusion = _importListExclusionService.Add(resource.ToModel()); + + return Created(importListExclusion.Id); + } + + [RestPutById] + [Consumes("application/json")] + public ActionResult UpdateImportListExclusion([FromBody] ImportListExclusionResource resource) + { + _importListExclusionService.Update(resource.ToModel()); + return Accepted(resource.Id); + } + + [HttpPost("bulk")] + public object AddImportListExclusions([FromBody] List resources) + { + var importListExclusions = _importListExclusionService.Add(resources.ToModel()); + + return importListExclusions.ToResource(); + } + + [RestDeleteById] + public void DeleteImportListExclusion(int id) + { + _importListExclusionService.Delete(id); + } + + [HttpDelete("bulk")] + [Produces("application/json")] + public object DeleteImportListExclusions([FromBody] ImportListExclusionBulkResource resource) + { + _importListExclusionService.Delete(resource.Ids.ToList()); + + return new { }; + } + } +} diff --git a/src/Radarr.Api.V3/ImportLists/ImportExclusionsResource.cs b/src/Radarr.Api.V3/ImportLists/ImportListExclusionResource.cs similarity index 60% rename from src/Radarr.Api.V3/ImportLists/ImportExclusionsResource.cs rename to src/Radarr.Api.V3/ImportLists/ImportListExclusionResource.cs index ac8f48304..348f94d88 100644 --- a/src/Radarr.Api.V3/ImportLists/ImportExclusionsResource.cs +++ b/src/Radarr.Api.V3/ImportLists/ImportListExclusionResource.cs @@ -4,7 +4,7 @@ using NzbDrone.Core.ImportLists.ImportExclusions; namespace Radarr.Api.V3.ImportLists { - public class ImportExclusionsResource : ProviderResource + public class ImportListExclusionResource : ProviderResource { // public int Id { get; set; } public int TmdbId { get; set; } @@ -12,16 +12,16 @@ namespace Radarr.Api.V3.ImportLists public int MovieYear { get; set; } } - public static class ImportExclusionsResourceMapper + public static class ImportListExclusionResourceMapper { - public static ImportExclusionsResource ToResource(this ImportExclusion model) + public static ImportListExclusionResource ToResource(this ImportListExclusion model) { if (model == null) { return null; } - return new ImportExclusionsResource + return new ImportListExclusionResource { Id = model.Id, TmdbId = model.TmdbId, @@ -30,14 +30,14 @@ namespace Radarr.Api.V3.ImportLists }; } - public static List ToResource(this IEnumerable exclusions) + public static List ToResource(this IEnumerable exclusions) { return exclusions.Select(ToResource).ToList(); } - public static ImportExclusion ToModel(this ImportExclusionsResource resource) + public static ImportListExclusion ToModel(this ImportListExclusionResource resource) { - return new ImportExclusion + return new ImportListExclusion { Id = resource.Id, TmdbId = resource.TmdbId, @@ -46,7 +46,7 @@ namespace Radarr.Api.V3.ImportLists }; } - public static List ToModel(this IEnumerable resources) + public static List ToModel(this IEnumerable resources) { return resources.Select(ToModel).ToList(); } diff --git a/src/Radarr.Api.V3/ImportLists/ImportListMoviesController.cs b/src/Radarr.Api.V3/ImportLists/ImportListMoviesController.cs index 751febd20..7257f8231 100644 --- a/src/Radarr.Api.V3/ImportLists/ImportListMoviesController.cs +++ b/src/Radarr.Api.V3/ImportLists/ImportListMoviesController.cs @@ -25,7 +25,7 @@ namespace Radarr.Api.V3.ImportLists private readonly IBuildFileNames _fileNameBuilder; private readonly IImportListMovieService _listMovieService; private readonly IImportListFactory _importListFactory; - private readonly IImportExclusionsService _importExclusionService; + private readonly IImportListExclusionService _importListExclusionService; private readonly INamingConfigService _namingService; private readonly IMovieTranslationService _movieTranslationService; private readonly IConfigService _configService; @@ -36,7 +36,7 @@ namespace Radarr.Api.V3.ImportLists IBuildFileNames fileNameBuilder, IImportListMovieService listMovieService, IImportListFactory importListFactory, - IImportExclusionsService importExclusionsService, + IImportListExclusionService importListExclusionService, INamingConfigService namingService, IMovieTranslationService movieTranslationService, IConfigService configService) @@ -47,7 +47,7 @@ namespace Radarr.Api.V3.ImportLists _fileNameBuilder = fileNameBuilder; _listMovieService = listMovieService; _importListFactory = importListFactory; - _importExclusionService = importExclusionsService; + _importListExclusionService = importListExclusionService; _namingService = namingService; _movieTranslationService = movieTranslationService; _configService = configService; @@ -59,7 +59,7 @@ namespace Radarr.Api.V3.ImportLists var movieLanguage = (Language)_configService.MovieInfoLanguage; var realResults = new List(); - var listExclusions = _importExclusionService.GetAllExclusions(); + var listExclusions = _importListExclusionService.All(); var existingTmdbIds = _movieService.AllMovieTmdbIds(); if (includeRecommendations)