#nullable enable using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using API.DTOs; using API.Entities; using API.Extensions.QueryExtensions; using AutoMapper; using AutoMapper.QueryableExtensions; using Microsoft.EntityFrameworkCore; namespace API.Data.Repositories; [Flags] public enum ReadingProfileIncludes { None = 0, Series = 1 << 1, Library = 1 << 2 } public interface IAppUserReadingProfileRepository { Task> GetProfilesForUser(int userId); Task GetProfileForSeries(int userId, int seriesId); Task GetProfileDtoForSeries(int userId, int seriesId); Task GetProfileForLibrary(int userId, int libraryId); Task GetProfileDtoForLibrary(int userId, int libraryId); Task GetProfile(int profileId, ReadingProfileIncludes includes = ReadingProfileIncludes.None); Task GetProfileDto(int profileId); void Add(AppUserReadingProfile readingProfile); void Update(AppUserReadingProfile readingProfile); void Remove(AppUserReadingProfile readingProfile); } public class AppUserReadingProfileRepository(DataContext context, IMapper mapper): IAppUserReadingProfileRepository { public async Task> GetProfilesForUser(int userId) { return await context.AppUserReadingProfile .Where(rp => rp.UserId == userId) .ToListAsync(); } public async Task GetProfileForSeries(int userId, int seriesId) { return await context.AppUserReadingProfile .Where(rp => rp.UserId == userId && rp.Series.Any(s => s.Id == seriesId)) .FirstOrDefaultAsync(); } public async Task GetProfileDtoForSeries(int userId, int seriesId) { return await context.AppUserReadingProfile .Where(rp => rp.UserId == userId && rp.Series.Any(s => s.Id == seriesId)) .ProjectTo(mapper.ConfigurationProvider) .FirstOrDefaultAsync(); } public async Task GetProfileForLibrary(int userId, int libraryId) { return await context.AppUserReadingProfile .Where(rp => rp.UserId == userId && rp.Libraries.Any(s => s.Id == libraryId)) .FirstOrDefaultAsync(); } public async Task GetProfileDtoForLibrary(int userId, int libraryId) { return await context.AppUserReadingProfile .Where(rp => rp.UserId == userId && rp.Libraries.Any(s => s.Id == libraryId)) .ProjectTo(mapper.ConfigurationProvider) .FirstOrDefaultAsync(); } public async Task GetProfile(int profileId, ReadingProfileIncludes includes = ReadingProfileIncludes.None) { return await context.AppUserReadingProfile .Where(rp => rp.Id == profileId) .Includes(includes) .FirstOrDefaultAsync(); } public async Task GetProfileDto(int profileId) { return await context.AppUserReadingProfile .Where(rp => rp.Id == profileId) .ProjectTo(mapper.ConfigurationProvider) .FirstOrDefaultAsync(); } public void Add(AppUserReadingProfile readingProfile) { context.AppUserReadingProfile.Add(readingProfile); } public void Update(AppUserReadingProfile readingProfile) { context.AppUserReadingProfile.Update(readingProfile).State = EntityState.Modified; } public void Remove(AppUserReadingProfile readingProfile) { context.AppUserReadingProfile.Remove(readingProfile); } }