From dc906379daa8f9ed705299e93dbca7e2961be36d Mon Sep 17 00:00:00 2001 From: j4ck <50369519+j4cegh@users.noreply.github.com> Date: Thu, 2 Jun 2022 22:55:16 +0300 Subject: [PATCH] Add base file support :trollface: --- RSND/Core/Database.cs | 51 +++++++++++++++++++++++++-------- RSND/Core/DbClient.cs | 27 +---------------- RSND/Core/DbInternals/Column.cs | 10 ++++--- RSND/Core/Response.cs | 2 +- RSND/RsndMain.cs | 5 +++- 5 files changed, 51 insertions(+), 44 deletions(-) diff --git a/RSND/Core/Database.cs b/RSND/Core/Database.cs index 6c160ae..67581fe 100644 --- a/RSND/Core/Database.cs +++ b/RSND/Core/Database.cs @@ -7,18 +7,46 @@ namespace RSND.Core; public class Database { - List tables = new(); + private string Name { get; set; } + + private List
_tables = new(); + public Database(string name) + { + Name = name; + } + + public void SetupFiles() + { + if (File.Exists($"{Name}.json")) + { + Console.WriteLine($"{Name} flag123"); + var json = File.ReadAllText($"{Name}.json"); + var db = JsonConvert.DeserializeObject>(json); + if (db != null) + _tables = db; + } + else + { + Console.WriteLine($"{Name} flag"); + File.Create($"{Name}.json"); + } + } + public void Save() + { + var json = JsonConvert.SerializeObject(_tables); + File.WriteAllText($"{Name}.json", json); + } + public void CreateTable(Table query) { - var matches = tables.FirstOrDefault(x => x.Name == query.Name); - - if (matches != null) + if (_tables.Any(x => x.Name == query.Name)) { - tables.Remove(matches); + _tables.RemoveAll(x => x.Name == query.Name); } - tables.Add(query); + _tables.Add(query); + Save(); } private static string GetRowsJson(Row[]? rows) @@ -28,20 +56,19 @@ public class Database public string GetValue(GetQuery? query) { - Console.WriteLine(tables.Count.ToString().Pastel("#ff0000")); + Console.WriteLine(_tables.Count.ToString().Pastel("#ff0000")); var table = query?.Table; var where = query?.Where; var split = where?.Split("|"); var colName = split?[0]; var condition = split?[1]; - var param = split?[2]; + var value = split?[2]; if (query?.Select != "*") return ""; - - - var tableToReturn = tables.Find(x => x.Name == table); - var rows = tableToReturn?.Rows?.Where(x => x.Columns?.FirstOrDefault(y => y.Data.Key == colName)?.Data.Value == param).ToArray(); + + var tableToReturn = _tables.Find(x => x.Name == table); + var rows = tableToReturn?.Rows?.Where(x => x.Columns?.FirstOrDefault(y => y.Name == colName)?.Value == value).ToArray(); return GetRowsJson(rows); } diff --git a/RSND/Core/DbClient.cs b/RSND/Core/DbClient.cs index 115174d..0e214d2 100644 --- a/RSND/Core/DbClient.cs +++ b/RSND/Core/DbClient.cs @@ -14,30 +14,6 @@ public class DbClient public DbClient(IWebSocketConnection socket) { _socket = socket; - - RsndMain.Db.CreateTable(new Table - { - Name = "j4ces.table", - Rows = new [] - { - new Row - { - Columns = new [] - { - new Column("id", "1"), - new Column("amog us", "is THE GAME") - } - }, - new Row - { - Columns = new [] - { - new Column("id", "1"), - new Column("fortnite", "is THE GAME") - } - } - } - }); } private void InvalidQuery() @@ -63,8 +39,7 @@ public class DbClient { if (query == null || queryType == null) InvalidQuery(); - - // check if + switch (queryType) { case QueryType.GetValue: diff --git a/RSND/Core/DbInternals/Column.cs b/RSND/Core/DbInternals/Column.cs index caced45..7d2e649 100644 --- a/RSND/Core/DbInternals/Column.cs +++ b/RSND/Core/DbInternals/Column.cs @@ -2,10 +2,12 @@ public class Column { - public KeyValuePair Data; - - public Column(string name, string value) + public Column(string? name, string? value) { - Data = new KeyValuePair(name, value); + Name = name; + Value = value; } + + public string? Name { get; set; } + public string? Value { get; set; } } \ No newline at end of file diff --git a/RSND/Core/Response.cs b/RSND/Core/Response.cs index cab2c21..deec7fb 100644 --- a/RSND/Core/Response.cs +++ b/RSND/Core/Response.cs @@ -3,5 +3,5 @@ public class Response { public bool Success { get; set; } - public string Message { get; set; } + public string? Message { get; set; } } \ No newline at end of file diff --git a/RSND/RsndMain.cs b/RSND/RsndMain.cs index 10b4a81..88d5ebd 100644 --- a/RSND/RsndMain.cs +++ b/RSND/RsndMain.cs @@ -5,10 +5,13 @@ namespace RSND; public static class RsndMain { - public static Database Db = new(); + public static Database Db = new(Environment.GetCommandLineArgs()[1]); public static void Run() { + Db.SetupFiles(); + Db.Save(); + WebSocketServer server = new WebSocketServer("ws://0.0.0.0:7878"); FleckLog.Level = LogLevel.Error;