mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-04-02 07:14:30 -04:00
73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
using AutoMapper.QueryableExtensions;
|
|
using Kavita.API.Repositories;
|
|
using Kavita.Models.DTOs.ReadingLists.CBL.RemapRules;
|
|
using Kavita.Models.Entities;
|
|
using Kavita.Models.Entities.ReadingLists;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Kavita.Database.Repositories;
|
|
|
|
public class ReadingListRemapRuleRepository(DataContext context, IMapper mapper) : IReadingListRemapRuleRepository
|
|
{
|
|
public async Task<IList<ReadingListRemapRule>> GetRulesForNamesAsync(IList<string> normalizedNames, int userId, CancellationToken ct = default)
|
|
{
|
|
return await context.ReadingListRemapRule
|
|
.Where(r => normalizedNames.Contains(r.NormalizedCblSeriesName)
|
|
&& (r.AppUserId == userId || r.IsGlobal))
|
|
.OrderByDescending(r => r.AppUserId == userId) // user-specific first
|
|
.ToListAsync(ct);
|
|
}
|
|
|
|
public async Task<IList<ReadingListRemapRule>> GetRulesForUserAsync(int userId, CancellationToken ct = default)
|
|
{
|
|
return await context.ReadingListRemapRule
|
|
.Include(r => r.AppUser)
|
|
.Include(r => r.Chapter)
|
|
.Include(r => r.Series).ThenInclude(s => s.Library)
|
|
.Where(r => r.AppUserId == userId || r.IsGlobal)
|
|
.OrderByDescending(r => r.AppUserId == userId)
|
|
.ThenByDescending(r => r.CreatedUtc)
|
|
.ToListAsync(ct);
|
|
}
|
|
|
|
public async Task<ReadingListRemapRule?> GetByIdAsync(int id, CancellationToken ct = default)
|
|
{
|
|
return await context.ReadingListRemapRule
|
|
.Include(r => r.AppUser)
|
|
.FirstOrDefaultAsync(r => r.Id == id, ct);
|
|
}
|
|
|
|
public async Task<RemapRuleDto?> GetDtoByIdAsync(int id, CancellationToken ct = default)
|
|
{
|
|
return await context.ReadingListRemapRule
|
|
.Include(r => r.AppUser)
|
|
.Where(r => r.Id == id)
|
|
.ProjectTo<RemapRuleDto>(mapper.ConfigurationProvider)
|
|
.FirstOrDefaultAsync(ct);
|
|
}
|
|
|
|
public async Task<IList<ReadingListRemapRule>> GetAllRulesAsync(CancellationToken ct = default)
|
|
{
|
|
return await context.ReadingListRemapRule
|
|
.Include(r => r.AppUser)
|
|
.OrderByDescending(r => r.IsGlobal)
|
|
.ThenBy(r => r.NormalizedCblSeriesName)
|
|
.ToListAsync(ct);
|
|
}
|
|
|
|
public void Add(ReadingListRemapRule rule)
|
|
{
|
|
context.ReadingListRemapRule.Add(rule);
|
|
}
|
|
|
|
public void Remove(ReadingListRemapRule rule)
|
|
{
|
|
context.ReadingListRemapRule.Remove(rule);
|
|
}
|
|
}
|