#nullable enable 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.Extensions; using API.Services; using AutoMapper; using Kavita.Common; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace API.Controllers; [Route("api/reading-profile")] public class ReadingProfileController(ILogger logger, IUnitOfWork unitOfWork, IReadingProfileService readingProfileService): BaseApiController { /// /// Gets all non-implicit reading profiles for a user /// /// [HttpGet("all")] public async Task>> GetAllReadingProfiles() { return Ok(await unitOfWork.AppUserReadingProfileRepository.GetProfilesDtoForUser(User.GetUserId(), true)); } /// /// Returns the ReadingProfile that should be applied to the given series, walks up the tree. /// Series -> Library -> Default /// /// /// /// [HttpGet("{seriesId:int}")] public async Task> GetProfileForSeries(int seriesId, [FromQuery] bool skipImplicit) { return Ok(await readingProfileService.GetReadingProfileDtoForSeries(User.GetUserId(), seriesId, skipImplicit)); } /// /// Returns the (potential) Reading Profile bound to the library /// /// /// [HttpGet("library")] public async Task> GetProfileForLibrary(int libraryId) { return Ok(await readingProfileService.GetReadingProfileDtoForLibrary(User.GetUserId(), libraryId)); } /// /// Creates a new reading profile for the current user /// /// /// [HttpPost("create")] public async Task> CreateReadingProfile([FromBody] UserReadingProfileDto dto) { return Ok(await readingProfileService.CreateReadingProfile(User.GetUserId(), dto)); } /// /// Promotes the implicit profile to a user profile. Removes the series from other profiles /// /// /// [HttpPost("promote")] public async Task> PromoteImplicitReadingProfile([FromQuery] int profileId) { return Ok(await readingProfileService.PromoteImplicitProfile(User.GetUserId(), profileId)); } /// /// Update the implicit reading profile for a series, creates one if none exists /// /// Any modification to the reader settings during reading will create an implicit profile. Use "update-parent" to save to the bound series profile. /// /// /// [HttpPost("series")] public async Task> UpdateReadingProfileForSeries([FromBody] UserReadingProfileDto dto, [FromQuery] int seriesId) { var updatedProfile = await readingProfileService.UpdateImplicitReadingProfile(User.GetUserId(), seriesId, dto); return Ok(updatedProfile); } /// /// Updates the non-implicit reading profile for the given series, and removes implicit profiles /// /// /// /// [HttpPost("update-parent")] public async Task> UpdateParentProfileForSeries([FromBody] UserReadingProfileDto dto, [FromQuery] int seriesId) { var newParentProfile = await readingProfileService.UpdateParent(User.GetUserId(), seriesId, dto); return Ok(newParentProfile); } /// /// Updates the given reading profile, must belong to the current user /// /// /// The updated reading profile /// /// This does not update connected series and libraries. /// [HttpPost] public async Task> UpdateReadingProfile(UserReadingProfileDto dto) { return Ok(await readingProfileService.UpdateReadingProfile(User.GetUserId(), dto)); } /// /// Deletes the given profile, requires the profile to belong to the logged-in user /// /// /// /// /// [HttpDelete] public async Task DeleteReadingProfile([FromQuery] int profileId) { await readingProfileService.DeleteReadingProfile(User.GetUserId(), profileId); return Ok(); } /// /// Sets the reading profile for a given series, removes the old one /// /// /// /// [HttpPost("series/{seriesId:int}")] public async Task AddProfileToSeries(int seriesId, [FromQuery] int profileId) { await readingProfileService.AddProfileToSeries(User.GetUserId(), profileId, seriesId); return Ok(); } /// /// Clears the reading profile for the given series for the currently logged-in user /// /// /// [HttpDelete("series/{seriesId:int}")] public async Task ClearSeriesProfile(int seriesId) { await readingProfileService.ClearSeriesProfile(User.GetUserId(), seriesId); return Ok(); } /// /// Sets the reading profile for a given library, removes the old one /// /// /// /// [HttpPost("library/{libraryId:int}")] public async Task AddProfileToLibrary(int libraryId, [FromQuery] int profileId) { await readingProfileService.AddProfileToLibrary(User.GetUserId(), profileId, libraryId); return Ok(); } /// /// Clears the reading profile for the given library for the currently logged-in user /// /// /// /// [HttpDelete("library/{libraryId:int}")] public async Task ClearLibraryProfile(int libraryId) { await readingProfileService.ClearLibraryProfile(User.GetUserId(), libraryId); return Ok(); } /// /// Assigns the reading profile to all passes series, and deletes their implicit profiles /// /// /// /// [HttpPost("bulk")] public async Task BulkAddReadingProfile([FromQuery] int profileId, [FromBody] IList seriesIds) { await readingProfileService.BulkAddProfileToSeries(User.GetUserId(), profileId, seriesIds); return Ok(); } }