Load, save, and delete chapter reviews

This commit is contained in:
Amelia 2025-04-25 22:38:32 +02:00
parent a3e04f3bc1
commit 85b6f107bc
No known key found for this signature in database
GPG key ID: D6D0ECE365407EAA
13 changed files with 192 additions and 13 deletions

View file

@ -62,6 +62,45 @@ public class ReviewController : BaseApiController
return Ok(_mapper.Map<UserReviewDto>(rating));
}
/// <summary>
/// Updates the review for a given series
/// </summary>
/// <param name="dto"></param>
/// <param name="chapterId"></param>
/// <returns></returns>
[HttpPost("chapter/{chapterId}")]
public async Task<ActionResult<UserReviewDto>> UpdateChapterReview(int chapterId, UpdateUserReviewDto dto)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.ChapterRatings);
if (user == null) return Unauthorized();
var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(chapterId, ChapterIncludes.None);
if (chapter == null) return BadRequest();
var builder = new ChapterRatingBuilder(user.ChapterRatings.FirstOrDefault(r => r.SeriesId == dto.SeriesId));
var rating = builder
.WithSeriesId(dto.SeriesId)
.WithVolumeId(chapter.VolumeId)
.WithChapterId(chapter.Id)
.WithReview(dto.Body)
.Build();
if (rating.Id == 0)
{
user.ChapterRatings.Add(rating);
}
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
// Do I need this?
//BackgroundJob.Enqueue(() =>
// _scrobblingService.ScrobbleReviewUpdate(user.Id, dto.SeriesId, string.Empty, dto.Body));
return Ok(_mapper.Map<UserReviewDto>(rating));
}
/// <summary>
/// Deletes the user's review for the given series
/// </summary>
@ -80,4 +119,24 @@ public class ReviewController : BaseApiController
return Ok();
}
/// <summary>
/// Deletes the user's review for a given chapter
/// </summary>
/// <param name="chapterId"></param>
/// <returns></returns>
[HttpDelete("chapter/{chapterId}")]
public async Task<IActionResult> DeleteChapterReview(int chapterId)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.ChapterRatings);
if (user == null) return Unauthorized();
user.ChapterRatings = user.ChapterRatings.Where(c => c.ChapterId != chapterId).ToList();
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
return Ok();
}
}