Kavita/API/Controllers/RatingController.cs
Joe Milazzo 52d19642f9
Misc Fixes (#2155)
* Fixed default token key not being long enough and Kavita auto-generating

* When scheduling nightly backup job, make it run at 2am to ensure everything else has ran.

* Made the overlay system work better on mobile. In order to do this, had to implement my own copy button.

* Tweaked the code to ensure we clear the selection doing anything and clicking off the overlay clears more reliably.

* Cleaned up the overlay code

* Added the ability to view the series that a rating is representing. Requires Kavita+ deployment.

* When calculating overall average rating of server, if only review is yours, don't include it.

When calculating overall average rating of server, scale to percentage (* 20) to match all other rating scales.

* Fixed side nav on mobile without donate link not fully covering the height of the screen

* Only trigger the task conversion warning on Media screen if you've touched the appropriate control.

* Fixed a bug where bookmark directory wasn't able to be changed.

* Fixed a bug where see More wouldn't show if there were just characters due to missing that check.

* Fixed a typo in documentation

* If a chapter has a range 1-6 and is fully read, when calculating highest chapter for Scrobbling, use the 6.
2023-07-23 09:53:16 -07:00

78 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Constants;
using API.Data;
using API.DTOs;
using API.Extensions;
using API.Services.Plus;
using EasyCaching.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace API.Controllers;
/// <summary>
/// Responsible for providing external ratings for Series
/// </summary>
public class RatingController : BaseApiController
{
private readonly ILicenseService _licenseService;
private readonly IRatingService _ratingService;
private readonly ILogger<RatingController> _logger;
private readonly IUnitOfWork _unitOfWork;
private readonly IEasyCachingProvider _cacheProvider;
public const string CacheKey = "rating_";
public RatingController(ILicenseService licenseService, IRatingService ratingService,
ILogger<RatingController> logger, IEasyCachingProviderFactory cachingProviderFactory, IUnitOfWork unitOfWork)
{
_licenseService = licenseService;
_ratingService = ratingService;
_logger = logger;
_unitOfWork = unitOfWork;
_cacheProvider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.KavitaPlusRatings);
}
/// <summary>
/// Get the external ratings for a given series
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpGet]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.KavitaPlus, VaryByQueryKeys = new []{"seriesId"})]
public async Task<ActionResult<IEnumerable<RatingDto>>> GetRating(int seriesId)
{
if (!await _licenseService.HasActiveLicense())
{
return Ok(Enumerable.Empty<RatingDto>());
}
var cacheKey = CacheKey + seriesId;
var results = await _cacheProvider.GetAsync<IEnumerable<RatingDto>>(cacheKey);
if (results.HasValue)
{
return Ok(results.Value);
}
var ratings = await _ratingService.GetRatings(seriesId);
await _cacheProvider.SetAsync(cacheKey, ratings, TimeSpan.FromHours(24));
_logger.LogDebug("Caching external rating for {Key}", cacheKey);
return Ok(ratings);
}
[HttpGet("overall")]
public async Task<ActionResult<RatingDto>> GetOverallRating(int seriesId)
{
return Ok(new RatingDto()
{
Provider = ScrobbleProvider.Kavita,
AverageScore = await _unitOfWork.SeriesRepository.GetAverageUserRating(seriesId, User.GetUserId()),
FavoriteCount = 0
});
}
}