parent
f76c97c3ce
commit
b1a7652753
@ -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<ImportExclusion> GetAllExclusions();
|
||||
bool IsMovieExcluded(int tmdbId);
|
||||
ImportExclusion AddExclusion(ImportExclusion exclusion);
|
||||
List<ImportExclusion> AddExclusions(List<ImportExclusion> exclusions);
|
||||
void RemoveExclusion(ImportExclusion exclusion);
|
||||
ImportExclusion GetById(int id);
|
||||
ImportExclusion Update(ImportExclusion exclusion);
|
||||
}
|
||||
|
||||
public class ImportExclusionsService : IImportExclusionsService, IHandleAsync<MoviesDeletedEvent>
|
||||
{
|
||||
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<ImportExclusion> AddExclusions(List<ImportExclusion> exclusions)
|
||||
{
|
||||
_exclusionRepository.InsertMany(DeDupeExclusions(exclusions));
|
||||
|
||||
return exclusions;
|
||||
}
|
||||
|
||||
public List<ImportExclusion> 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<ImportExclusion> DeDupeExclusions(List<ImportExclusion> exclusions)
|
||||
{
|
||||
var existingExclusions = _exclusionRepository.AllExcludedTmdbIds();
|
||||
|
||||
return exclusions
|
||||
.DistinctBy(x => x.TmdbId)
|
||||
.Where(x => !existingExclusions.Contains(x.TmdbId))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ImportListExclusion> Add(List<ImportListExclusion> importListExclusions);
|
||||
List<ImportListExclusion> All();
|
||||
bool IsMovieExcluded(int tmdbId);
|
||||
void Delete(int id);
|
||||
void Delete(List<int> ids);
|
||||
ImportListExclusion Get(int id);
|
||||
ImportListExclusion Update(ImportListExclusion importListExclusion);
|
||||
List<int> AllExcludedTmdbIds();
|
||||
}
|
||||
|
||||
public class ImportListExclusionService : IImportListExclusionService, IHandleAsync<MoviesDeletedEvent>
|
||||
{
|
||||
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<ImportListExclusion> Add(List<ImportListExclusion> 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<int> ids)
|
||||
{
|
||||
_repo.DeleteMany(ids);
|
||||
}
|
||||
|
||||
public ImportListExclusion Get(int id)
|
||||
{
|
||||
return _repo.Get(id);
|
||||
}
|
||||
|
||||
public ImportListExclusion Update(ImportListExclusion importListExclusion)
|
||||
{
|
||||
return _repo.Update(importListExclusion);
|
||||
}
|
||||
|
||||
public List<ImportListExclusion> All()
|
||||
{
|
||||
return _repo.All().ToList();
|
||||
}
|
||||
|
||||
public List<int> 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<ImportListExclusion> DeDupeExclusions(List<ImportListExclusion> exclusions)
|
||||
{
|
||||
var existingExclusions = _repo.AllExcludedTmdbIds();
|
||||
|
||||
return exclusions
|
||||
.DistinctBy(x => x.TmdbId)
|
||||
.Where(x => !existingExclusions.Contains(x.TmdbId))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ImportExclusionsResource>
|
||||
{
|
||||
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<ImportExclusionsResource> GetAll()
|
||||
{
|
||||
return _exclusionService.GetAllExclusions().ToResource();
|
||||
}
|
||||
|
||||
protected override ImportExclusionsResource GetResourceById(int id)
|
||||
{
|
||||
return _exclusionService.GetById(id).ToResource();
|
||||
}
|
||||
|
||||
[RestPutById]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<ImportExclusionsResource> UpdateExclusion([FromBody] ImportExclusionsResource exclusionResource)
|
||||
{
|
||||
var model = exclusionResource.ToModel();
|
||||
return Accepted(_exclusionService.Update(model));
|
||||
}
|
||||
|
||||
[RestPostById]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<ImportExclusionsResource> AddExclusion([FromBody] ImportExclusionsResource exclusionResource)
|
||||
{
|
||||
var model = exclusionResource.ToModel();
|
||||
|
||||
return Created(_exclusionService.AddExclusion(model).Id);
|
||||
}
|
||||
|
||||
[HttpPost("bulk")]
|
||||
public object AddExclusions([FromBody] List<ImportExclusionsResource> resource)
|
||||
{
|
||||
var newMovies = resource.ToModel();
|
||||
|
||||
return _exclusionService.AddExclusions(newMovies).ToResource();
|
||||
}
|
||||
|
||||
[RestDeleteById]
|
||||
public void RemoveExclusion(int id)
|
||||
{
|
||||
_exclusionService.RemoveExclusion(new ImportExclusion { Id = id });
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ImportListExclusionResource>
|
||||
{
|
||||
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<ImportListExclusionResource> GetImportListExclusions()
|
||||
{
|
||||
return _importListExclusionService.All().ToResource();
|
||||
}
|
||||
|
||||
protected override ImportListExclusionResource GetResourceById(int id)
|
||||
{
|
||||
return _importListExclusionService.Get(id).ToResource();
|
||||
}
|
||||
|
||||
[RestPostById]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<ImportListExclusionResource> AddImportListExclusion([FromBody] ImportListExclusionResource resource)
|
||||
{
|
||||
var importListExclusion = _importListExclusionService.Add(resource.ToModel());
|
||||
|
||||
return Created(importListExclusion.Id);
|
||||
}
|
||||
|
||||
[RestPutById]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<ImportListExclusionResource> UpdateImportListExclusion([FromBody] ImportListExclusionResource resource)
|
||||
{
|
||||
_importListExclusionService.Update(resource.ToModel());
|
||||
return Accepted(resource.Id);
|
||||
}
|
||||
|
||||
[HttpPost("bulk")]
|
||||
public object AddImportListExclusions([FromBody] List<ImportListExclusionResource> 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 { };
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue