using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using API.Constants;
using API.DTOs;
using API.DTOs.SeriesDetail;
using API.Services.Plus;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace API.Controllers;
///
/// Responsible for providing external ratings for Series
///
public class RatingController : BaseApiController
{
private readonly ILicenseService _licenseService;
private readonly IRatingService _ratingService;
private readonly IMemoryCache _cache;
private readonly ILogger _logger;
public const string CacheKey = "rating-";
public RatingController(ILicenseService licenseService, IRatingService ratingService, IMemoryCache memoryCache, ILogger logger)
{
_licenseService = licenseService;
_ratingService = ratingService;
_cache = memoryCache;
_logger = logger;
}
///
/// Get the external ratings for a given series
///
///
///
[HttpGet]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Recommendation, VaryByQueryKeys = new []{"seriesId"})]
public async Task>> GetRating(int seriesId)
{
if (!await _licenseService.HasActiveLicense())
{
return Ok(new List());
}
var cacheKey = CacheKey + seriesId;
var setCache = false;
IEnumerable ratings;
if (_cache.TryGetValue(cacheKey, out string cachedData))
{
ratings = JsonConvert.DeserializeObject>(cachedData);
}
else
{
ratings = await _ratingService.GetRatings(seriesId);
setCache = true;
}
if (setCache)
{
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSize(1)
.SetAbsoluteExpiration(TimeSpan.FromHours(24));
_cache.Set(cacheKey, JsonConvert.SerializeObject(ratings), cacheEntryOptions);
_logger.LogDebug("Caching external rating for {Key}", cacheKey);
}
return Ok(ratings);
}
}