mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com> Co-authored-by: Weblate (bot) <hosted@weblate.org> Co-authored-by: Lyrq <lyrq.ku@gmail.com>
85 lines
2.6 KiB
C#
85 lines
2.6 KiB
C#
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.SignalR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace API.Controllers;
|
|
#nullable enable
|
|
|
|
public class VolumeController : BaseApiController
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly ILocalizationService _localizationService;
|
|
private readonly IEventHub _eventHub;
|
|
|
|
public VolumeController(IUnitOfWork unitOfWork, ILocalizationService localizationService, IEventHub eventHub)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_localizationService = localizationService;
|
|
_eventHub = eventHub;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the appropriate Volume
|
|
/// </summary>
|
|
/// <param name="volumeId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<ActionResult<VolumeDto?>> GetVolume(int volumeId)
|
|
{
|
|
return Ok(await _unitOfWork.VolumeRepository.GetVolumeDtoAsync(volumeId, User.GetUserId()));
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpDelete]
|
|
public async Task<ActionResult<bool>> DeleteVolume(int volumeId)
|
|
{
|
|
var volume = await _unitOfWork.VolumeRepository.GetVolumeAsync(volumeId,
|
|
VolumeIncludes.Chapters | VolumeIncludes.People | VolumeIncludes.Tags);
|
|
if (volume == null)
|
|
return BadRequest(_localizationService.Translate(User.GetUserId(), "volume-doesnt-exist"));
|
|
|
|
_unitOfWork.VolumeRepository.Remove(volume);
|
|
|
|
if (await _unitOfWork.CommitAsync())
|
|
{
|
|
await _eventHub.SendMessageAsync(MessageFactory.VolumeRemoved, MessageFactory.VolumeRemovedEvent(volume.Id, volume.SeriesId), false);
|
|
return Ok(true);
|
|
}
|
|
|
|
return Ok(false);
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpPost("multiple")]
|
|
public async Task<ActionResult<bool>> DeleteMultipleVolumes(int[] volumesIds)
|
|
{
|
|
var volumes = await _unitOfWork.VolumeRepository.GetVolumesById(volumesIds);
|
|
if (volumes.Count != volumesIds.Length)
|
|
{
|
|
return BadRequest(_localizationService.Translate(User.GetUserId(), "volume-doesnt-exist"));
|
|
}
|
|
|
|
_unitOfWork.VolumeRepository.Remove(volumes);
|
|
|
|
if (!await _unitOfWork.CommitAsync())
|
|
{
|
|
return Ok(false);
|
|
}
|
|
|
|
foreach (var volume in volumes)
|
|
{
|
|
await _eventHub.SendMessageAsync(MessageFactory.VolumeRemoved, MessageFactory.VolumeRemovedEvent(volume.Id, volume.SeriesId), false);
|
|
}
|
|
|
|
return Ok(true);
|
|
}
|
|
}
|