mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
84 lines
2.3 KiB
C#
84 lines
2.3 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Data;
|
|
using API.Data.Repositories;
|
|
using API.DTOs.SeriesDetail;
|
|
using API.Extensions;
|
|
using API.Helpers.Builders;
|
|
using API.Services.Plus;
|
|
using AutoMapper;
|
|
using Hangfire;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace API.Controllers;
|
|
|
|
#nullable enable
|
|
|
|
public class ReviewController : BaseApiController
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly IMapper _mapper;
|
|
private readonly IScrobblingService _scrobblingService;
|
|
|
|
public ReviewController(IUnitOfWork unitOfWork,
|
|
IMapper mapper, IScrobblingService scrobblingService)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_mapper = mapper;
|
|
_scrobblingService = scrobblingService;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Updates the review for a given series
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ActionResult<UserReviewDto>> UpdateReview(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 rating = ratingBuilder
|
|
.WithBody(dto.Body)
|
|
.WithSeriesId(dto.SeriesId)
|
|
.WithTagline(string.Empty)
|
|
.Build();
|
|
|
|
if (rating.Id == 0)
|
|
{
|
|
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>
|
|
/// Deletes the user's review for the given series
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpDelete]
|
|
public async Task<ActionResult> DeleteReview(int seriesId)
|
|
{
|
|
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.Ratings);
|
|
if (user == null) return Unauthorized();
|
|
|
|
user.Ratings = user.Ratings.Where(r => r.SeriesId != seriesId).ToList();
|
|
|
|
_unitOfWork.UserRepository.Update(user);
|
|
|
|
await _unitOfWork.CommitAsync();
|
|
|
|
return Ok();
|
|
}
|
|
}
|