Chapter/Issue level Reviews and Ratings (#3778)
Co-authored-by: Joseph Milazzo <josephmajora@gmail.com>
This commit is contained in:
parent
3b8997e46e
commit
4f7625ea77
60 changed files with 5097 additions and 497 deletions
|
@ -1,8 +1,11 @@
|
|||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
using API.Data.Repositories;
|
||||
using API.DTOs.SeriesDetail;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.Extensions;
|
||||
using API.Helpers.Builders;
|
||||
using API.Services.Plus;
|
||||
|
@ -30,17 +33,17 @@ public class ReviewController : BaseApiController
|
|||
|
||||
|
||||
/// <summary>
|
||||
/// Updates the review for a given series
|
||||
/// 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);
|
||||
if (user == null) return Unauthorized();
|
||||
|
||||
var ratingBuilder = new RatingBuilder(user.Ratings.FirstOrDefault(r => r.SeriesId == dto.SeriesId));
|
||||
var ratingBuilder = new RatingBuilder(await _unitOfWork.UserRepository.GetUserRatingAsync(dto.SeriesId, user.Id));
|
||||
|
||||
var rating = ratingBuilder
|
||||
.WithBody(dto.Body)
|
||||
|
@ -52,22 +55,58 @@ public class ReviewController : BaseApiController
|
|||
{
|
||||
user.Ratings.Add(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));
|
||||
}
|
||||
|
||||
/// <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
|
||||
.WithBody(dto.Body)
|
||||
.WithSeriesId(dto.SeriesId)
|
||||
.WithChapterId(chapterId)
|
||||
.Build();
|
||||
|
||||
if (rating.Id == 0)
|
||||
{
|
||||
user.ChapterRatings.Add(rating);
|
||||
}
|
||||
|
||||
_unitOfWork.UserRepository.Update(user);
|
||||
|
||||
await _unitOfWork.CommitAsync();
|
||||
|
||||
return Ok(_mapper.Map<UserReviewDto>(rating));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the user's review for the given series
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpDelete]
|
||||
public async Task<ActionResult> DeleteReview(int seriesId)
|
||||
[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();
|
||||
|
@ -80,4 +119,23 @@ public class ReviewController : BaseApiController
|
|||
|
||||
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);
|
||||
|
||||
await _unitOfWork.CommitAsync();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue