Browse Source

Add base file support :trollface:

master
j4ck 2 years ago
parent
commit
dc906379da
  1. 51
      RSND/Core/Database.cs
  2. 27
      RSND/Core/DbClient.cs
  3. 10
      RSND/Core/DbInternals/Column.cs
  4. 2
      RSND/Core/Response.cs
  5. 5
      RSND/RsndMain.cs

51
RSND/Core/Database.cs

@ -7,18 +7,46 @@ namespace RSND.Core;
public class Database
{
List<Table> tables = new();
private string Name { get; set; }
private List<Table> _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<List<Table>>(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);
}

27
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:

10
RSND/Core/DbInternals/Column.cs

@ -2,10 +2,12 @@
public class Column
{
public KeyValuePair<string, string> Data;
public Column(string name, string value)
public Column(string? name, string? value)
{
Data = new KeyValuePair<string, string>(name, value);
Name = name;
Value = value;
}
public string? Name { get; set; }
public string? Value { get; set; }
}

2
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; }
}

5
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;

Loading…
Cancel
Save