Functional GFS implementation.
- Mass refactoring. - Decoupled Firelance CRUD from DB.
This commit is contained in:
+46
-22
@@ -4,21 +4,20 @@ using Gsm;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
|
|
||||||
namespace DevConsole
|
namespace DevConsole
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
public static readonly GsmRO gsmRO = new GsmRO();
|
public static readonly GsmRO gsmRO = new GsmRO();
|
||||||
public static FireLanceCRUD fireLanceCRUD = new FireLanceCRUD();
|
public static FirelanceMgr flcMgr = new FirelanceMgr();
|
||||||
|
public static FirelanceCRUD flcCRUD;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
ConnectToGsmDb();
|
ConnectToGsmDb();
|
||||||
ConnectToFlcDb();
|
ConnectToFirelanceManager();
|
||||||
Console.WriteLine("");
|
Console.WriteLine("");
|
||||||
while (true) { CommandsMenu(); }
|
while (true) { CommandsMenu(); }
|
||||||
}
|
}
|
||||||
@@ -61,10 +60,11 @@ namespace DevConsole
|
|||||||
Console.WriteLine($"GSMParser Connection State: {gsmRO.IsConnectionOpen()}");
|
Console.WriteLine($"GSMParser Connection State: {gsmRO.IsConnectionOpen()}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ConnectToFlcDb()
|
private static void ConnectToFirelanceManager()
|
||||||
{
|
{
|
||||||
fireLanceCRUD.OpenConnection("sr3", 27017);
|
flcMgr.OpenConnection("sr3", 27017);
|
||||||
Console.WriteLine($"FireLance Connection State: {fireLanceCRUD.IsConnectionOpen()}");
|
flcCRUD = new FirelanceCRUD(flcMgr.DBInstance);
|
||||||
|
Console.WriteLine($"FireLance Connection State: {flcMgr.IsConnectionOpen()}");
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Command Options
|
#region Command Options
|
||||||
@@ -73,7 +73,7 @@ namespace DevConsole
|
|||||||
Console.WriteLine("[GsmDb]------------");
|
Console.WriteLine("[GsmDb]------------");
|
||||||
Console.WriteLine("A. Open DB");
|
Console.WriteLine("A. Open DB");
|
||||||
Console.WriteLine("B. Close DB");
|
Console.WriteLine("B. Close DB");
|
||||||
Console.WriteLine("[FlcDb]------------");
|
Console.WriteLine("[Firelance]------------");
|
||||||
Console.WriteLine("C. Open DB");
|
Console.WriteLine("C. Open DB");
|
||||||
Console.WriteLine("D. Close DB (Unsupported)");
|
Console.WriteLine("D. Close DB (Unsupported)");
|
||||||
Console.WriteLine("X. <--- Go back\n");
|
Console.WriteLine("X. <--- Go back\n");
|
||||||
@@ -91,10 +91,10 @@ namespace DevConsole
|
|||||||
gsmRO.CloseCurrentConnection();
|
gsmRO.CloseCurrentConnection();
|
||||||
break;
|
break;
|
||||||
case "C":
|
case "C":
|
||||||
ConnectToFlcDb();
|
ConnectToFirelanceManager();
|
||||||
break;
|
break;
|
||||||
case "D":
|
case "D":
|
||||||
fireLanceCRUD.CloseCurrentConnection();
|
flcMgr.CloseCurrentConnection();
|
||||||
break;
|
break;
|
||||||
case "X":
|
case "X":
|
||||||
break;
|
break;
|
||||||
@@ -152,10 +152,12 @@ namespace DevConsole
|
|||||||
|
|
||||||
private static void CmdFlcActions()
|
private static void CmdFlcActions()
|
||||||
{
|
{
|
||||||
Console.WriteLine("A. FireLance Write Predefined MasterGameRecord");
|
Console.WriteLine("A. Write predefined MasterGameRecord");
|
||||||
Console.WriteLine("B. FireLance Read Predefined MasterGameRecord");
|
Console.WriteLine("B. Read predefined MasterGameRecord");
|
||||||
Console.WriteLine("C. FireLance Insert Save Entry into existing MasterGameRecord");
|
Console.WriteLine("C. Insert save entry into existing MasterGameRecord");
|
||||||
Console.WriteLine("D. FireLance Remove Save Entry from existing MasterGameRecord");
|
Console.WriteLine("D. Remove save entry from existing MasterGameRecord");
|
||||||
|
Console.WriteLine("E. Upload save entry using GridFS");
|
||||||
|
Console.WriteLine("F. Download save entry using GridFS");
|
||||||
Console.WriteLine("X. <--- Go back\n");
|
Console.WriteLine("X. <--- Go back\n");
|
||||||
Console.Write(":");
|
Console.Write(":");
|
||||||
|
|
||||||
@@ -176,6 +178,12 @@ namespace DevConsole
|
|||||||
case "D":
|
case "D":
|
||||||
FlcRemoveSaveEntryAction();
|
FlcRemoveSaveEntryAction();
|
||||||
break;
|
break;
|
||||||
|
case "E":
|
||||||
|
FlcUploadToGridFsAction();
|
||||||
|
break;
|
||||||
|
case "F":
|
||||||
|
FlcDownloadToGridFsAction();
|
||||||
|
break;
|
||||||
case "X":
|
case "X":
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -205,12 +213,12 @@ namespace DevConsole
|
|||||||
masterGameRecord.GameEntry = gameEntry;
|
masterGameRecord.GameEntry = gameEntry;
|
||||||
masterGameRecord.SaveGameEntries = saveGameEntries;
|
masterGameRecord.SaveGameEntries = saveGameEntries;
|
||||||
|
|
||||||
fireLanceCRUD.InsertRecord("GameRecords", masterGameRecord);
|
flcCRUD.InsertRecord("GameRecords", masterGameRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void FlcReadMasterGameRecordAction()
|
static void FlcReadMasterGameRecordAction()
|
||||||
{
|
{
|
||||||
var masterGameRecords = fireLanceCRUD.LoadRecordsFromTable<MasterGameRecordModel>("GameRecords");
|
var masterGameRecords = flcCRUD.LoadRecordsFromTable<MasterGameRecordModel>("GameRecords");
|
||||||
foreach (var record in masterGameRecords)
|
foreach (var record in masterGameRecords)
|
||||||
{
|
{
|
||||||
Console.WriteLine(record.Id);
|
Console.WriteLine(record.Id);
|
||||||
@@ -234,21 +242,37 @@ namespace DevConsole
|
|||||||
|
|
||||||
private static void FlcRemoveSaveEntryAction()
|
private static void FlcRemoveSaveEntryAction()
|
||||||
{
|
{
|
||||||
Guid firstMasterGameRecordGuid = fireLanceCRUD.LoadRecordsFromTable<MasterGameRecordModel>("GameRecords").First().Id;
|
Guid firstMasterGameRecordGuid = flcCRUD.LoadRecordsFromTable<MasterGameRecordModel>("GameRecords").First().Id;
|
||||||
var masterGameRecordToRemove = fireLanceCRUD.LoadRecordById<MasterGameRecordModel>("GameRecords", firstMasterGameRecordGuid);
|
var masterGameRecordToRemove = flcCRUD.LoadRecordById<MasterGameRecordModel>("GameRecords", firstMasterGameRecordGuid);
|
||||||
Guid firstSaveGameEntryGuid = masterGameRecordToRemove.SaveGameEntries.Select(x => x.Id).First();
|
Guid firstSaveGameEntryGuid = masterGameRecordToRemove.SaveGameEntries.Select(x => x.Id).First();
|
||||||
masterGameRecordToRemove.SaveGameEntries.RemoveAll(x => x.Id == firstSaveGameEntryGuid);
|
masterGameRecordToRemove.SaveGameEntries.RemoveAll(x => x.Id == firstSaveGameEntryGuid);
|
||||||
|
|
||||||
fireLanceCRUD.UpsertRecordById("GameRecords", firstMasterGameRecordGuid, masterGameRecordToRemove);
|
flcCRUD.UpsertRecordById("GameRecords", firstMasterGameRecordGuid, masterGameRecordToRemove);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void FlcInsertSaveEntryAction()
|
private static void FlcInsertSaveEntryAction()
|
||||||
{
|
{
|
||||||
Guid firstMasterGameRecordGuid = fireLanceCRUD.LoadRecordsFromTable<MasterGameRecordModel>("GameRecords").First().Id;
|
Guid firstMasterGameRecordGuid = flcCRUD.LoadRecordsFromTable<MasterGameRecordModel>("GameRecords").First().Id;
|
||||||
var masterGameRecordToInsert = fireLanceCRUD.LoadRecordById<MasterGameRecordModel>("GameRecords", firstMasterGameRecordGuid);
|
var masterGameRecordToInsert = flcCRUD.LoadRecordById<MasterGameRecordModel>("GameRecords", firstMasterGameRecordGuid);
|
||||||
masterGameRecordToInsert.SaveGameEntries.Add(new SaveGameEntriesModel());
|
masterGameRecordToInsert.SaveGameEntries.Add(new SaveGameEntriesModel());
|
||||||
|
|
||||||
fireLanceCRUD.UpsertRecordById("GameRecords", firstMasterGameRecordGuid, masterGameRecordToInsert);
|
flcCRUD.UpsertRecordById("GameRecords", firstMasterGameRecordGuid, masterGameRecordToInsert);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FlcUploadToGridFsAction()
|
||||||
|
{
|
||||||
|
var flcGFS = new FirelanceGFS(flcMgr.DBInstance);
|
||||||
|
var ulResult = flcGFS.UploadFile("TestFile", @"C:\Users\Dunestorm\Downloads\OpenMW-0.46.0-win64.exe");
|
||||||
|
Console.WriteLine(ulResult.Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FlcDownloadToGridFsAction()
|
||||||
|
{
|
||||||
|
var objId = "5f00a560b65a2f02d0c7996e";
|
||||||
|
|
||||||
|
var flcGFC = new FirelanceGFS(flcMgr.DBInstance);
|
||||||
|
var dlResult = flcGFC.DownloadFile(new MongoDB.Bson.ObjectId(objId), @$"D:\Temp\{objId}.exe");
|
||||||
|
Console.WriteLine($"Download Successful: {dlResult.Result}");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GSMParser", "GSMParser\GSMP
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevConsole", "DevConsole\DevConsole.csproj", "{6B7A9B29-420B-4F58-975E-3D72A3D95314}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevConsole", "DevConsole\DevConsole.csproj", "{6B7A9B29-420B-4F58-975E-3D72A3D95314}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireLance", "FireLance\FireLance.csproj", "{6ACF5E6F-42EE-4FD6-8657-51E6380D422C}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Firelance", "FireLance\Firelance.csproj", "{6ACF5E6F-42EE-4FD6-8657-51E6380D422C}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
|
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
|
||||||
|
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.10.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -5,17 +5,23 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace FireLance
|
namespace FireLance
|
||||||
{
|
{
|
||||||
public class FireLanceCRUD : FireLanceDB
|
public class FirelanceCRUD : IFlcMongoConnecton
|
||||||
{
|
{
|
||||||
|
public IMongoDatabase DBInstance { get; set; }
|
||||||
|
public FirelanceCRUD(IMongoDatabase db)
|
||||||
|
{
|
||||||
|
DBInstance = db;
|
||||||
|
}
|
||||||
|
|
||||||
public void InsertRecord<T>(string table, T record)
|
public void InsertRecord<T>(string table, T record)
|
||||||
{
|
{
|
||||||
var collection = db.GetCollection<T>(table);
|
var collection = DBInstance.GetCollection<T>(table);
|
||||||
collection.InsertOne(record);
|
collection.InsertOne(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpsertRecordById<T>(string table, Guid id, T record)
|
public void UpsertRecordById<T>(string table, Guid id, T record)
|
||||||
{
|
{
|
||||||
var collection = db.GetCollection<T>(table);
|
var collection = DBInstance.GetCollection<T>(table);
|
||||||
collection.ReplaceOne(
|
collection.ReplaceOne(
|
||||||
new BsonDocument("_id", id),
|
new BsonDocument("_id", id),
|
||||||
record,
|
record,
|
||||||
@@ -24,13 +30,13 @@ namespace FireLance
|
|||||||
|
|
||||||
public List<T> LoadRecordsFromTable<T>(string table)
|
public List<T> LoadRecordsFromTable<T>(string table)
|
||||||
{
|
{
|
||||||
var collection = db.GetCollection<T>(table);
|
var collection = DBInstance.GetCollection<T>(table);
|
||||||
return collection.Find(new BsonDocument()).ToList();
|
return collection.Find(new BsonDocument()).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public T LoadRecordById<T>(string table, Guid id)
|
public T LoadRecordById<T>(string table, Guid id)
|
||||||
{
|
{
|
||||||
var collection = db.GetCollection<T>(table);
|
var collection = DBInstance.GetCollection<T>(table);
|
||||||
var filter = Builders<T>.Filter.Eq("Id", id);
|
var filter = Builders<T>.Filter.Eq("Id", id);
|
||||||
|
|
||||||
return collection.Find(filter).First();
|
return collection.Find(filter).First();
|
||||||
@@ -38,7 +44,7 @@ namespace FireLance
|
|||||||
|
|
||||||
public void DeleteRecordById<T>(string table, Guid id)
|
public void DeleteRecordById<T>(string table, Guid id)
|
||||||
{
|
{
|
||||||
var collection = db.GetCollection<T>(table);
|
var collection = DBInstance.GetCollection<T>(table);
|
||||||
var filter = Builders<T>.Filter.Eq("Id", id);
|
var filter = Builders<T>.Filter.Eq("Id", id);
|
||||||
collection.DeleteOne(filter);
|
collection.DeleteOne(filter);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Driver.GridFS;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FireLance
|
||||||
|
{
|
||||||
|
public class FirelanceGFS
|
||||||
|
{
|
||||||
|
private GridFSBucket gridFSBucket;
|
||||||
|
|
||||||
|
public FirelanceGFS(IMongoDatabase db)
|
||||||
|
{
|
||||||
|
gridFSBucket = new GridFSBucket(db, new GridFSBucketOptions
|
||||||
|
{
|
||||||
|
BucketName = "SaveGames"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> UploadFile(string fileName, string location)
|
||||||
|
{
|
||||||
|
ObjectId fileId;
|
||||||
|
using (var fs = new FileStream(location, FileMode.Open))
|
||||||
|
{
|
||||||
|
fileId = await gridFSBucket.UploadFromStreamAsync(fileName, fs);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileId.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DownloadFile(ObjectId id, string saveLocation)
|
||||||
|
{
|
||||||
|
using (var fs = new FileStream(saveLocation, FileMode.Create))
|
||||||
|
{
|
||||||
|
await gridFSBucket.DownloadToStreamAsync(id, fs);
|
||||||
|
}
|
||||||
|
|
||||||
|
return VerifyDownloadedFile(saveLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool VerifyDownloadedFile(string path) { return File.Exists(path); }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,31 +5,31 @@ using System;
|
|||||||
|
|
||||||
namespace FireLance
|
namespace FireLance
|
||||||
{
|
{
|
||||||
public abstract class FireLanceDB : IDatabaseConnection
|
public class FirelanceMgr : IFlcDbConnection, IFlcMongoConnecton
|
||||||
{
|
{
|
||||||
internal IMongoDatabase db;
|
public IMongoDatabase DBInstance { get; private set; }
|
||||||
|
|
||||||
public void OpenConnection(string dbLocation, int port)
|
public void OpenConnection(string dbLocation, int port)
|
||||||
{
|
{
|
||||||
var client = new MongoClient($"mongodb://{dbLocation}:{port}");
|
var client = new MongoClient($"mongodb://{dbLocation}:{port}");
|
||||||
var settings = new MongoDatabaseSettings { GuidRepresentation = GuidRepresentation.Standard };
|
var settings = new MongoDatabaseSettings { GuidRepresentation = GuidRepresentation.Standard };
|
||||||
|
|
||||||
db = client.GetDatabase(dbLocation, settings);
|
DBInstance = client.GetDatabase(dbLocation, settings);
|
||||||
}
|
|
||||||
|
|
||||||
public void CloseCurrentConnection()
|
|
||||||
{
|
|
||||||
throw new NotSupportedException("MongoDB does not support closing connections.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsConnectionOpen()
|
public bool IsConnectionOpen()
|
||||||
{
|
{
|
||||||
int connectionCnt = 0;
|
int connectionCnt = 0;
|
||||||
|
|
||||||
try { connectionCnt = db.Client.ListDatabases().ToList().Count; }
|
try { connectionCnt = DBInstance.Client.ListDatabases().ToList().Count; }
|
||||||
catch { }
|
catch { }
|
||||||
|
|
||||||
return (connectionCnt > 0) ? true : false;
|
return (connectionCnt > 0) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CloseCurrentConnection()
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("MongoDB does not support closing connections.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
namespace FireLance.Interfaces
|
namespace FireLance.Interfaces
|
||||||
{
|
{
|
||||||
public interface IDatabaseConnection
|
public interface IFlcDbConnection
|
||||||
{
|
{
|
||||||
public void OpenConnection(string dbLocation, int port);
|
public void OpenConnection(string dbLocation, int port);
|
||||||
public void CloseCurrentConnection();
|
public void CloseCurrentConnection();
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace FireLance
|
||||||
|
{
|
||||||
|
public interface IFlcMongoConnecton
|
||||||
|
{
|
||||||
|
public IMongoDatabase DBInstance { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\FireLance\FireLance.csproj" />
|
<ProjectReference Include="..\FireLance\Firelance.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ using FireLance.Interfaces;
|
|||||||
|
|
||||||
namespace Gsm
|
namespace Gsm
|
||||||
{
|
{
|
||||||
public abstract class GsmDB : IDatabaseConnection
|
public abstract class GsmDB : IFlcDbConnection
|
||||||
{
|
{
|
||||||
internal SqliteConnection DBConnection = new SqliteConnection();
|
internal SqliteConnection DBConnection = new SqliteConnection();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user