#nullable enable using System; using System.Collections.Generic; using System.Threading.Tasks; using API.Data; using API.Data.Repositories; using API.DTOs; using API.Extensions; using API.Services; using Kavita.Common; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace API.Controllers; 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.GetProfilesForUser(User.GetUserId(), true)); } /// /// Returns the ReadingProfile that should be applied to the given series, walks up the tree. /// Series -> Library -> Default /// /// /// [HttpGet("{seriesId}")] public async Task> GetProfileForSeries(int seriesId) { return Ok(await readingProfileService.GetReadingProfileForSeries(User.GetUserId(), seriesId)); } /// /// Updates the given reading profile, must belong to the current user /// /// /// /// Optionally, from which series the update is called. /// If set, will delete the implicit reading profile if it exists /// /// [HttpPost] public async Task UpdateReadingProfile([FromBody] UserReadingProfileDto dto, [FromQuery] int? seriesCtx) { if (seriesCtx.HasValue) { await readingProfileService.DeleteImplicitForSeries(User.GetUserId(), seriesCtx.Value); } var success = await readingProfileService.UpdateReadingProfile(User.GetUserId(), dto); if (!success) return BadRequest(); return Ok(); } /// /// 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)); } /// /// Update the implicit reading profile for a series, creates one if none exists /// /// /// /// [HttpPost("series")] public async Task UpdateReadingProfileForSeries([FromBody] UserReadingProfileDto dto, [FromQuery] int seriesId) { var success = await readingProfileService.UpdateImplicitReadingProfile(User.GetUserId(), seriesId, dto); if (!success) return BadRequest(); return Ok(); } /// /// Sets the given profile as the global default /// /// /// /// /// [HttpPost("set-default")] public async Task SetDefault([FromQuery] int profileId) { await readingProfileService.SetDefaultReadingProfile(User.GetUserId(), profileId); return Ok(); } /// /// 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(); } }