init
This commit is contained in:
16
DouyinApi.Repository/MongoRepository/IMongoBaseRepository.cs
Normal file
16
DouyinApi.Repository/MongoRepository/IMongoBaseRepository.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using MongoDB.Driver;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DouyinApi.Repository.MongoRepository
|
||||
{
|
||||
|
||||
public interface IMongoBaseRepository<TEntity> where TEntity : class
|
||||
{
|
||||
Task AddAsync(TEntity entity);
|
||||
Task<TEntity> GetAsync(int Id);
|
||||
Task<List<TEntity>> GetListAsync();
|
||||
Task<TEntity> GetByObjectIdAsync(string Id);
|
||||
Task<List<TEntity>> GetListFilterAsync(FilterDefinition<TEntity> filter);
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
31
DouyinApi.Repository/MongoRepository/MongoDbContext.cs
Normal file
31
DouyinApi.Repository/MongoRepository/MongoDbContext.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
using DouyinApi.Common;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace DouyinApi.Repository.MongoRepository
|
||||
{
|
||||
|
||||
public class MongoDbContext
|
||||
{
|
||||
private readonly IMongoDatabase _database = null;
|
||||
|
||||
public MongoDbContext()
|
||||
{
|
||||
var client = new MongoClient(AppSettings.app(new string[] { "Mongo", "ConnectionString" }));
|
||||
_database = client.GetDatabase(AppSettings.app(new string[] { "Mongo", "Database" }));
|
||||
}
|
||||
|
||||
public IMongoDatabase Db
|
||||
{
|
||||
get { return _database; }
|
||||
}
|
||||
|
||||
//public IMongoCollection<TEntity> Query
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return _database.GetCollection<TEntity>(nameof(TEntity));
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user