automated-testing-demo/consumer/Repository/DataRepository.cs

44 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-06-14 15:27:10 +00:00
using Model;
2024-06-14 21:06:24 +00:00
using MongoDB.Bson;
2024-06-14 15:27:10 +00:00
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);
}
2024-06-14 21:06:24 +00:00
public async Task Save(DataModel data)
2024-06-14 15:27:10 +00:00
{
await this._dataCollection.InsertOneAsync(data);
}
2024-06-14 21:06:24 +00:00
public async Task<DataModel> FindById(string id)
{
var idFilter = Builders<DataModel>.Filter.Eq(data => data.id, id);
return await this._dataCollection.Find(idFilter).FirstOrDefaultAsync();
}
2024-06-14 15:27:10 +00:00
}
}