Kavita/API/Services/RatingService.cs
Amelia 184cf46533
Revert "Unify ChapterRating with Rating"
This wasn't working out, there is still some duplicate code. But not
that much, and from the API, there is no different. Hooray!
2025-04-28 17:14:02 +02:00

128 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using API.Data;
using API.Data.Repositories;
using API.DTOs;
using API.Entities;
using API.Services.Plus;
using Hangfire;
using Microsoft.Extensions.Logging;
namespace API.Services;
public interface IRatingService
{
/// <summary>
///
/// </summary>
/// <param name="user">Should include ratings</param>
/// <param name="updateRatingDto"></param>
/// <returns></returns>
Task<bool> UpdateRating(AppUser user, UpdateRatingDto updateRatingDto);
}
public class RatingService: IRatingService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IScrobblingService _scrobblingService;
private readonly ILogger<RatingService> _logger;
public RatingService(IUnitOfWork unitOfWork, IScrobblingService scrobblingService, ILogger<RatingService> logger)
{
_unitOfWork = unitOfWork;
_scrobblingService = scrobblingService;
_logger = logger;
}
public async Task<bool> UpdateRating(AppUser user, UpdateRatingDto updateRatingDto)
{
if (updateRatingDto.ChapterId != null)
{
return await UpdateChapterRating(user, updateRatingDto);
}
return await UpdateSeriesRating(user, updateRatingDto);
}
private async Task<bool> UpdateSeriesRating(AppUser user, UpdateRatingDto updateRatingDto)
{
var userRating =
await _unitOfWork.UserRepository.GetUserRatingAsync(updateRatingDto.SeriesId, user.Id) ??
new AppUserRating();
try
{
userRating.Rating = Math.Clamp(updateRatingDto.UserRating, 0f, 5f);
userRating.HasBeenRated = true;
userRating.SeriesId = updateRatingDto.SeriesId;
if (userRating.Id == 0)
{
user.Ratings ??= new List<AppUserRating>();
user.Ratings.Add(userRating);
}
_unitOfWork.UserRepository.Update(user);
if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync())
{
BackgroundJob.Enqueue(() =>
_scrobblingService.ScrobbleRatingUpdate(user.Id, updateRatingDto.SeriesId,
userRating.Rating));
return true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "There was an exception saving rating");
}
await _unitOfWork.RollbackAsync();
user.Ratings?.Remove(userRating);
return false;
}
private async Task<bool> UpdateChapterRating(AppUser user, UpdateRatingDto updateRatingDto)
{
if (updateRatingDto.ChapterId == null)
{
return false;
}
var userRating =
await _unitOfWork.UserRepository.GetUserChapterRatingAsync(user.Id, updateRatingDto.ChapterId.Value) ??
new AppUserChapterRating();
try
{
userRating.Rating = Math.Clamp(updateRatingDto.UserRating, 0f, 5f);
userRating.HasBeenRated = true;
userRating.SeriesId = updateRatingDto.SeriesId;
userRating.ChapterId = updateRatingDto.ChapterId.Value;
if (userRating.Id == 0)
{
user.ChapterRatings ??= new List<AppUserChapterRating>();
user.ChapterRatings.Add(userRating);
}
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "There was an exception saving rating");
}
await _unitOfWork.RollbackAsync();
user.ChapterRatings?.Remove(userRating);
return false;
}
}