H2: Hero-module (tabellen en migratie, beheer-API, publieke GET /hero, afbeeldingsupload met focus, opschoonservice, teksten) en OpenAPI-snapshot
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
|
||||
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);
|
||||
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)
|
||||
=> await context.HeroImages.Where(i => ids.Contains(i.Id)).ToListAsync(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);
|
||||
}
|
||||
Reference in New Issue
Block a user