jellyfin/MediaBrowser.Server.Implementations/News/NewsService.cs

79 lines
2.2 KiB
C#
Raw Normal View History

2014-01-18 14:52:01 -07:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.News;
using MediaBrowser.Model.News;
using MediaBrowser.Model.Querying;
2014-04-26 20:42:05 -07:00
using MediaBrowser.Model.Serialization;
2014-01-18 14:52:01 -07:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Server.Implementations.News
{
public class NewsService : INewsService
{
private readonly IApplicationPaths _appPaths;
2014-01-18 21:25:01 -07:00
private readonly IJsonSerializer _json;
2014-01-18 14:52:01 -07:00
2014-01-18 21:25:01 -07:00
public NewsService(IApplicationPaths appPaths, IJsonSerializer json)
2014-01-18 14:52:01 -07:00
{
_appPaths = appPaths;
2014-01-18 21:25:01 -07:00
_json = json;
2014-01-18 14:52:01 -07:00
}
2014-01-18 21:25:01 -07:00
public QueryResult<NewsItem> GetProductNews(NewsQuery query)
2014-01-18 14:52:01 -07:00
{
2014-01-18 21:25:01 -07:00
try
{
return GetProductNewsInternal(query);
}
2014-12-22 20:58:14 -07:00
catch (DirectoryNotFoundException)
{
// No biggie
return new QueryResult<NewsItem>
{
Items = new NewsItem[] { }
};
}
2014-01-18 21:25:01 -07:00
catch (FileNotFoundException)
{
// No biggie
return new QueryResult<NewsItem>
{
Items = new NewsItem[] { }
};
}
}
2014-01-18 14:52:01 -07:00
2014-01-18 21:25:01 -07:00
private QueryResult<NewsItem> GetProductNewsInternal(NewsQuery query)
{
var path = Path.Combine(_appPaths.CachePath, "news.json");
2014-01-18 14:52:01 -07:00
2014-01-18 21:25:01 -07:00
var items = GetNewsItems(path).OrderByDescending(i => i.Date);
2014-01-18 14:52:01 -07:00
var itemsArray = items.ToArray();
var count = itemsArray.Length;
if (query.StartIndex.HasValue)
{
itemsArray = itemsArray.Skip(query.StartIndex.Value).ToArray();
}
if (query.Limit.HasValue)
{
itemsArray = itemsArray.Take(query.Limit.Value).ToArray();
}
return new QueryResult<NewsItem>
{
Items = itemsArray,
TotalRecordCount = count
};
}
private IEnumerable<NewsItem> GetNewsItems(string path)
{
2014-01-18 21:25:01 -07:00
return _json.DeserializeFromFile<List<NewsItem>>(path);
2014-01-18 14:52:01 -07:00
}
}
}