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

71 lines
1.9 KiB
C#
Raw Normal View History

2014-01-18 21:52:01 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.News;
using MediaBrowser.Model.News;
using MediaBrowser.Model.Querying;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2014-01-19 04:25:01 +00:00
using MediaBrowser.Model.Serialization;
2014-01-18 21:52:01 +00:00
namespace MediaBrowser.Server.Implementations.News
{
public class NewsService : INewsService
{
private readonly IApplicationPaths _appPaths;
2014-01-19 04:25:01 +00:00
private readonly IJsonSerializer _json;
2014-01-18 21:52:01 +00:00
2014-01-19 04:25:01 +00:00
public NewsService(IApplicationPaths appPaths, IJsonSerializer json)
2014-01-18 21:52:01 +00:00
{
_appPaths = appPaths;
2014-01-19 04:25:01 +00:00
_json = json;
2014-01-18 21:52:01 +00:00
}
2014-01-19 04:25:01 +00:00
public QueryResult<NewsItem> GetProductNews(NewsQuery query)
2014-01-18 21:52:01 +00:00
{
2014-01-19 04:25:01 +00:00
try
{
return GetProductNewsInternal(query);
}
catch (FileNotFoundException)
{
// No biggie
return new QueryResult<NewsItem>
{
Items = new NewsItem[] { }
};
}
}
2014-01-18 21:52:01 +00:00
2014-01-19 04:25:01 +00:00
private QueryResult<NewsItem> GetProductNewsInternal(NewsQuery query)
{
var path = Path.Combine(_appPaths.CachePath, "news.json");
2014-01-18 21:52:01 +00:00
2014-01-19 04:25:01 +00:00
var items = GetNewsItems(path).OrderByDescending(i => i.Date);
2014-01-18 21:52:01 +00: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-19 04:25:01 +00:00
return _json.DeserializeFromFile<List<NewsItem>>(path);
2014-01-18 21:52:01 +00:00
}
}
}