You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
193 lines
7.1 KiB
193 lines
7.1 KiB
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NzbDrone.Core.AutoTagging;
|
|
using NzbDrone.Core.Datastore;
|
|
using NzbDrone.Core.Download;
|
|
using NzbDrone.Core.ImportLists;
|
|
using NzbDrone.Core.Indexers;
|
|
using NzbDrone.Core.Messaging.Events;
|
|
using NzbDrone.Core.Movies;
|
|
using NzbDrone.Core.Notifications;
|
|
using NzbDrone.Core.Profiles.Delay;
|
|
using NzbDrone.Core.Restrictions;
|
|
|
|
namespace NzbDrone.Core.Tags
|
|
{
|
|
public interface ITagService
|
|
{
|
|
Tag GetTag(int tagId);
|
|
Tag GetTag(string tag);
|
|
List<Tag> GetTags(IEnumerable<int> ids);
|
|
TagDetails Details(int tagId);
|
|
List<TagDetails> Details();
|
|
List<Tag> All();
|
|
Tag Add(Tag tag);
|
|
Tag Update(Tag tag);
|
|
void Delete(int tagId);
|
|
}
|
|
|
|
public class TagService : ITagService
|
|
{
|
|
private readonly ITagRepository _repo;
|
|
private readonly IEventAggregator _eventAggregator;
|
|
private readonly IDelayProfileService _delayProfileService;
|
|
private readonly IImportListFactory _importListFactory;
|
|
private readonly INotificationFactory _notificationFactory;
|
|
private readonly IRestrictionService _restrictionService;
|
|
private readonly IMovieService _movieService;
|
|
private readonly IIndexerFactory _indexerService;
|
|
private readonly IAutoTaggingService _autoTaggingService;
|
|
private readonly IDownloadClientFactory _downloadClientFactory;
|
|
|
|
public TagService(ITagRepository repo,
|
|
IEventAggregator eventAggregator,
|
|
IDelayProfileService delayProfileService,
|
|
IImportListFactory importListFactory,
|
|
INotificationFactory notificationFactory,
|
|
IRestrictionService restrictionService,
|
|
IMovieService movieService,
|
|
IIndexerFactory indexerService,
|
|
IAutoTaggingService autoTaggingService,
|
|
IDownloadClientFactory downloadClientFactory)
|
|
{
|
|
_repo = repo;
|
|
_eventAggregator = eventAggregator;
|
|
_delayProfileService = delayProfileService;
|
|
_importListFactory = importListFactory;
|
|
_notificationFactory = notificationFactory;
|
|
_restrictionService = restrictionService;
|
|
_movieService = movieService;
|
|
_indexerService = indexerService;
|
|
_autoTaggingService = autoTaggingService;
|
|
_downloadClientFactory = downloadClientFactory;
|
|
}
|
|
|
|
public Tag GetTag(int tagId)
|
|
{
|
|
return _repo.Get(tagId);
|
|
}
|
|
|
|
public Tag GetTag(string tag)
|
|
{
|
|
if (tag.All(char.IsDigit))
|
|
{
|
|
return _repo.Get(int.Parse(tag));
|
|
}
|
|
else
|
|
{
|
|
return _repo.GetByLabel(tag);
|
|
}
|
|
}
|
|
|
|
public List<Tag> GetTags(IEnumerable<int> ids)
|
|
{
|
|
return _repo.Get(ids).ToList();
|
|
}
|
|
|
|
public TagDetails Details(int tagId)
|
|
{
|
|
var tag = GetTag(tagId);
|
|
var delayProfiles = _delayProfileService.AllForTag(tagId);
|
|
var importLists = _importListFactory.AllForTag(tagId);
|
|
var notifications = _notificationFactory.AllForTag(tagId);
|
|
var restrictions = _restrictionService.AllForTag(tagId);
|
|
var movies = _movieService.AllMovieTags().Where(x => x.Value.Contains(tagId)).Select(x => x.Key).ToList();
|
|
var indexers = _indexerService.AllForTag(tagId);
|
|
var autoTags = _autoTaggingService.AllForTag(tagId);
|
|
var downloadClients = _downloadClientFactory.AllForTag(tagId);
|
|
|
|
return new TagDetails
|
|
{
|
|
Id = tagId,
|
|
Label = tag.Label,
|
|
DelayProfileIds = delayProfiles.Select(c => c.Id).ToList(),
|
|
ImportListIds = importLists.Select(c => c.Id).ToList(),
|
|
NotificationIds = notifications.Select(c => c.Id).ToList(),
|
|
RestrictionIds = restrictions.Select(c => c.Id).ToList(),
|
|
MovieIds = movies,
|
|
IndexerIds = indexers.Select(c => c.Id).ToList(),
|
|
AutoTagIds = autoTags.Select(c => c.Id).ToList(),
|
|
DownloadClientIds = downloadClients.Select(c => c.Id).ToList()
|
|
};
|
|
}
|
|
|
|
public List<TagDetails> Details()
|
|
{
|
|
var tags = All();
|
|
var delayProfiles = _delayProfileService.All();
|
|
var importLists = _importListFactory.All();
|
|
var notifications = _notificationFactory.All();
|
|
var restrictions = _restrictionService.All();
|
|
var movies = _movieService.AllMovieTags();
|
|
var indexers = _indexerService.All();
|
|
var autotags = _autoTaggingService.All();
|
|
var downloadClients = _downloadClientFactory.All();
|
|
|
|
var details = new List<TagDetails>();
|
|
|
|
foreach (var tag in tags)
|
|
{
|
|
details.Add(new TagDetails
|
|
{
|
|
Id = tag.Id,
|
|
Label = tag.Label,
|
|
DelayProfileIds = delayProfiles.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
|
ImportListIds = importLists.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
|
NotificationIds = notifications.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
|
RestrictionIds = restrictions.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
|
MovieIds = movies.Where(c => c.Value.Contains(tag.Id)).Select(c => c.Key).ToList(),
|
|
IndexerIds = indexers.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
|
AutoTagIds = autotags.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
|
DownloadClientIds = downloadClients.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
|
});
|
|
}
|
|
|
|
return details;
|
|
}
|
|
|
|
public List<Tag> All()
|
|
{
|
|
return _repo.All().OrderBy(t => t.Label).ToList();
|
|
}
|
|
|
|
public Tag Add(Tag tag)
|
|
{
|
|
var existingTag = _repo.FindByLabel(tag.Label);
|
|
|
|
if (existingTag != null)
|
|
{
|
|
return existingTag;
|
|
}
|
|
|
|
tag.Label = tag.Label.ToLowerInvariant();
|
|
|
|
_repo.Insert(tag);
|
|
_eventAggregator.PublishEvent(new TagsUpdatedEvent());
|
|
|
|
return tag;
|
|
}
|
|
|
|
public Tag Update(Tag tag)
|
|
{
|
|
tag.Label = tag.Label.ToLowerInvariant();
|
|
|
|
_repo.Update(tag);
|
|
_eventAggregator.PublishEvent(new TagsUpdatedEvent());
|
|
|
|
return tag;
|
|
}
|
|
|
|
public void Delete(int tagId)
|
|
{
|
|
var details = Details(tagId);
|
|
if (details.InUse)
|
|
{
|
|
throw new ModelConflictException(typeof(Tag), tagId, $"'{details.Label}' cannot be deleted since it's still in use");
|
|
}
|
|
|
|
_repo.Delete(tagId);
|
|
_eventAggregator.PublishEvent(new TagsUpdatedEvent());
|
|
}
|
|
}
|
|
}
|