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.
80 lines
1.8 KiB
80 lines
1.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
namespace NzbDrone.Core.Tags
|
|
{
|
|
public interface ITagService
|
|
{
|
|
Tag GetTag(int tagId);
|
|
Tag GetTag(string tag);
|
|
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;
|
|
|
|
public TagService(ITagRepository repo, IEventAggregator eventAggregator)
|
|
{
|
|
_repo = repo;
|
|
_eventAggregator = eventAggregator;
|
|
}
|
|
|
|
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> All()
|
|
{
|
|
return _repo.All().OrderBy(t => t.Label).ToList();
|
|
}
|
|
|
|
public Tag Add(Tag tag)
|
|
{
|
|
//TODO: check for duplicate tag by label and return that tag instead?
|
|
|
|
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)
|
|
{
|
|
_repo.Delete(tagId);
|
|
_eventAggregator.PublishEvent(new TagsUpdatedEvent());
|
|
}
|
|
}
|
|
}
|