2014-01-18 22:52:01 +01:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Model.News;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
2014-04-27 05:42:05 +02:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-01-18 22:52:01 +01: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-19 05:25:01 +01:00
|
|
|
|
private readonly IJsonSerializer _json;
|
2014-01-18 22:52:01 +01:00
|
|
|
|
|
2014-01-19 05:25:01 +01:00
|
|
|
|
public NewsService(IApplicationPaths appPaths, IJsonSerializer json)
|
2014-01-18 22:52:01 +01:00
|
|
|
|
{
|
|
|
|
|
_appPaths = appPaths;
|
2014-01-19 05:25:01 +01:00
|
|
|
|
_json = json;
|
2014-01-18 22:52:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-19 05:25:01 +01:00
|
|
|
|
public QueryResult<NewsItem> GetProductNews(NewsQuery query)
|
2014-01-18 22:52:01 +01:00
|
|
|
|
{
|
2014-01-19 05:25:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return GetProductNewsInternal(query);
|
|
|
|
|
}
|
2014-12-23 04:58:14 +01:00
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No biggie
|
|
|
|
|
return new QueryResult<NewsItem>
|
|
|
|
|
{
|
|
|
|
|
Items = new NewsItem[] { }
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-01-19 05:25:01 +01:00
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No biggie
|
|
|
|
|
return new QueryResult<NewsItem>
|
|
|
|
|
{
|
|
|
|
|
Items = new NewsItem[] { }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-18 22:52:01 +01:00
|
|
|
|
|
2014-01-19 05:25:01 +01:00
|
|
|
|
private QueryResult<NewsItem> GetProductNewsInternal(NewsQuery query)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(_appPaths.CachePath, "news.json");
|
2014-01-18 22:52:01 +01:00
|
|
|
|
|
2014-01-19 05:25:01 +01:00
|
|
|
|
var items = GetNewsItems(path).OrderByDescending(i => i.Date);
|
2014-01-18 22:52:01 +01: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 05:25:01 +01:00
|
|
|
|
return _json.DeserializeFromFile<List<NewsItem>>(path);
|
2014-01-18 22:52:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|