网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
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);
}
loading