init
This commit is contained in:
57
DouyinApi.Repository/MongoRepository/MongoBaseRepository.cs
Normal file
57
DouyinApi.Repository/MongoRepository/MongoBaseRepository.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DouyinApi.Repository.MongoRepository
|
||||
{
|
||||
|
||||
|
||||
public class MongoBaseRepository<TEntity> : IMongoBaseRepository<TEntity> where TEntity : class, new()
|
||||
{
|
||||
private readonly MongoDbContext _context;
|
||||
|
||||
public MongoBaseRepository()
|
||||
{
|
||||
_context = new MongoDbContext();
|
||||
}
|
||||
|
||||
public async Task AddAsync(TEntity entity)
|
||||
{
|
||||
await _context.Db.GetCollection<TEntity>(typeof(TEntity).Name)
|
||||
.InsertOneAsync(entity);
|
||||
}
|
||||
|
||||
public async Task<TEntity> GetAsync(int Id)
|
||||
{
|
||||
var filter = Builders<TEntity>.Filter.Eq("Id", Id);
|
||||
|
||||
return await _context.Db.GetCollection<TEntity>(typeof(TEntity).Name)
|
||||
.Find(filter)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<TEntity> GetByObjectIdAsync(string Id)
|
||||
{
|
||||
var filter = Builders<TEntity>.Filter.Eq("_id", ObjectId.Parse(Id));
|
||||
|
||||
return await _context.Db.GetCollection<TEntity>(typeof(TEntity).Name)
|
||||
.Find(filter)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<List<TEntity>> GetListAsync()
|
||||
{
|
||||
return await _context.Db.GetCollection<TEntity>(typeof(TEntity).Name)
|
||||
.Find(new BsonDocument())
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<TEntity>> GetListFilterAsync(FilterDefinition<TEntity> filter)
|
||||
{
|
||||
|
||||
return await _context.Db.GetCollection<TEntity>(typeof(TEntity).Name)
|
||||
.Find(filter).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user