Add rating to chapter page & volume (if one chapter)

This commit is contained in:
Amelia 2025-04-26 09:40:56 +02:00
parent e96cb0fde9
commit 8ccc2b5801
No known key found for this signature in database
GPG key ID: D6D0ECE365407EAA
18 changed files with 226 additions and 47 deletions

View file

@ -0,0 +1,63 @@
using System;
using System.Threading.Tasks;
using API.Data;
using API.Data.Repositories;
using API.DTOs;
using API.Entities;
using Microsoft.Extensions.Logging;
namespace API.Services;
public interface IRatingService
{
Task<bool> UpdateChapterRating(int userId, UpdateChapterRatingDto dto);
}
public class RatingService: IRatingService
{
private readonly IUnitOfWork _unitOfWork;
private readonly ILogger<RatingService> _logger;
public RatingService(IUnitOfWork unitOfWork, ILogger<RatingService> logger)
{
_unitOfWork = unitOfWork;
_logger = logger;
}
public async Task<bool> UpdateChapterRating(int userId, UpdateChapterRatingDto dto)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId, AppUserIncludes.ChapterRatings);
if (user == null) throw new UnauthorizedAccessException();
var rating = await _unitOfWork.UserRepository.GetUserChapterRatingAsync(dto.ChapterId, userId) ?? new AppUserChapterRating();
rating.Rating = Math.Clamp(dto.Rating, 0, 5);
rating.HasBeenRated = true;
rating.ChapterId = dto.ChapterId;
if (rating.Id == 0)
{
user.ChapterRatings.Add(rating);
}
_unitOfWork.UserRepository.Update(user);
try
{
if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync())
{
// Scrobble Update?
return true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "There was an exception while updating chapter rating");
}
await _unitOfWork.RollbackAsync();
user.ChapterRatings.Remove(rating);
return false;
}
}