using MongoDB.Bson; using MongoDB.Driver; using System.Collections.Generic; using System.Threading.Tasks; namespace EntrustSettle.Repository.MongoRepository { public class MongoBaseRepository : IMongoBaseRepository where TEntity : class, new() { private readonly MongoDbContext _context; public MongoBaseRepository() { _context = new MongoDbContext(); } public async Task AddAsync(TEntity entity) { await _context.Db.GetCollection(typeof(TEntity).Name) .InsertOneAsync(entity); } public async Task GetAsync(int Id) { var filter = Builders.Filter.Eq("Id", Id); return await _context.Db.GetCollection(typeof(TEntity).Name) .Find(filter) .FirstOrDefaultAsync(); } public async Task GetByObjectIdAsync(string Id) { var filter = Builders.Filter.Eq("_id", ObjectId.Parse(Id)); return await _context.Db.GetCollection(typeof(TEntity).Name) .Find(filter) .FirstOrDefaultAsync(); } public async Task> GetListAsync() { return await _context.Db.GetCollection(typeof(TEntity).Name) .Find(new BsonDocument()) .ToListAsync(); } public async Task> GetListFilterAsync(FilterDefinition filter) { return await _context.Db.GetCollection(typeof(TEntity).Name) .Find(filter).ToListAsync(); } } }