47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace FireLance
|
|
{
|
|
public class FireLanceCRUD : FireLanceDB
|
|
{
|
|
public void InsertRecord<T>(string table, T record)
|
|
{
|
|
var collection = db.GetCollection<T>(table);
|
|
collection.InsertOne(record);
|
|
}
|
|
|
|
public void UpsertRecordById<T>(string table, Guid id, T record)
|
|
{
|
|
var collection = db.GetCollection<T>(table);
|
|
collection.ReplaceOne(
|
|
new BsonDocument("_id", id),
|
|
record,
|
|
new ReplaceOptions { IsUpsert = true });
|
|
}
|
|
|
|
public List<T> LoadRecordsFromTable<T>(string table)
|
|
{
|
|
var collection = db.GetCollection<T>(table);
|
|
return collection.Find(new BsonDocument()).ToList();
|
|
}
|
|
|
|
public T LoadRecordById<T>(string table, Guid id)
|
|
{
|
|
var collection = db.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 = db.GetCollection<T>(table);
|
|
var filter = Builders<T>.Filter.Eq("Id", id);
|
|
collection.DeleteOne(filter);
|
|
}
|
|
}
|
|
}
|