
Co-authored-by: JeanPaulDOT <jp.houssier@gmail.com> Co-authored-by: Francois Wilhelmy <ice_mouton@hotmail.com> Co-authored-by: Gazy Mahomar <gmahomarf@gmail.com> Co-authored-by: Stijn <stijn.biemans@gmail.com> Co-authored-by: 無情天 <kofzhanganguo@126.com> Co-authored-by: Havokdan <havokdan@yahoo.com.br> Co-authored-by: Andre <andruecha32@gmail.com> Co-authored-by: Mateusz <mateuszvx8.96@gmail.com> Co-authored-by: Antonio Sanchez Castellón <angelfx19@gmail.com> Co-authored-by: Duarte Silva <smallflake@protonmail.com> Co-authored-by: LeeWan1210 <dldhks456@live.com> Co-authored-by: aleixcox <18121624@qq.com> Co-authored-by: Tomas Battistini <tomas.battistini@gmail.com> Co-authored-by: mareczek82 <marek.posiadala@gmail.com> Co-authored-by: Hans Kalisvaart <hans.kalisvaart@gmail.com> Co-authored-by: majora2007 <kavitareader@gmail.com> Co-authored-by: afermar <adrian.fm@protonmail.com> Co-authored-by: oxygen44k <iiccpp@outlook.com> Co-authored-by: Weblate (bot) <hosted@weblate.org> Co-authored-by: Hadrien b <hadrien.1997@gmail.com> Co-authored-by: Robbie Davis <robbie@therobbiedavis.com> Co-authored-by: Andre Smith <Hobogrammer@users.noreply.github.com> Co-authored-by: Safu Wan <safu@yahoo.com> Co-authored-by: sibeck <sibeck.clown@gmail.com> Co-authored-by: Florestano Pepe <florestano.pepe@gmail.com> Co-authored-by: 书签 <shuqian.emu@gmail.com> Co-authored-by: Stéphane Dupont <aleistor@gmail.com> Co-authored-by: gallegonovato <fran-carro@hotmail.es> Co-authored-by: AlienHack <the4got10@windowslive.com> Co-authored-by: 周書丞 <tmrsm_chan@hotmail.com> Co-authored-by: Andre Smith <andrepsmithjr@gmail.com> Co-authored-by: xe1st <dnzkckali@gmail.com> Co-authored-by: Jiří Heger <jiri.heger@gmail.com> Co-authored-by: DR <weblate-kavita.snowflake668@slmail.me> Co-authored-by: Mathieu Ares <matguitarist@gmail.com> Co-authored-by: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Co-authored-by: Gazy Mahomar <gmahomarf@users.noreply.github.com> Co-authored-by: Elias Jakob <elias.jakob100@gmail.com> Co-authored-by: Christian Zanon <chri8431@libero.it> Co-authored-by: Eryk Michalak <gnu.ewm@protonmail.com> Co-authored-by: Hoshino0881118 <hoshino0881118@gmail.com>
111 lines
3.4 KiB
C#
111 lines
3.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.DTOs;
|
|
using API.Entities;
|
|
using API.Entities.Enums;
|
|
using API.Extensions;
|
|
using API.Extensions.QueryExtensions;
|
|
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<IList<PersonDto>> GetAllPersonDtosByRoleAsync(int userId, PersonRole role);
|
|
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();
|
|
}
|
|
|
|
public async Task<IList<PersonDto>> GetAllPersonDtosByRoleAsync(int userId, PersonRole role)
|
|
{
|
|
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
|
return await _context.Person
|
|
.Where(p => p.Role == role)
|
|
.OrderBy(p => p.Name)
|
|
.RestrictAgainstAgeRestriction(ageRating)
|
|
.ProjectTo<PersonDto>(_mapper.ConfigurationProvider)
|
|
.ToListAsync();
|
|
}
|
|
}
|