2023-05-08 21:21:31 +00:00
|
|
|
using System.Collections.Generic;
|
2018-08-05 14:28:05 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NzbDrone.Common.Cache;
|
2020-01-22 21:47:33 +00:00
|
|
|
using NzbDrone.Core.CustomFormats.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2018-08-05 14:28:05 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.CustomFormats
|
|
|
|
{
|
|
|
|
public interface ICustomFormatService
|
|
|
|
{
|
|
|
|
void Update(CustomFormat customFormat);
|
|
|
|
CustomFormat Insert(CustomFormat customFormat);
|
|
|
|
List<CustomFormat> All();
|
|
|
|
CustomFormat GetById(int id);
|
2018-09-10 19:25:10 +00:00
|
|
|
void Delete(int id);
|
2018-08-05 14:28:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 21:56:41 +00:00
|
|
|
public class CustomFormatService : ICustomFormatService
|
2018-08-05 14:28:05 +00:00
|
|
|
{
|
|
|
|
private readonly ICustomFormatRepository _formatRepository;
|
2020-01-22 21:47:33 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2018-08-05 14:28:05 +00:00
|
|
|
private readonly ICached<Dictionary<int, CustomFormat>> _cache;
|
|
|
|
|
2019-12-22 22:08:53 +00:00
|
|
|
public CustomFormatService(ICustomFormatRepository formatRepository,
|
2020-01-22 21:47:33 +00:00
|
|
|
ICacheManager cacheManager,
|
|
|
|
IEventAggregator eventAggregator)
|
2018-08-05 14:28:05 +00:00
|
|
|
{
|
|
|
|
_formatRepository = formatRepository;
|
2020-01-22 21:47:33 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2018-08-05 14:28:05 +00:00
|
|
|
_cache = cacheManager.GetCache<Dictionary<int, CustomFormat>>(typeof(CustomFormat), "formats");
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
private Dictionary<int, CustomFormat> AllDictionary()
|
2018-08-05 14:28:05 +00:00
|
|
|
{
|
2020-01-22 21:47:33 +00:00
|
|
|
return _cache.Get("all", () => _formatRepository.All().ToDictionary(m => m.Id));
|
2018-08-05 14:28:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
public List<CustomFormat> All()
|
2018-08-05 14:28:05 +00:00
|
|
|
{
|
2020-01-22 21:47:33 +00:00
|
|
|
return AllDictionary().Values.ToList();
|
2018-08-05 14:28:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
public CustomFormat GetById(int id)
|
2018-09-10 19:25:10 +00:00
|
|
|
{
|
2020-01-22 21:47:33 +00:00
|
|
|
return AllDictionary()[id];
|
|
|
|
}
|
2018-09-10 19:25:10 +00:00
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
public void Update(CustomFormat customFormat)
|
|
|
|
{
|
|
|
|
_formatRepository.Update(customFormat);
|
2018-09-10 19:25:10 +00:00
|
|
|
_cache.Clear();
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
public CustomFormat Insert(CustomFormat customFormat)
|
2018-09-10 19:25:10 +00:00
|
|
|
{
|
2020-01-22 21:47:33 +00:00
|
|
|
// Add to DB then insert into profiles
|
|
|
|
var result = _formatRepository.Insert(customFormat);
|
|
|
|
_cache.Clear();
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
_eventAggregator.PublishEvent(new CustomFormatAddedEvent(result));
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
return result;
|
2018-09-10 19:25:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
public void Delete(int id)
|
2018-08-05 14:28:05 +00:00
|
|
|
{
|
2020-01-22 21:47:33 +00:00
|
|
|
var format = _formatRepository.Get(id);
|
2018-08-05 14:28:05 +00:00
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
// Remove from profiles before removing from DB
|
|
|
|
_eventAggregator.PublishEvent(new CustomFormatDeletedEvent(format));
|
2018-08-05 14:28:05 +00:00
|
|
|
|
2020-01-22 21:47:33 +00:00
|
|
|
_formatRepository.Delete(id);
|
|
|
|
_cache.Clear();
|
2018-08-05 14:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|