using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using API.Data; using API.Data.Repositories; using API.DTOs; using API.DTOs.Filtering; using API.DTOs.SeriesDetail; using API.Entities; using API.Entities.Enums; using API.Entities.Metadata; using API.Extensions; using API.Helpers; using API.Services; using Kavita.Common; using Kavita.Common.Extensions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace API.Controllers; public class SeriesController : BaseApiController { private readonly ILogger _logger; private readonly ITaskScheduler _taskScheduler; private readonly IUnitOfWork _unitOfWork; private readonly ISeriesService _seriesService; public SeriesController(ILogger logger, ITaskScheduler taskScheduler, IUnitOfWork unitOfWork, ISeriesService seriesService) { _logger = logger; _taskScheduler = taskScheduler; _unitOfWork = unitOfWork; _seriesService = seriesService; } [HttpPost] public async Task>> GetSeriesForLibrary(int libraryId, [FromQuery] UserParams userParams, [FromBody] FilterDto filterDto) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); var series = await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId, userParams, filterDto); // Apply progress/rating information (I can't work out how to do this in initial query) if (series == null) return BadRequest("Could not get series for library"); await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series); Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages); return Ok(series); } /// /// Fetches a Series for a given Id /// /// Series Id to fetch details for /// /// Throws an exception if the series Id does exist [HttpGet("{seriesId:int}")] public async Task> GetSeries(int seriesId) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); try { return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoByIdAsync(seriesId, userId)); } catch (Exception e) { _logger.LogError(e, "There was an issue fetching {SeriesId}", seriesId); throw new KavitaException("This series does not exist"); } } [Authorize(Policy = "RequireAdminRole")] [HttpDelete("{seriesId}")] public async Task> DeleteSeries(int seriesId) { var username = User.GetUsername(); _logger.LogInformation("Series {SeriesId} is being deleted by {UserName}", seriesId, username); return Ok(await _seriesService.DeleteMultipleSeries(new[] {seriesId})); } [Authorize(Policy = "RequireAdminRole")] [HttpPost("delete-multiple")] public async Task DeleteMultipleSeries(DeleteSeriesDto dto) { var username = User.GetUsername(); _logger.LogInformation("Series {SeriesId} is being deleted by {UserName}", dto.SeriesIds, username); if (await _seriesService.DeleteMultipleSeries(dto.SeriesIds)) return Ok(); return BadRequest("There was an issue deleting the series requested"); } /// /// Returns All volumes for a series with progress information and Chapters /// /// /// [HttpGet("volumes")] public async Task>> GetVolumes(int seriesId) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); return Ok(await _unitOfWork.VolumeRepository.GetVolumesDtoAsync(seriesId, userId)); } [HttpGet("volume")] public async Task> GetVolume(int volumeId) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); return Ok(await _unitOfWork.VolumeRepository.GetVolumeDtoAsync(volumeId, userId)); } [HttpGet("chapter")] public async Task> GetChapter(int chapterId) { return Ok(await _unitOfWork.ChapterRepository.GetChapterDtoAsync(chapterId)); } [HttpGet("chapter-metadata")] public async Task> GetChapterMetadata(int chapterId) { return Ok(await _unitOfWork.ChapterRepository.GetChapterMetadataDtoAsync(chapterId)); } [HttpPost("update-rating")] public async Task UpdateSeriesRating(UpdateSeriesRatingDto updateSeriesRatingDto) { var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Ratings); if (!await _seriesService.UpdateRating(user, updateSeriesRatingDto)) return BadRequest("There was a critical error."); return Ok(); } [HttpPost("update")] public async Task UpdateSeries(UpdateSeriesDto updateSeries) { _logger.LogInformation("{UserName} is updating Series {SeriesName}", User.GetUsername(), updateSeries.Name); var series = await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(updateSeries.Id); if (series == null) return BadRequest("Series does not exist"); var seriesExists = await _unitOfWork.SeriesRepository.DoesSeriesNameExistInLibrary(updateSeries.Name.Trim(), series.LibraryId, series.Format); if (series.Name != updateSeries.Name && seriesExists) { return BadRequest("A series already exists in this library with this name. Series Names must be unique to a library."); } series.Name = updateSeries.Name.Trim(); series.NormalizedName = Services.Tasks.Scanner.Parser.Parser.Normalize(series.Name); if (!string.IsNullOrEmpty(updateSeries.SortName.Trim())) { series.SortName = updateSeries.SortName.Trim(); } series.LocalizedName = updateSeries.LocalizedName.Trim(); series.NormalizedLocalizedName = Services.Tasks.Scanner.Parser.Parser.Normalize(series.LocalizedName); series.NameLocked = updateSeries.NameLocked; series.SortNameLocked = updateSeries.SortNameLocked; series.LocalizedNameLocked = updateSeries.LocalizedNameLocked; var needsRefreshMetadata = false; // This is when you hit Reset if (series.CoverImageLocked && !updateSeries.CoverImageLocked) { // Trigger a refresh when we are moving from a locked image to a non-locked needsRefreshMetadata = true; series.CoverImage = string.Empty; series.CoverImageLocked = updateSeries.CoverImageLocked; } _unitOfWork.SeriesRepository.Update(series); if (await _unitOfWork.CommitAsync()) { if (needsRefreshMetadata) { _taskScheduler.RefreshSeriesMetadata(series.LibraryId, series.Id); } return Ok(); } return BadRequest("There was an error with updating the series"); } [ResponseCache(CacheProfileName = "Instant")] [HttpPost("recently-added")] public async Task>> GetRecentlyAdded(FilterDto filterDto, [FromQuery] UserParams userParams, [FromQuery] int libraryId = 0) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); var series = await _unitOfWork.SeriesRepository.GetRecentlyAdded(libraryId, userId, userParams, filterDto); // Apply progress/rating information (I can't work out how to do this in initial query) if (series == null) return BadRequest("Could not get series"); await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series); Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages); return Ok(series); } [ResponseCache(CacheProfileName = "Instant")] [HttpPost("recently-updated-series")] public async Task>> GetRecentlyAddedChapters() { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); return Ok(await _unitOfWork.SeriesRepository.GetRecentlyUpdatedSeries(userId)); } [HttpPost("all")] public async Task>> GetAllSeries(FilterDto filterDto, [FromQuery] UserParams userParams, [FromQuery] int libraryId = 0) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); var series = await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId, userParams, filterDto); // Apply progress/rating information (I can't work out how to do this in initial query) if (series == null) return BadRequest("Could not get series"); await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series); Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages); return Ok(series); } /// /// Fetches series that are on deck aka have progress on them. /// /// /// /// Default of 0 meaning all libraries /// [ResponseCache(CacheProfileName = "Instant")] [HttpPost("on-deck")] public async Task>> GetOnDeck(FilterDto filterDto, [FromQuery] UserParams userParams, [FromQuery] int libraryId = 0) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); var pagedList = await _unitOfWork.SeriesRepository.GetOnDeck(userId, libraryId, userParams, filterDto); await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, pagedList); Response.AddPaginationHeader(pagedList.CurrentPage, pagedList.PageSize, pagedList.TotalCount, pagedList.TotalPages); return Ok(pagedList); } /// /// Runs a Cover Image Generation task /// /// /// [Authorize(Policy = "RequireAdminRole")] [HttpPost("refresh-metadata")] public ActionResult RefreshSeriesMetadata(RefreshSeriesDto refreshSeriesDto) { _taskScheduler.RefreshSeriesMetadata(refreshSeriesDto.LibraryId, refreshSeriesDto.SeriesId, refreshSeriesDto.ForceUpdate); return Ok(); } /// /// Scan a series and force each file to be updated. This should be invoked via the User, hence why we force. /// /// /// [Authorize(Policy = "RequireAdminRole")] [HttpPost("scan")] public ActionResult ScanSeries(RefreshSeriesDto refreshSeriesDto) { _taskScheduler.ScanSeries(refreshSeriesDto.LibraryId, refreshSeriesDto.SeriesId, refreshSeriesDto.ForceUpdate); return Ok(); } /// /// Run a file analysis on the series. /// /// /// [Authorize(Policy = "RequireAdminRole")] [HttpPost("analyze")] public ActionResult AnalyzeSeries(RefreshSeriesDto refreshSeriesDto) { _taskScheduler.AnalyzeFilesForSeries(refreshSeriesDto.LibraryId, refreshSeriesDto.SeriesId, refreshSeriesDto.ForceUpdate); return Ok(); } /// /// Returns metadata for a given series /// /// /// [HttpGet("metadata")] public async Task> GetSeriesMetadata(int seriesId) { var metadata = await _unitOfWork.SeriesRepository.GetSeriesMetadata(seriesId); return Ok(metadata); } /// /// Update series metadata /// /// /// [HttpPost("metadata")] public async Task UpdateSeriesMetadata(UpdateSeriesMetadataDto updateSeriesMetadataDto) { if (await _seriesService.UpdateSeriesMetadata(updateSeriesMetadataDto)) { return Ok("Successfully updated"); } return BadRequest("Could not update metadata"); } /// /// Returns all Series grouped by the passed Collection Id with Pagination. /// /// Collection Id to pull series from /// Pagination information /// [HttpGet("series-by-collection")] public async Task>> GetSeriesByCollectionTag(int collectionId, [FromQuery] UserParams userParams) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); var series = await _unitOfWork.SeriesRepository.GetSeriesDtoForCollectionAsync(collectionId, userId, userParams); // Apply progress/rating information (I can't work out how to do this in initial query) if (series == null) return BadRequest("Could not get series for collection"); await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series); Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages); return Ok(series); } /// /// Fetches Series for a set of Ids. This will check User for permission access and filter out any Ids that don't exist or /// the user does not have access to. /// /// [HttpPost("series-by-ids")] public async Task>> GetAllSeriesById(SeriesByIdsDto dto) { if (dto.SeriesIds == null) return BadRequest("Must pass seriesIds"); var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForIdsAsync(dto.SeriesIds, userId)); } /// /// Get the age rating for the enum value /// /// /// /// This is cached for an hour [ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = new [] {"ageRating"})] [HttpGet("age-rating")] public ActionResult GetAgeRating(int ageRating) { var val = (AgeRating) ageRating; if (val == AgeRating.NotApplicable) return "No Restriction"; return Ok(val.ToDescription()); } /// /// Get a special DTO for Series Detail page. /// /// /// /// Do not rely on this API externally. May change without hesitation. [ResponseCache(CacheProfileName = "5Minute", VaryByQueryKeys = new [] {"seriesId"})] [HttpGet("series-detail")] public async Task> GetSeriesDetailBreakdown(int seriesId) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); return await _seriesService.GetSeriesDetail(seriesId, userId); } /// /// Fetches the related series for a given series /// /// /// Type of Relationship to pull back /// [HttpGet("related")] public async Task>> GetRelatedSeries(int seriesId, RelationKind relation) { // Send back a custom DTO with each type or maybe sorted in some way var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); return Ok(await _unitOfWork.SeriesRepository.GetSeriesForRelationKind(userId, seriesId, relation)); } /// /// Returns all related series against the passed series Id /// /// /// [HttpGet("all-related")] public async Task> GetAllRelatedSeries(int seriesId) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); return Ok(await _seriesService.GetRelatedSeries(userId, seriesId)); } /// /// Update the relations attached to the Series. Does not generate associated Sequel/Prequel pairs on target series. /// /// /// [Authorize(Policy="RequireAdminRole")] [HttpPost("update-related")] public async Task UpdateRelatedSeries(UpdateRelatedSeriesDto dto) { if (await _seriesService.UpdateRelatedSeries(dto)) { return Ok(); } return BadRequest("There was an issue updating relationships"); } }