Able to create and read data from the DB
This commit is contained in:
@@ -14,6 +14,7 @@ public class UserRepository
|
||||
private string _connectionString;
|
||||
private string _databaseName;
|
||||
private string _tableName;
|
||||
private readonly IMongoCollection<User> _bookCollection;
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -23,17 +24,35 @@ public class UserRepository
|
||||
this._connectionString = connectionString;
|
||||
this._databaseName = "TextSender";
|
||||
this._tableName = "Users";
|
||||
var client = this.InitializeClient();
|
||||
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<User>(this._tableName);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Metods
|
||||
public void CreateUser(User user)
|
||||
{
|
||||
var client = this.InitializeClient();
|
||||
|
||||
var coll = client.GetDatabase(this._databaseName).GetCollection<User>(this._tableName);
|
||||
coll.InsertOne(user);
|
||||
this._bookCollection.InsertOne(user);
|
||||
var i = 0;
|
||||
}
|
||||
|
||||
public List<User> RetrieveAllUsers()
|
||||
{
|
||||
return this._bookCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public void Update(User user)
|
||||
{
|
||||
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
||||
this._bookCollection.ReplaceOne(filter, user);
|
||||
}
|
||||
|
||||
public void Delete(User user)
|
||||
{
|
||||
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
||||
this._bookCollection.DeleteOne(filter);
|
||||
}
|
||||
|
||||
private MongoClient InitializeClient()
|
||||
{
|
||||
return new MongoClient(this._connectionString);
|
||||
|
||||
Reference in New Issue
Block a user