Simply entities & seperate endpoints

This commit is contained in:
Amelia 2025-04-28 20:31:47 +02:00
parent 41faa30e6f
commit 6d4dfcda67
No known key found for this signature in database
GPG key ID: D6D0ECE365407EAA
22 changed files with 299 additions and 615 deletions

View file

@ -399,7 +399,7 @@ public class ChapterController : BaseApiController
[HttpGet("chapter-detail-plus")]
public async Task<ChapterDetailPlusDto> ChapterDetailPlus([FromQuery] int chapterId)
public async Task<ActionResult<ChapterDetailPlusDto>> ChapterDetailPlus([FromQuery] int chapterId)
{
var ret = new ChapterDetailPlusDto();
@ -415,11 +415,10 @@ public class ChapterController : BaseApiController
ret.HasBeenRated = ownRating.HasBeenRated;
}
var externalMetadata = await _unitOfWork.ExternalChapterMetadataRepository.Get(chapterId);
if (externalMetadata != null && externalMetadata.ExternalReviews.Count > 0)
var externalReviews = await _unitOfWork.ChapterRepository.GetExternalChapterReviews(chapterId);
if (externalReviews.Count > 0)
{
var dtos = externalMetadata.ExternalReviews.Select(ex => _mapper.Map<UserReviewDto>(ex)).ToList();
userReviews.AddRange(ReviewHelper.SelectSpectrumOfReviews(dtos));
userReviews.AddRange(ReviewHelper.SelectSpectrumOfReviews(externalReviews));
}
ret.Reviews = userReviews;

View file

@ -1,17 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Constants;
using API.Data;
using API.Data.Repositories;
using API.DTOs;
using API.Extensions;
using API.Services;
using API.Services.Plus;
using EasyCaching.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace API.Controllers;
@ -33,13 +28,19 @@ public class RatingController : BaseApiController
_localizationService = localizationService;
}
[HttpPost]
public async Task<ActionResult> UpdateRating(UpdateRatingDto updateRating)
/// <summary>
/// Update the users' rating of the given series
/// </summary>
/// <param name="updateRating"></param>
/// <returns></returns>
/// <exception cref="UnauthorizedAccessException"></exception>
[HttpPost("series")]
public async Task<ActionResult> UpdateSeriesRating(UpdateRatingDto updateRating)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.Ratings | AppUserIncludes.ChapterRatings);
if (user == null) throw new UnauthorizedAccessException();
if (await _ratingService.UpdateRating(user, updateRating))
if (await _ratingService.UpdateSeriesRating(user, updateRating))
{
return Ok();
}
@ -47,24 +48,44 @@ public class RatingController : BaseApiController
return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-error"));
}
[HttpGet("overall")]
public async Task<ActionResult<RatingDto>> GetOverallRating(int seriesId, [FromQuery] int? chapterId)
/// <summary>
/// Update the users' rating of the given chapter
/// </summary>
/// <param name="updateRating">chapterId must be set</param>
/// <returns></returns>
/// <exception cref="UnauthorizedAccessException"></exception>
[HttpPost("chapter")]
public async Task<ActionResult> UpdateChapterRating(UpdateRatingDto updateRating)
{
int average;
if (chapterId != null)
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.Ratings | AppUserIncludes.ChapterRatings);
if (user == null) throw new UnauthorizedAccessException();
if (await _ratingService.UpdateChapterRating(user, updateRating))
{
average = await _unitOfWork.ChapterRepository.GetAverageUserRating(chapterId.Value, User.GetUserId());
}
else
{
average = await _unitOfWork.SeriesRepository.GetAverageUserRating(seriesId, User.GetUserId());
return Ok();
}
return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-error"));
}
[HttpGet("overall-series")]
public async Task<ActionResult<RatingDto>> GetOverallSeriesRating(int seriesId)
{
return Ok(new RatingDto()
{
Provider = ScrobbleProvider.Kavita,
AverageScore = average,
AverageScore = await _unitOfWork.SeriesRepository.GetAverageUserRating(seriesId, User.GetUserId()),
FavoriteCount = 0,
});
}
[HttpGet("overall-chapter")]
public async Task<ActionResult<RatingDto>> GetOverallChapterRating(int chapterId)
{
return Ok(new RatingDto()
{
Provider = ScrobbleProvider.Kavita,
AverageScore = await _unitOfWork.ChapterRepository.GetAverageUserRating(chapterId, User.GetUserId()),
FavoriteCount = 0,
});
}

