Kavita/API/Data/Repositories/PersonRepository.cs
Joe Milazzo b802e1e1b0
Release Shakeout Day 1 (#1591)
* Fixed an issue where reading list were not able to update their summary due to a duplicate title check.

* Misc code smell cleanup

* Updated .net dependencies and removed unneeded ones

* Fixed an issue where removing a series from want to read list page wouldn't update the page correctly

* Fixed age restriction not applied to Recommended page

* Ensure that Genres and Tags are age restricted gated

* Persons are now age gated as well

* When you choose a cover, the new cover will properly be selected and will focus on it, in the cases there are many other covers available.

* Fixed caching profiles

* Added in a special hook when deleting a library to clear all series Relations before we delete
2022-10-18 16:53:17 -07:00

97 lines
2.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs;
using API.Entities;
using API.Extensions;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
namespace API.Data.Repositories;
public interface IPersonRepository
{
void Attach(Person person);
void Remove(Person person);
Task<IList<Person>> GetAllPeople();
Task<IList<PersonDto>> GetAllPersonDtosAsync(int userId);
Task RemoveAllPeopleNoLongerAssociated(bool removeExternal = false);
Task<IList<PersonDto>> GetAllPeopleDtosForLibrariesAsync(List<int> libraryIds, int userId);
Task<int> GetCountAsync();
}
public class PersonRepository : IPersonRepository
{
private readonly DataContext _context;
private readonly IMapper _mapper;
public PersonRepository(DataContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public void Attach(Person person)
{
_context.Person.Attach(person);
}
public void Remove(Person person)
{
_context.Person.Remove(person);
}
public async Task RemoveAllPeopleNoLongerAssociated(bool removeExternal = false)
{
var peopleWithNoConnections = await _context.Person
.Include(p => p.SeriesMetadatas)
.Include(p => p.ChapterMetadatas)
.Where(p => p.SeriesMetadatas.Count == 0 && p.ChapterMetadatas.Count == 0)
.AsSplitQuery()
.ToListAsync();
_context.Person.RemoveRange(peopleWithNoConnections);
await _context.SaveChangesAsync();
}
public async Task<IList<PersonDto>> GetAllPeopleDtosForLibrariesAsync(List<int> libraryIds, int userId)
{
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
return await _context.Series
.Where(s => libraryIds.Contains(s.LibraryId))
.RestrictAgainstAgeRestriction(ageRating)
.SelectMany(s => s.Metadata.People)
.Distinct()
.OrderBy(p => p.Name)
.AsNoTracking()
.AsSplitQuery()
.ProjectTo<PersonDto>(_mapper.ConfigurationProvider)
.ToListAsync();
}
public async Task<int> GetCountAsync()
{
return await _context.Person.CountAsync();
}
public async Task<IList<Person>> GetAllPeople()
{
return await _context.Person
.OrderBy(p => p.Name)
.ToListAsync();
}
public async Task<IList<PersonDto>> GetAllPersonDtosAsync(int userId)
{
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
return await _context.Person
.OrderBy(p => p.Name)
.RestrictAgainstAgeRestriction(ageRating)
.ProjectTo<PersonDto>(_mapper.ConfigurationProvider)
.ToListAsync();
}
}