I can't even count the changes 💀

This commit is contained in:
j4ck 2022-06-03 22:02:22 +03:00
parent 232c5a5421
commit 2b87d05e9e
8 changed files with 46 additions and 10 deletions

View File

@ -23,6 +23,8 @@ public class Database
Console.WriteLine($"[{Name}] The file exists."); Console.WriteLine($"[{Name}] The file exists.");
var json = File.ReadAllText($"{Name}.json"); var json = File.ReadAllText($"{Name}.json");
var db = JsonConvert.DeserializeObject<List<Table>>(json); var db = JsonConvert.DeserializeObject<List<Table>>(json);
// if this is null, then something is wrong with the file
if (db != null) if (db != null)
_tables = db; _tables = db;
} }
@ -61,7 +63,7 @@ public class Database
public string GetValue(GetQuery? query) public string GetValue(GetQuery? query)
{ {
Console.WriteLine(_tables.Count.ToString().Pastel("#ff0000")); Console.WriteLine(_tables.Count.ToString().Pastel("#ff0000"));
var table = query?.Table; var table = query?.TableName;
var where = query?.Where; var where = query?.Where;
var split = where?.Split("|"); var split = where?.Split("|");
@ -72,11 +74,30 @@ public class Database
if (query?.Select != "*") return ""; if (query?.Select != "*") return "";
var tableToReturn = _tables.Find(x => x.Name == table); var tableToReturn = _tables.Find(x => x.Name == table);
var rows = tableToReturn?.Rows?.Where(x => x.Columns?.FirstOrDefault(y => y.Name == colName)?.Value == value).ToArray(); var rows = tableToReturn?.Rows?.Where(x => x.Columns?.FirstOrDefault(y => y.Name == colName)?.Value == value)
.ToArray();
return JsonConvert.SerializeObject(rows); return JsonConvert.SerializeObject(rows);
} }
public void SetValue(SetQuery? query)
{
var table = query?.TableName;
var key = query?.Key;
var value = query?.Value;
var newValue = query?.NewValue;
Console.WriteLine(table);
var tableToReturn = _tables.Find(x => x.Name == table);
Console.WriteLine(tableToReturn == null ? "shits null" : "shits not null");
Console.WriteLine(key);
var row = tableToReturn?.Rows?.FirstOrDefault(x => x.Columns?.FirstOrDefault(y => y.Name == key)?.Value == value);
var column = row?.Columns?.FirstOrDefault(x => x.Name == key);
column.Value = newValue;
Console.WriteLine(column.Value);
}
public static void Loop() public static void Loop()
{ {
while (true) while (true)

View File

@ -50,7 +50,12 @@ public class DbClient
Console.WriteLine($"Sent: {queryResult}".Pastel("#71C562")); Console.WriteLine($"Sent: {queryResult}".Pastel("#71C562"));
break; break;
} }
case QueryType.SetValue:
{
SetQuery setQuery = JsonConvert.DeserializeObject<SetQuery>(query);
RsndMain.Db.SetValue(setQuery);
break;
}
case QueryType.CreateTable: case QueryType.CreateTable:
{ {
CreateTableQuery createTableQuery = JsonConvert.DeserializeObject<CreateTableQuery>(query); CreateTableQuery createTableQuery = JsonConvert.DeserializeObject<CreateTableQuery>(query);

View File

@ -2,7 +2,7 @@
namespace RSND.Core.Querying.Queries; namespace RSND.Core.Querying.Queries;
public class CreateTableQuery : IQuery public class CreateTableQuery : Query
{ {
public CreateTableQuery(string tableName, Row[] rows) public CreateTableQuery(string tableName, Row[] rows)
{ {

View File

@ -1,15 +1,15 @@
namespace RSND.Core.Querying.Queries; namespace RSND.Core.Querying.Queries;
public class GetQuery : IQuery public class GetQuery : Query
{ {
public GetQuery(string table, string select, string where) public GetQuery(string tableName, string select, string where)
{ {
Table = table; TableName = tableName;
Select = select; Select = select;
Where = where; Where = where;
} }
public string Table { get; set; } public string TableName { get; set; }
public string Select { get; set; } public string Select { get; set; }
public string Where { get; set; } public string Where { get; set; }
} }

View File

@ -0,0 +1,9 @@
namespace RSND.Core.Querying.Queries;
public class SetQuery : Query
{
public string TableName { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public string NewValue { get; set; }
}

View File

@ -1,6 +1,5 @@
namespace RSND.Core.Querying; namespace RSND.Core.Querying;
public interface IQuery public abstract class Query
{ {
} }

View File

@ -13,6 +13,7 @@ public static class QueryHelper
return query.Type switch return query.Type switch
{ {
"GetValue" => QueryType.GetValue, "GetValue" => QueryType.GetValue,
"SetValue" => QueryType.SetValue,
"CreateTable" => QueryType.CreateTable, "CreateTable" => QueryType.CreateTable,
_ => null _ => null
}; };

View File

@ -3,5 +3,6 @@
public enum QueryType public enum QueryType
{ {
GetValue, GetValue,
SetValue,
CreateTable CreateTable
} }