View file

@ -33,39 +33,16 @@ public class ReviewController : BaseApiController
/// <summary>
/// Updates the review for a given series, or chapter
/// Updates the user's review for a given series
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<UserReviewDto>> UpdateReview(UpdateUserReviewDto dto)
[HttpPost("series")]
public async Task<ActionResult<UserReviewDto>> UpdateSeriesReview(UpdateUserReviewDto dto)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.Ratings | AppUserIncludes.ChapterRatings);
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.Ratings);
if (user == null) return Unauthorized();
UserReviewDto review;
if (dto.ChapterId != null)
{
review = await UpdateChapterReview(user, dto, dto.ChapterId.Value);
}
else
{
review = await UpdateSeriesReview(user, dto);
}
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
if (dto.ChapterId == null)
{
BackgroundJob.Enqueue(() =>
_scrobblingService.ScrobbleReviewUpdate(user.Id, dto.SeriesId, string.Empty, dto.Body));
}
return Ok(review);
}
private async Task<UserReviewDto> UpdateSeriesReview(AppUser user, UpdateUserReviewDto dto)
{
var ratingBuilder = new RatingBuilder(await _unitOfWork.UserRepository.GetUserRatingAsync(dto.SeriesId, user.Id));
var rating = ratingBuilder
@ -78,11 +55,31 @@ public class ReviewController : BaseApiController
{
user.Ratings.Add(rating);
}
return _mapper.Map<UserReviewDto>(rating);
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
BackgroundJob.Enqueue(() =>
_scrobblingService.ScrobbleReviewUpdate(user.Id, dto.SeriesId, string.Empty, dto.Body));
return Ok(_mapper.Map<UserReviewDto>(rating));
}
private async Task<UserReviewDto> UpdateChapterReview(AppUser user, UpdateUserReviewDto dto, int chapterId)
/// <summary>
/// Update the user's review for a given chapter
/// </summary>
/// <param name="dto">chapterId must be set</param>
/// <returns></returns>
[HttpPost("chapter")]
public async Task<ActionResult<UserReviewDto>> UpdateChapterReview(UpdateUserReviewDto dto)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.ChapterRatings);
if (user == null) return Unauthorized();
if (dto.ChapterId == null) return BadRequest();
int chapterId = dto.ChapterId.Value;
var ratingBuilder = new ChapterRatingBuilder(await _unitOfWork.UserRepository.GetUserChapterRatingAsync(user.Id, chapterId));
var rating = ratingBuilder
@ -95,28 +92,45 @@ public class ReviewController : BaseApiController
{
user.ChapterRatings.Add(rating);
}
return _mapper.Map<UserReviewDto>(rating);
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
return Ok(_mapper.Map<UserReviewDto>(rating));
}
/// <summary>
/// Deletes the user's review for the given series, or chapter
/// Deletes the user's review for the given series
/// </summary>
/// <returns></returns>
[HttpDelete]
public async Task<ActionResult> DeleteReview([FromQuery] int seriesId, [FromQuery] int? chapterId)
[HttpDelete("series")]
public async Task<ActionResult> DeleteSeriesReview([FromQuery] int seriesId)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.Ratings);
if (user == null) return Unauthorized();
if (chapterId != null)
{
user.ChapterRatings = user.ChapterRatings.Where(r => r.ChapterId != chapterId).ToList();
}
else
{
user.Ratings = user.Ratings.Where(r => r.SeriesId != seriesId).ToList();
}
user.Ratings = user.Ratings.Where(r => r.SeriesId != seriesId).ToList();
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
return Ok();
}
/// <summary>
/// Deletes the user's review for the given chapter
/// </summary>
/// <returns></returns>
[HttpDelete("chapter")]
public async Task<ActionResult> DeleteChapterReview([FromQuery] int chapterId)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.ChapterRatings);
if (user == null) return Unauthorized();
user.ChapterRatings = user.ChapterRatings.Where(r => r.ChapterId != chapterId).ToList();
_unitOfWork.UserRepository.Update(user);