200 lines
13 KiB
C#
200 lines
13 KiB
C#
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
|
|
{
|
|
/// <summary>The hero of a language with its slides (and their images), tracked for update.</summary>
|
|
Task<HeroContent?> GetAsync(string language, CancellationToken cancellationToken);
|
|
|
|
/// <summary>The same, not tracked (read paths).</summary>
|
|
Task<HeroContent?> GetReadOnlyAsync(string language, CancellationToken cancellationToken);
|
|
|
|
void Add(HeroContent hero);
|
|
void AddSlide(HeroSlide slide);
|
|
void RemoveSlide(HeroSlide slide);
|
|
|
|
Task<HeroImage?> GetImageAsync(Guid id, CancellationToken cancellationToken);
|
|
Task<IReadOnlyList<HeroImage>> GetImagesAsync(IReadOnlyCollection<Guid> ids, CancellationToken cancellationToken);
|
|
/// <summary>Images whose set is missing or does not match their focus, oldest first (tracked).</summary>
|
|
Task<int> CountImagesAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>Every image including those in the trash, not tracked (the media library copy).</summary>
|
|
Task<IReadOnlyList<HeroImage>> GetAllImagesIncludingTrashedReadOnlyAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>An image whether or not it is in the trash, tracked (restore, trash).</summary>
|
|
Task<HeroImage?> GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken);
|
|
|
|
/// <summary>The photos in the trash, most recently trashed first, not tracked.</summary>
|
|
Task<IReadOnlyList<HeroImage>> GetTrashedImagesReadOnlyAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>Photos that were trashed before the cutoff, tracked (the trash is emptied for them).</summary>
|
|
Task<IReadOnlyList<HeroImage>> GetTrashedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
|
|
|
|
/// <summary>True when an image other than the given one has this set hash or this original key (the files are shared).</summary>
|
|
Task<(bool SetShared, bool OriginalShared)> FilesSharedWithOthersAsync(Guid imageId, string setHash, string originalKey, CancellationToken cancellationToken);
|
|
|
|
/// <summary>The slides that use an image, tracked (they are removed when the image goes to the trash).</summary>
|
|
Task<IReadOnlyList<HeroSlide>> GetSlidesOfImageAsync(Guid imageId, CancellationToken cancellationToken);
|
|
|
|
/// <summary>All images, newest first, not tracked (the media library lists them).</summary>
|
|
Task<IReadOnlyList<HeroImage>> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>All slides, not tracked (where each image is used).</summary>
|
|
Task<IReadOnlyList<HeroSlide>> GetAllSlidesReadOnlyAsync(CancellationToken cancellationToken);
|
|
|
|
Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>One slide with its photo, tracked (a new crop for the slide); null when there is none or its photo is in the trash.</summary>
|
|
Task<HeroSlide?> GetSlideAsync(Guid id, CancellationToken cancellationToken);
|
|
|
|
/// <summary>The slide as stored right now, with its photo, not tracked.</summary>
|
|
Task<HeroSlide?> GetSlideFreshAsync(Guid id, CancellationToken cancellationToken);
|
|
|
|
/// <summary>Slides with a crop of their own whose set is missing or does not match that crop, in hero order (tracked).</summary>
|
|
Task<IReadOnlyList<HeroSlide>> GetSlidesNeedingRenderAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>Images with a complete set made before the full-frame preview existed (tracked).</summary>
|
|
Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>The image as stored right now, not tracked.</summary>
|
|
Task<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken);
|
|
|
|
Task<HeroImage?> GetImageBySha256Async(string sha256, CancellationToken cancellationToken);
|
|
void AddImage(HeroImage image);
|
|
void RemoveImage(HeroImage image);
|
|
Task<bool> ImageInUseAsync(Guid imageId, CancellationToken cancellationToken);
|
|
|
|
Task<IReadOnlyList<HeroImage>> GetUnattachedImagesCreatedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
|
|
Task<HashSet<string>> GetReferencedSetHashesAsync(CancellationToken cancellationToken);
|
|
Task<HashSet<string>> GetReferencedOriginalKeysAsync(CancellationToken cancellationToken);
|
|
|
|
void AddRetired(HeroRetiredFile file);
|
|
void RemoveRetired(HeroRetiredFile file);
|
|
Task<IReadOnlyList<HeroRetiredFile>> GetRetiredBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
|
|
Task<HashSet<string>> GetRetiredSetHashesAsync(CancellationToken cancellationToken);
|
|
|
|
Task SaveChangesAsync(CancellationToken cancellationToken);
|
|
}
|
|
|
|
public class HeroRepository(HeroDbContext context) : IHeroRepository
|
|
{
|
|
public Task<HeroContent?> GetAsync(string language, CancellationToken cancellationToken)
|
|
=> context.Heroes.Include(h => h.Slides).ThenInclude(s => s.Image)
|
|
.FirstOrDefaultAsync(h => h.Language == language, cancellationToken);
|
|
|
|
public Task<HeroContent?> 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<HeroImage?> GetImageAsync(Guid id, CancellationToken cancellationToken)
|
|
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null, cancellationToken);
|
|
|
|
public Task<HeroImage?> GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken)
|
|
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> GetAllImagesIncludingTrashedReadOnlyAsync(CancellationToken cancellationToken)
|
|
=> (await context.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).OrderBy(i => i.CreatedAt).ToList();
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> GetTrashedImagesReadOnlyAsync(CancellationToken cancellationToken)
|
|
=> (await context.HeroImages.AsNoTracking().Where(i => i.DeletedAt != null).ToListAsync(cancellationToken)).OrderByDescending(i => i.DeletedAt).ToList();
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> 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<IReadOnlyList<HeroSlide>> GetSlidesOfImageAsync(Guid imageId, CancellationToken cancellationToken)
|
|
=> await context.HeroSlides.Where(s => s.ImageId == imageId).ToListAsync(cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> GetImagesAsync(IReadOnlyCollection<Guid> 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<int> CountImagesAsync(CancellationToken cancellationToken) => context.HeroImages.CountAsync(cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken)
|
|
=> (await context.HeroImages.AsNoTracking().Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).OrderByDescending(i => i.CreatedAt).ToList();
|
|
|
|
public async Task<IReadOnlyList<HeroSlide>> GetAllSlidesReadOnlyAsync(CancellationToken cancellationToken)
|
|
=> await context.HeroSlides.AsNoTracking().ToListAsync(cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken)
|
|
=> (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken)
|
|
=> (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList();
|
|
|
|
public Task<HeroSlide?> 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<HeroSlide?> 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<IReadOnlyList<HeroSlide>> 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<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken)
|
|
=> context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null, cancellationToken);
|
|
|
|
public Task<HeroImage?> 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<bool> ImageInUseAsync(Guid imageId, CancellationToken cancellationToken)
|
|
=> context.HeroSlides.AnyAsync(s => s.ImageId == imageId, cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<HeroImage>> 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<HashSet<string>> 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<HashSet<string>> 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<IReadOnlyList<HeroRetiredFile>> GetRetiredBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
|
|
=> (await context.HeroRetiredFiles.ToListAsync(cancellationToken)).Where(f => f.RetiredAt < cutoff).ToList();
|
|
|
|
public async Task<HashSet<string>> 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);
|
|
}
|