using Microsoft.EntityFrameworkCore;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Repositories;
public interface IHeroRepository
{
/// The hero of a language with its slides (and their images), tracked for update.
Task GetAsync(string language, CancellationToken cancellationToken);
/// The same, not tracked (read paths).
Task GetReadOnlyAsync(string language, CancellationToken cancellationToken);
void Add(HeroContent hero);
void AddSlide(HeroSlide slide);
void RemoveSlide(HeroSlide slide);
Task GetImageAsync(Guid id, CancellationToken cancellationToken);
Task> GetImagesAsync(IReadOnlyCollection ids, CancellationToken cancellationToken);
/// Images whose set is missing or does not match their focus, oldest first (tracked).
Task CountImagesAsync(CancellationToken cancellationToken);
/// Every image including those in the trash, not tracked (the media library copy).
Task> GetAllImagesIncludingTrashedReadOnlyAsync(CancellationToken cancellationToken);
/// An image whether or not it is in the trash, tracked (restore, trash).
Task GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken);
/// The photos in the trash, most recently trashed first, not tracked.
Task> GetTrashedImagesReadOnlyAsync(CancellationToken cancellationToken);
/// Photos that were trashed before the cutoff, tracked (the trash is emptied for them).
Task> GetTrashedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
/// True when an image other than the given one has this set hash or this original key (the files are shared).
Task<(bool SetShared, bool OriginalShared)> FilesSharedWithOthersAsync(Guid imageId, string setHash, string originalKey, CancellationToken cancellationToken);
/// The slides that use an image, tracked (they are removed when the image goes to the trash).
Task> GetSlidesOfImageAsync(Guid imageId, CancellationToken cancellationToken);
/// All images, newest first, not tracked (the media library lists them).
Task> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken);
/// All slides, not tracked (where each image is used).
Task> GetAllSlidesReadOnlyAsync(CancellationToken cancellationToken);
Task> GetImagesNeedingRenderAsync(CancellationToken cancellationToken);
/// One slide with its photo, tracked (a new crop for the slide); null when there is none or its photo is in the trash.
Task GetSlideAsync(Guid id, CancellationToken cancellationToken);
/// The slide as stored right now, with its photo, not tracked.
Task GetSlideFreshAsync(Guid id, CancellationToken cancellationToken);
/// Slides with a crop of their own whose set is missing or does not match that crop, in hero order (tracked).
Task> GetSlidesNeedingRenderAsync(CancellationToken cancellationToken);
/// Images with a complete set made before the full-frame preview existed (tracked).
Task> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken);
/// The image as stored right now, not tracked.
Task GetImageFreshAsync(Guid id, CancellationToken cancellationToken);
Task GetImageBySha256Async(string sha256, CancellationToken cancellationToken);
void AddImage(HeroImage image);
void RemoveImage(HeroImage image);
Task ImageInUseAsync(Guid imageId, CancellationToken cancellationToken);
Task> GetUnattachedImagesCreatedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
Task> GetReferencedSetHashesAsync(CancellationToken cancellationToken);
Task> GetReferencedOriginalKeysAsync(CancellationToken cancellationToken);
void AddRetired(HeroRetiredFile file);
void RemoveRetired(HeroRetiredFile file);
Task> GetRetiredBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
Task> GetRetiredSetHashesAsync(CancellationToken cancellationToken);
Task SaveChangesAsync(CancellationToken cancellationToken);
}
public class HeroRepository(HeroDbContext context) : IHeroRepository
{
public Task GetAsync(string language, CancellationToken cancellationToken)
=> context.Heroes.Include(h => h.Slides).ThenInclude(s => s.Image)
.FirstOrDefaultAsync(h => h.Language == language, cancellationToken);
public Task GetReadOnlyAsync(string language, CancellationToken cancellationToken)
=> context.Heroes.AsNoTracking().Include(h => h.Slides).ThenInclude(s => s.Image)
.FirstOrDefaultAsync(h => h.Language == language, cancellationToken);
public void Add(HeroContent hero) => context.Heroes.Add(hero);
public void AddSlide(HeroSlide slide) => context.HeroSlides.Add(slide);
public void RemoveSlide(HeroSlide slide) => context.HeroSlides.Remove(slide);
public Task GetImageAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null, cancellationToken);
public Task GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
public async Task> GetAllImagesIncludingTrashedReadOnlyAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).OrderBy(i => i.CreatedAt).ToList();
public async Task> GetTrashedImagesReadOnlyAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.AsNoTracking().Where(i => i.DeletedAt != null).ToListAsync(cancellationToken)).OrderByDescending(i => i.DeletedAt).ToList();
public async Task> GetTrashedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
=> (await context.HeroImages.Where(i => i.DeletedAt != null).ToListAsync(cancellationToken)).Where(i => i.DeletedAt < cutoff).ToList();
public async Task<(bool SetShared, bool OriginalShared)> FilesSharedWithOthersAsync(Guid imageId, string setHash, string originalKey, CancellationToken cancellationToken)
{
var others = (await context.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).Where(i => i.Id != imageId).ToList();
return (setHash.Length > 0 && others.Any(i => i.SetHash == setHash), others.Any(i => i.OriginalKey == originalKey));
}
public async Task> GetSlidesOfImageAsync(Guid imageId, CancellationToken cancellationToken)
=> await context.HeroSlides.Where(s => s.ImageId == imageId).ToListAsync(cancellationToken);
public async Task> GetImagesAsync(IReadOnlyCollection ids, CancellationToken cancellationToken)
{
// The MySQL provider cannot map a Guid list parameter in an IN clause; there are only a few dozen images, so they
// are read and filtered here.
var wanted = ids.ToHashSet();
return (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(i => wanted.Contains(i.Id)).ToList();
}
public Task CountImagesAsync(CancellationToken cancellationToken) => context.HeroImages.CountAsync(cancellationToken);
public async Task> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.AsNoTracking().Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).OrderByDescending(i => i.CreatedAt).ToList();
public async Task> GetAllSlidesReadOnlyAsync(CancellationToken cancellationToken)
=> await context.HeroSlides.AsNoTracking().ToListAsync(cancellationToken);
public async Task> GetImagesNeedingRenderAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
public async Task> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList();
public Task GetSlideAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroSlides.Include(s => s.Image).FirstOrDefaultAsync(s => s.Id == id && s.Image != null && s.Image.DeletedAt == null, cancellationToken);
public Task GetSlideFreshAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroSlides.AsNoTracking().Include(s => s.Image).FirstOrDefaultAsync(s => s.Id == id && s.Image != null && s.Image.DeletedAt == null, cancellationToken);
public async Task> GetSlidesNeedingRenderAsync(CancellationToken cancellationToken)
=> (await context.HeroSlides.Include(s => s.Image).Where(s => s.FocusX != null && s.FocusY != null).ToListAsync(cancellationToken))
.Where(s => s.Image is { DeletedAt: null } && HeroMapping.SlideNeedsRender(s))
.OrderBy(s => s.SortOrder)
.ToList();
public Task GetImageFreshAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null, cancellationToken);
public Task GetImageBySha256Async(string sha256, CancellationToken cancellationToken)
=> context.HeroImages.FirstOrDefaultAsync(i => i.OriginalSha256 == sha256, cancellationToken);
public void AddImage(HeroImage image) => context.HeroImages.Add(image);
public void RemoveImage(HeroImage image) => context.HeroImages.Remove(image);
public Task ImageInUseAsync(Guid imageId, CancellationToken cancellationToken)
=> context.HeroSlides.AnyAsync(s => s.ImageId == imageId, cancellationToken);
public async Task> GetUnattachedImagesCreatedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
{
var used = context.HeroSlides.Select(s => s.ImageId);
var candidates = await context.HeroImages.Where(i => !used.Contains(i.Id) && i.DeletedAt == null).ToListAsync(cancellationToken);
return candidates.Where(i => i.CreatedAt < cutoff).ToList();
}
public async Task> GetReferencedSetHashesAsync(CancellationToken cancellationToken)
{
// The sets of the photos and the own sets of slides with a crop of their own.
var sets = (await context.HeroImages.Select(i => i.SetHash).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal);
sets.UnionWith((await context.HeroSlides.Select(s => s.SetHash).ToListAsync(cancellationToken)).Where(h => !string.IsNullOrEmpty(h))!);
return sets;
}
public async Task> GetReferencedOriginalKeysAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.Select(i => i.OriginalKey).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal);
public void AddRetired(HeroRetiredFile file) => context.HeroRetiredFiles.Add(file);
public void RemoveRetired(HeroRetiredFile file) => context.HeroRetiredFiles.Remove(file);
public async Task> GetRetiredBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
=> (await context.HeroRetiredFiles.ToListAsync(cancellationToken)).Where(f => f.RetiredAt < cutoff).ToList();
public async Task> GetRetiredSetHashesAsync(CancellationToken cancellationToken)
=> (await context.HeroRetiredFiles.Where(f => f.Kind == "set").Select(f => f.Key).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal);
public Task SaveChangesAsync(CancellationToken cancellationToken) => context.SaveChangesAsync(cancellationToken);
}