Files
slp-modular-cms/src/SlpModularCms.Modules.Hero/Repositories/HeroRepository.cs
T
SluijsensandClaude Sonnet 5.5 6999f5b2ba Hero-admin: schakelaar Actief, destructieve knoppen rood, slide-kopjes, juiste voorvertoning in het focusvenster, status per foto
- Schakelaar (role=switch) Actief / Niet actief bij de knoppen, gedimde rij; 'Verberg voor schermlezers'; veld 'Beschrijving' binnen een groep 'Slide N'
- Alle foto's zonder complete set tonen 'Wordt verwerkt' (voortgang bij de foto die bezig is, 'in de wachtrij' bij de rest); totaal bovenaan telt ook nog niet opgeslagen foto's
- Per foto 'Verwerking afgerond' (zelfde weergave voor een hergebruikte foto), foto direct bruikbaar; niet-opgeslagen foto's worden zelf ververst (GET /hero/images/{id}), ook in een achtergrondtab
- Focusvenster toont de hele foto (full-preview in elke set, achtergrond-aanvulling voor oude sets) in plaats van de uitgesneden wide-variant (vervorming)
- Prullenbak en 'Wijzigingen weggooien' in de destructieve stijl
- upload-respons: reused (intern)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 23:26:47 +01:00

119 lines
6.7 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<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(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, 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.ToListAsync(cancellationToken)).Where(i => wanted.Contains(i.Id)).ToList();
}
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList();
public Task<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id, 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)).ToListAsync(cancellationToken);
return candidates.Where(i => i.CreatedAt < cutoff).ToList();
}
public async Task<HashSet<string>> GetReferencedSetHashesAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.Select(i => i.SetHash).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal);
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);
}