Kavita/API/Controllers/ReadingProfileController.cs
Amelia 558a1d73f5
Correctly remove implicit profiles when updating
- Add actions (library actions aren't working ?)
- Auto update for implicit is going off too often
2025-05-30 12:58:47 +02:00

180 lines
6.3 KiB
C#

#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;
public class ReadingProfileController(ILogger<ReadingProfileController> logger, IUnitOfWork unitOfWork,
IReadingProfileService readingProfileService): BaseApiController
{
/// <summary>
/// Gets all non-implicit reading profiles for a user
/// </summary>
/// <returns></returns>
[HttpGet("all")]
public async Task<ActionResult<IList<UserReadingProfileDto>>> GetAllReadingProfiles()
{
var profiles = await unitOfWork.AppUserReadingProfileRepository
.GetProfilesDtoForUser(User.GetUserId(), true,
ReadingProfileIncludes.Series | ReadingProfileIncludes.Library);
return Ok(profiles);
}
/// <summary>
/// Returns the ReadingProfile that should be applied to the given series, walks up the tree.
/// Series -> Library -> Default
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpGet("{seriesId}")]
public async Task<ActionResult<UserReadingProfileDto>> GetProfileForSeries(int seriesId)
{
return Ok(await readingProfileService.GetReadingProfileForSeries(User.GetUserId(), seriesId));
}
/// <summary>
/// Updates the given reading profile, must belong to the current user
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
/// <remarks>
/// This does not update connected series, and libraries.
/// Deletes all implicit profiles for series linked to this profile
/// </remarks>
[HttpPost]
public async Task<ActionResult> UpdateReadingProfile([FromBody] UserReadingProfileDto dto)
{
await readingProfileService.UpdateReadingProfile(User.GetUserId(), dto);
return Ok();
}
/// <summary>
/// Creates a new reading profile for the current user
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("create")]
public async Task<ActionResult<UserReadingProfileDto>> CreateReadingProfile([FromBody] UserReadingProfileDto dto)
{
return Ok(await readingProfileService.CreateReadingProfile(User.GetUserId(), dto));
}
/// <summary>
/// Update the implicit reading profile for a series, creates one if none exists
/// </summary>
/// <param name="dto"></param>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpPost("series")]
public async Task<ActionResult> UpdateReadingProfileForSeries([FromBody] UserReadingProfileDto dto, [FromQuery] int seriesId)
{
await readingProfileService.UpdateImplicitReadingProfile(User.GetUserId(), seriesId, dto);
return Ok();
}
/// <summary>
/// Sets the given profile as the global default
/// </summary>
/// <param name="profileId"></param>
/// <returns></returns>
/// <exception cref="KavitaException"></exception>
/// <exception cref="UnauthorizedAccessException"></exception>
[HttpPost("set-default")]
public async Task<IActionResult> SetDefault([FromQuery] int profileId)
{
await readingProfileService.SetDefaultReadingProfile(User.GetUserId(), profileId);
return Ok();
}
/// <summary>
/// Deletes the given profile, requires the profile to belong to the logged-in user
/// </summary>
/// <param name="profileId"></param>
/// <returns></returns>
/// <exception cref="KavitaException"></exception>
/// <exception cref="UnauthorizedAccessException"></exception>
[HttpDelete]
public async Task<IActionResult> DeleteReadingProfile([FromQuery] int profileId)
{
await readingProfileService.DeleteReadingProfile(User.GetUserId(), profileId);
return Ok();
}
/// <summary>
/// Sets the reading profile for a given series, removes the old one
/// </summary>
/// <param name="seriesId"></param>
/// <param name="profileId"></param>
/// <returns></returns>
[HttpPost("series/{seriesId}")]
public async Task<IActionResult> AddProfileToSeries(int seriesId, [FromQuery] int profileId)
{
await readingProfileService.AddProfileToSeries(User.GetUserId(), profileId, seriesId);
return Ok();
}
/// <summary>
/// Clears the reading profile for the given series for the currently logged-in user
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpDelete("series/{seriesId}")]
public async Task<IActionResult> ClearSeriesProfile(int seriesId)
{
await readingProfileService.ClearSeriesProfile(User.GetUserId(), seriesId);
return Ok();
}
/// <summary>
/// Sets the reading profile for a given library, removes the old one
/// </summary>
/// <param name="libraryId"></param>
/// <param name="profileId"></param>
/// <returns></returns>
[HttpPost("library/{libraryId}")]
public async Task<IActionResult> AddProfileToLibrary(int libraryId, [FromQuery] int profileId)
{
await readingProfileService.AddProfileToLibrary(User.GetUserId(), profileId, libraryId);
return Ok();
}
/// <summary>
/// Clears the reading profile for the given library for the currently logged-in user
/// </summary>
/// <param name="libraryId"></param>
/// <param name="profileId"></param>
/// <returns></returns>
[HttpDelete("library/{libraryId}")]
public async Task<IActionResult> ClearLibraryProfile(int libraryId)
{
await readingProfileService.ClearLibraryProfile(User.GetUserId(), libraryId);
return Ok();
}
/// <summary>
/// Assigns the reading profile to all passes series, and deletes their implicit profiles
/// </summary>
/// <param name="profileId"></param>
/// <param name="seriesIds"></param>
/// <returns></returns>
[HttpPost("bulk")]
public async Task<IActionResult> BulkAddReadingProfile([FromQuery] int profileId, [FromBody] IList<int> seriesIds)
{
await readingProfileService.BulkAddProfileToSeries(User.GetUserId(), profileId, seriesIds);
return Ok();
}
}