Functional MongoDB CRUD
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
using FireLance.Interfaces;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FireLance
|
||||
{
|
||||
public class DatabaseConnection : IDatabaseConnection
|
||||
{
|
||||
IMongoDatabase mongoDatabase;
|
||||
IMongoDatabase db;
|
||||
|
||||
public void CloseCurrentConnection()
|
||||
{
|
||||
@@ -16,7 +19,7 @@ namespace FireLance
|
||||
{
|
||||
int connectionCnt = 0;
|
||||
|
||||
try { connectionCnt = mongoDatabase.Client.ListDatabases().ToList().Count; }
|
||||
try { connectionCnt = db.Client.ListDatabases().ToList().Count; }
|
||||
catch {}
|
||||
|
||||
return (connectionCnt > 0) ? true : false;
|
||||
@@ -25,13 +28,45 @@ namespace FireLance
|
||||
public void OpenConnection(string dbLocation, int port)
|
||||
{
|
||||
var client = new MongoClient($"mongodb://{dbLocation}:{port}");
|
||||
mongoDatabase = client.GetDatabase(dbLocation);
|
||||
var settings = new MongoDatabaseSettings { GuidRepresentation = GuidRepresentation.Standard };
|
||||
|
||||
db = client.GetDatabase(dbLocation, settings);
|
||||
}
|
||||
|
||||
public void InsertRecord<T>(string table, T record)
|
||||
{
|
||||
var collection = mongoDatabase.GetCollection<T>(table);
|
||||
var collection = db.GetCollection<T>(table);
|
||||
collection.InsertOne(record);
|
||||
}
|
||||
|
||||
public void UpsertRecord<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> LoadRecords<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
using System;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FireLance.Model
|
||||
namespace FireLance.Models
|
||||
{
|
||||
public class SaveDataModel
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public int GameID { get; set; }
|
||||
public string SHA256Hash { get; set; }
|
||||
public string Description { get; set; }
|
||||
@@ -12,3 +18,4 @@ namespace FireLance.Model
|
||||
public uint BackupSize { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user