68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
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<MediaEntry> _entries = db.GetCollection<MediaEntry>("media");
|
|
|
|
[BsonIgnoreExtraElements]
|
|
private record MediaInfo(string FilePath, int Version);
|
|
public async Task<FrozenDictionary<string, int>> GetExistingFilePathsAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var files = await _entries.Find("{}").As<MediaInfo>().ToListAsync(cancellationToken);
|
|
if (files.Count == 0)
|
|
return FrozenDictionary<string, int>.Empty;
|
|
return files.ToFrozenDictionary(m => m.FilePath, m => m.Version);
|
|
}
|
|
|
|
public async Task AddMediaBulkAsync(List<MediaEntry> entries, CancellationToken cancellationToken = default)
|
|
{
|
|
await _entries.InsertManyAsync(entries, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public async Task<List<MediaEntry>> GetEntriesInRangeAsync(MediaType mediaType, DateTime from, DateTime to, CancellationToken cancellationToken = default)
|
|
{
|
|
var filter = Builders<MediaEntry>.Filter
|
|
.And([
|
|
Builders<MediaEntry>.Filter.BitsAnySet(m => m.Type, (long)mediaType),
|
|
Builders<MediaEntry>.Filter.Gte(m => m.Date, from),
|
|
Builders<MediaEntry>.Filter.Lte(m => m.Date, to),
|
|
]);
|
|
|
|
return await _entries.Find(filter).ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task DeleteAllEntriesAsync(IEnumerable<ObjectId> ids, CancellationToken cancellationToken = default)
|
|
{
|
|
await _entries.DeleteManyAsync(e => ids.Contains(e.Id), cancellationToken);
|
|
}
|
|
|
|
public async Task DeleteAllEntriesAsync(IEnumerable<string> 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();
|
|
}
|
|
}
|
|
|
|
|
|
}
|