- Renamed CSProj for both Gsm and Firelance. - Made namespace names consistent.
53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Firelance
|
|
{
|
|
public class FirelanceCRUD : IFlcMongoDBConnecton
|
|
{
|
|
public IMongoDatabase Database { get; private set; }
|
|
public FirelanceCRUD(IMongoDatabase db)
|
|
{
|
|
Database = db;
|
|
}
|
|
|
|
public void InsertRecord<T>(string table, T record)
|
|
{
|
|
var collection = Database.GetCollection<T>(table);
|
|
collection.InsertOne(record);
|
|
}
|
|
|
|
public void UpsertRecordById<T>(string table, Guid id, T record)
|
|
{
|
|
var collection = Database.GetCollection<T>(table);
|
|
collection.ReplaceOne(
|
|
new BsonDocument("_id", id),
|
|
record,
|
|
new ReplaceOptions { IsUpsert = true });
|
|
}
|
|
|
|
public List<T> LoadRecordsFromTable<T>(string table)
|
|
{
|
|
var collection = Database.GetCollection<T>(table);
|
|
return collection.Find(new BsonDocument()).ToList();
|
|
}
|
|
|
|
public T LoadRecordById<T>(string table, Guid id)
|
|
{
|
|
var collection = Database.GetCollection<T>(table);
|
|
var filter = Builders<T>.Filter.Eq("Id", id);
|
|
|
|
return collection.Find(filter).First();
|
|
}
|
|
|
|
public void DeleteRecordById<T>(string table, Guid id)
|
|
{
|
|
var collection = Database.GetCollection<T>(table);
|
|
var filter = Builders<T>.Filter.Eq("Id", id);
|
|
collection.DeleteOne(filter);
|
|
}
|
|
}
|
|
}
|