jellyfin/Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs
Erwin de Haan ec1f5dc317 Mayor code cleanup
Add Argument*Exceptions now use proper nameof operators.

Added exception messages to quite a few Argument*Exceptions.

Fixed rethorwing to be proper syntax.

Added a ton of null checkes. (This is only a start, there are about 500 places that need proper null handling)

Added some TODOs to log certain exceptions.

Fix sln again.

Fixed all AssemblyInfo's and added proper copyright (where I could find them)

We live in *current year*.

Fixed the use of braces.

Fixed a ton of properties, and made a fair amount of functions static that should be and can be static.

Made more Methods that should be static static.

You can now use static to find bad functions!

Removed unused variable. And added one more proper XML comment.
2019-01-10 20:38:53 +01:00

145 lines
3.8 KiB
C#

using Microsoft.Extensions.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
namespace Emby.Server.Implementations.LiveTv.EmbyTV
{
public class ItemDataProvider<T>
where T : class
{
private readonly object _fileDataLock = new object();
private List<T> _items;
private readonly IJsonSerializer _jsonSerializer;
protected readonly ILogger Logger;
private readonly string _dataPath;
protected readonly Func<T, T, bool> EqualityComparer;
private readonly IFileSystem _fileSystem;
public ItemDataProvider(IFileSystem fileSystem, IJsonSerializer jsonSerializer, ILogger logger, string dataPath, Func<T, T, bool> equalityComparer)
{
Logger = logger;
_dataPath = dataPath;
EqualityComparer = equalityComparer;
_jsonSerializer = jsonSerializer;
_fileSystem = fileSystem;
}
public IReadOnlyList<T> GetAll()
{
lock (_fileDataLock)
{
if (_items == null)
{
Logger.LogInformation("Loading live tv data from {0}", _dataPath);
_items = GetItemsFromFile(_dataPath);
}
return _items.ToList();
}
}
private List<T> GetItemsFromFile(string path)
{
var jsonFile = path + ".json";
try
{
return _jsonSerializer.DeserializeFromFile<List<T>>(jsonFile) ?? new List<T>();
}
catch (FileNotFoundException)
{
}
catch (IOException)
{
}
catch (Exception ex)
{
Logger.LogError(ex, "Error deserializing {jsonFile}", jsonFile);
}
return new List<T>();
}
private void UpdateList(List<T> newList)
{
if (newList == null)
{
throw new ArgumentNullException(nameof(newList));
}
var file = _dataPath + ".json";
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(file));
lock (_fileDataLock)
{
_jsonSerializer.SerializeToFile(newList, file);
_items = newList;
}
}
public virtual void Update(T item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
var list = GetAll().ToList();
var index = list.FindIndex(i => EqualityComparer(i, item));
if (index == -1)
{
throw new ArgumentException("item not found");
}
list[index] = item;
UpdateList(list);
}
public virtual void Add(T item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
var list = GetAll().ToList();
if (list.Any(i => EqualityComparer(i, item)))
{
throw new ArgumentException("item already exists");
}
list.Add(item);
UpdateList(list);
}
public void AddOrUpdate(T item)
{
var list = GetAll().ToList();
if (!list.Any(i => EqualityComparer(i, item)))
{
Add(item);
}
else
{
Update(item);
}
}
public virtual void Delete(T item)
{
var list = GetAll().Where(i => !EqualityComparer(i, item)).ToList();
UpdateList(list);
}
}
}