using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Dpz.Core.MongodbAccess;
public class RepositoryAny(string? connectionString) : IRepositoryAny
{
private readonly AnyAccess _access = string.IsNullOrWhiteSpace(connectionString)
? throw new ArgumentNullException(nameof(connectionString))
: new AnyAccess(connectionString);
public IMongoDatabase Database => _access.Database;
public async Task<List<string>> GetAllCollectionAsync()
{
using var cursor = await Database.ListCollectionNamesAsync();
var list = await cursor.ToListAsync();
return list;
}
public async Task<List<BsonDocument>> GetCollectionDataAsync(
string collectionName,
FilterDefinition<BsonDocument>? filter = null
)
{
filter ??= Builders<BsonDocument>.Filter.Empty;
return await Database
.GetCollection<BsonDocument>(collectionName)
.Find(filter)
.ToListAsync();
}
public async Task InsertAsync(string collectionName, ICollection<BsonDocument> source)
{
await Database.GetCollection<BsonDocument>(collectionName).InsertManyAsync(source);
}
public async Task DeleteAllAsync(string collectionName)
{
await Database
.GetCollection<BsonDocument>(collectionName)
.DeleteManyAsync(Builders<BsonDocument>.Filter.Empty);
}
private class AnyAccess(string? connectionString) : MongodbAccess(connectionString);
}