Kavita/API/Controllers/WantToReadController.cs
Joe Milazzo 15e09a0cf1
Fixed Series Relations Schema (#1654)
* Bump loader-utils from 2.0.2 to 2.0.3 in /UI/Web

Bumps [loader-utils](https://github.com/webpack/loader-utils) from 2.0.2 to 2.0.3.
- [Release notes](https://github.com/webpack/loader-utils/releases)
- [Changelog](https://github.com/webpack/loader-utils/blob/v2.0.3/CHANGELOG.md)
- [Commits](https://github.com/webpack/loader-utils/compare/v2.0.2...v2.0.3)

---
updated-dependencies:
- dependency-name: loader-utils
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

* Fixed is want to read coming back as a string and not working correctly.

* Changed from to Continue to be more explicit

* Added the first migration which exports data as a csv in temp/. This is the backup in case data is lost in the migration.

* Note for later

* Fixed the migration for the series relation so when deleting any series on any edge of the relationship, the SeriesRelation row deletes.

* Change buttons back to titles on series detail page

* Wrote the code to import relations from the backup.

* Added an additional version check to avoid file io on migration.

* Code cleanup

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-11-15 06:45:02 -08:00

98 lines
3.4 KiB
C#

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.WantToRead;
using API.Extensions;
using API.Helpers;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
/// <summary>
/// Responsible for all things Want To Read
/// </summary>
[Route("api/want-to-read")]
public class WantToReadController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
public WantToReadController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
/// <summary>
/// Return all Series that are in the current logged in user's Want to Read list, filtered
/// </summary>
/// <param name="userParams"></param>
/// <param name="filterDto"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<PagedList<SeriesDto>>> GetWantToRead([FromQuery] UserParams userParams, FilterDto filterDto)
{
userParams ??= new UserParams();
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
var pagedList = await _unitOfWork.SeriesRepository.GetWantToReadForUserAsync(user.Id, userParams, filterDto);
Response.AddPaginationHeader(pagedList.CurrentPage, pagedList.PageSize, pagedList.TotalCount, pagedList.TotalPages);
return Ok(pagedList);
}
[HttpGet]
public async Task<ActionResult<bool>> GetWantToRead([FromQuery] int seriesId)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
return Ok(await _unitOfWork.SeriesRepository.IsSeriesInWantToRead(user.Id, seriesId));
}
/// <summary>
/// Given a list of Series Ids, add them to the current logged in user's Want To Read list
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("add-series")]
public async Task<ActionResult> AddSeries(UpdateWantToReadDto dto)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(),
AppUserIncludes.WantToRead);
var existingIds = user.WantToRead.Select(s => s.Id).ToList();
existingIds.AddRange(dto.SeriesIds);
var idsToAdd = existingIds.Distinct().ToList();
var seriesToAdd = await _unitOfWork.SeriesRepository.GetSeriesByIdsAsync(idsToAdd);
foreach (var series in seriesToAdd)
{
user.WantToRead.Add(series);
}
if (!_unitOfWork.HasChanges()) return Ok();
if (await _unitOfWork.CommitAsync()) return Ok();
return BadRequest("There was an issue updating Read List");
}
/// <summary>
/// Given a list of Series Ids, remove them from the current logged in user's Want To Read list
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("remove-series")]
public async Task<ActionResult> RemoveSeries(UpdateWantToReadDto dto)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(),
AppUserIncludes.WantToRead);
user.WantToRead = user.WantToRead.Where(s => !dto.SeriesIds.Contains(s.Id)).ToList();
if (!_unitOfWork.HasChanges()) return Ok();
if (await _unitOfWork.CommitAsync()) return Ok();
return BadRequest("There was an issue updating Read List");
}
}