Implemented ability to delete a series. Refactored some operations to remove unneeded parameters.

This commit is contained in:
Joseph Milazzo 2021-01-12 16:51:23 -06:00
parent 83076f02ad
commit 56e8a0059e
9 changed files with 50 additions and 16 deletions

View file

@ -159,13 +159,13 @@ namespace API.Controllers
var username = User.GetUsername();
_logger.LogInformation($"Library {libraryId} is being deleted by {username}.");
var series = await _seriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId);
var volumes = (await _seriesRepository.GetVolumesForSeriesAsync(series.Select(x => x.Id).ToArray())).ToList();
var volumes = (await _seriesRepository.GetVolumesForSeriesAsync(series.Select(x => x.Id).ToArray()))
.Select(x => x.Id).ToArray();
var result = await _libraryRepository.DeleteLibrary(libraryId);
if (result && volumes.Any())
{
BackgroundJob.Enqueue(() => _cacheService.CleanupLibrary(libraryId, volumes.Select(x => x.Id).ToArray()));
BackgroundJob.Enqueue(() => _cacheService.CleanupVolumes(volumes));
}
return Ok(result);

View file

@ -1,8 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs;
using API.Extensions;
using API.Interfaces;
using AutoMapper;
using Hangfire;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@ -14,14 +18,17 @@ namespace API.Controllers
private readonly IMapper _mapper;
private readonly ITaskScheduler _taskScheduler;
private readonly ISeriesRepository _seriesRepository;
private readonly ICacheService _cacheService;
public SeriesController(ILogger<SeriesController> logger, IMapper mapper,
ITaskScheduler taskScheduler, ISeriesRepository seriesRepository)
ITaskScheduler taskScheduler, ISeriesRepository seriesRepository,
ICacheService cacheService)
{
_logger = logger;
_mapper = mapper;
_taskScheduler = taskScheduler;
_seriesRepository = seriesRepository;
_cacheService = cacheService;
}
[HttpGet("{seriesId}")]
@ -30,6 +37,22 @@ namespace API.Controllers
return Ok(await _seriesRepository.GetSeriesDtoByIdAsync(seriesId));
}
[Authorize(Policy = "RequireAdminRole")]
[HttpDelete("{seriesId}")]
public async Task<ActionResult<bool>> DeleteSeries(int seriesId)
{
var username = User.GetUsername();
var volumes = (await _seriesRepository.GetVolumesForSeriesAsync(new []{seriesId})).Select(x => x.Id).ToArray();
_logger.LogInformation($"Series {seriesId} is being deleted by {username}.");
var result = await _seriesRepository.DeleteSeriesAsync(seriesId);
if (result)
{
BackgroundJob.Enqueue(() => _cacheService.CleanupVolumes(volumes));
}
return Ok(result);
}
[HttpGet("volumes")]
public async Task<ActionResult<IEnumerable<VolumeDto>>> GetVolumes(int seriesId)
{

View file

@ -38,10 +38,12 @@ namespace API.Data
public async Task<IEnumerable<LibraryDto>> GetLibrariesDtoForUsernameAsync(string userName)
{
// TODO: Speed this query up
return await _context.Library
.Include(l => l.AppUsers)
.Where(library => library.AppUsers.Any(x => x.UserName == userName))
.ProjectTo<LibraryDto>(_mapper.ConfigurationProvider).ToListAsync();
.ProjectTo<LibraryDto>(_mapper.ConfigurationProvider)
.ToListAsync();
}
public async Task<Library> GetLibraryForNameAsync(string libraryName)

View file

@ -104,5 +104,13 @@ namespace API.Data
.Where(v => seriesIds.Contains(v.SeriesId))
.ToListAsync();
}
public async Task<bool> DeleteSeriesAsync(int seriesId)
{
var series = await _context.Series.Where(s => s.Id == seriesId).SingleOrDefaultAsync();
_context.Series.Remove(series);
return await _context.SaveChangesAsync() > 0;
}
}
}

View file

@ -16,15 +16,13 @@ namespace API.Interfaces
/// <summary>
/// Clears cache directory of all folders and files.
/// </summary>
/// <param name="volume"></param>
void Cleanup();
/// <summary>
/// Clears cache directory of all volumes that belong to a given library.
/// Clears cache directory of all volumes. This can be invoked from deleting a library or a series.
/// </summary>
/// <param name="libraryId"></param>
/// <param name="volumeIds">Volumes that belong to that library. Assume the library might have been deleted before this invocation.</param>
void CleanupLibrary(int libraryId, int[] volumeIds);
void CleanupVolumes(int[] volumeIds);
/// <summary>

View file

@ -21,5 +21,6 @@ namespace API.Interfaces
Task<VolumeDto> GetVolumeDtoAsync(int volumeId);
Task<IEnumerable<Volume>> GetVolumesForSeriesAsync(int[] seriesIds);
Task<bool> DeleteSeriesAsync(int seriesId);
}
}

View file

@ -48,7 +48,6 @@ namespace API.Services
return volume;
}
public void Cleanup()
{
@ -74,9 +73,9 @@ namespace API.Services
_logger.LogInformation("Cache directory purged.");
}
public void CleanupLibrary(int libraryId, int[] volumeIds)
public void CleanupVolumes(int[] volumeIds)
{
_logger.LogInformation($"Running Cache cleanup on Library: {libraryId}");
_logger.LogInformation($"Running Cache cleanup on Volumes");
foreach (var volume in volumeIds)
{
@ -89,7 +88,7 @@ namespace API.Services
}
_logger.LogInformation("Cache directory purged");
}
private string GetVolumeCachePath(int volumeId, MangaFile file)
{

View file

@ -343,6 +343,7 @@ namespace API.Services
}
using ZipArchive archive = ZipFile.OpenRead(archivePath);
Console.WriteLine();
return archive.Entries.Count(e => Parser.Parser.IsImage(e.FullName));
}