44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using Model;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
|
|
namespace Repository
|
|
{
|
|
public class DataRepository
|
|
{
|
|
private static string COLLECTION_NAME = "data";
|
|
private string _connectionString = "mongodb://mongo:mongo@localhost:27017";
|
|
private string _databaseName = "mongo";
|
|
private IMongoCollection<DataModel> _dataCollection;
|
|
private MongoClient _mongoClient;
|
|
|
|
public DataRepository(string connectionString, string databaseName)
|
|
{
|
|
this._connectionString = connectionString;
|
|
this._databaseName = databaseName;
|
|
this._mongoClient = new MongoClient(this._connectionString);
|
|
|
|
var db = this._mongoClient.GetDatabase(this._databaseName);
|
|
this._dataCollection = db.GetCollection<DataModel>(COLLECTION_NAME);
|
|
}
|
|
|
|
public DataRepository()
|
|
{
|
|
this._mongoClient = new MongoClient(this._connectionString);
|
|
|
|
var db = this._mongoClient.GetDatabase(this._databaseName);
|
|
this._dataCollection = db.GetCollection<DataModel>(COLLECTION_NAME);
|
|
}
|
|
|
|
public async Task Save(DataModel data)
|
|
{
|
|
await this._dataCollection.InsertOneAsync(data);
|
|
}
|
|
|
|
public async Task<DataModel> FindById(string id)
|
|
{
|
|
var idFilter = Builders<DataModel>.Filter.Eq(data => data.id, id);
|
|
return await this._dataCollection.Find(idFilter).FirstOrDefaultAsync();
|
|
}
|
|
}
|
|
} |