using AZKiServer.Models; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; using System.Collections.Frozen; namespace AZKiServer.Services; public class MediaService(IMongoDatabase db) { public readonly IMongoCollection _entries = db.GetCollection("media"); [BsonIgnoreExtraElements] private record class MediaInfo(string Filepath, int Version); public async Task> GetExistingFilePathsAsync(CancellationToken cancellationToken = default) { var files = await _entries.Find("{}").As().ToListAsync(cancellationToken); if (files.Count == 0) return FrozenDictionary.Empty; return files.ToFrozenDictionary(m => m.Filepath, m => m.Version); } public async Task AddMediaBulkAsync(List entries, CancellationToken cancellationToken = default) { await _entries.InsertManyAsync(entries, cancellationToken: cancellationToken); } public async Task> GetEntriesInRangeAsync(MediaType mediaType, DateTime from, DateTime to, CancellationToken cancellationToken = default) { var filter = Builders.Filter .And([ Builders.Filter.BitsAnySet(m => m.Type, (long)mediaType), Builders.Filter.Gte(m => m.Date, from), Builders.Filter.Lte(m => m.Date, to), ]); return await _entries.Find(filter).ToListAsync(cancellationToken); } public async Task DeleteAllEntriesAsync(IEnumerable ids, CancellationToken cancellationToken = default) { await _entries.DeleteManyAsync(e => ids.Contains(e.Id), cancellationToken); } public async Task DeleteAllEntriesAsync(IEnumerable paths, CancellationToken cancellationToken = default) { await _entries.DeleteManyAsync(e => paths.Contains(e.Filepath), cancellationToken); } #if DEBUG public async Task DeleteAllEntriesAsync(CancellationToken cancellationToken = default) { await _entries.DeleteManyAsync("{}", cancellationToken); } #endif public class IndexCreation : BackgroundService { protected override Task ExecuteAsync(CancellationToken stoppingToken) { throw new NotImplementedException(); } } }