|
@ -0,0 +1,93 @@ |
|
|
|
|
|
using Fleck; |
|
|
|
|
|
using Newtonsoft.Json; |
|
|
|
|
|
using RSND.Core.DbInternals; |
|
|
|
|
|
using RSND.Core.Querying; |
|
|
|
|
|
using RSND.Core.Querying.Queries; |
|
|
|
|
|
|
|
|
|
|
|
namespace RSND.Core; |
|
|
|
|
|
|
|
|
|
|
|
public class DbClient |
|
|
|
|
|
{ |
|
|
|
|
|
private readonly IWebSocketConnection _socket; |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
{ |
|
|
|
|
|
_socket.Send(JsonConvert.SerializeObject(new Response |
|
|
|
|
|
{ |
|
|
|
|
|
Success = false, |
|
|
|
|
|
Message = "Invalid query" |
|
|
|
|
|
})); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Handle() |
|
|
|
|
|
{ |
|
|
|
|
|
_socket.OnMessage = message => |
|
|
|
|
|
{ |
|
|
|
|
|
QueryType? queryType = QueryHelper.GetQueryType(message); |
|
|
|
|
|
|
|
|
|
|
|
RunQuery(message, queryType); |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void RunQuery(string? query, QueryType? queryType) |
|
|
|
|
|
{ |
|
|
|
|
|
if (query == null || queryType == null) |
|
|
|
|
|
InvalidQuery(); |
|
|
|
|
|
|
|
|
|
|
|
// check if
|
|
|
|
|
|
switch (queryType) |
|
|
|
|
|
{ |
|
|
|
|
|
case QueryType.GetValue: |
|
|
|
|
|
{ |
|
|
|
|
|
GetQuery getQuery = JsonConvert.DeserializeObject<GetQuery>(query); |
|
|
|
|
|
var queryResult = RsndMain.Db.GetValue(getQuery); |
|
|
|
|
|
_socket.Send(queryResult); |
|
|
|
|
|
Console.WriteLine($"Yooo: {queryResult}"); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
case QueryType.CreateTable: |
|
|
|
|
|
{ |
|
|
|
|
|
/*RsndMain.Db.CreateTable(new Table |
|
|
|
|
|
{ |
|
|
|
|
|
Name = query.Table |
|
|
|
|
|
});*/ |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
case null: |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
throw new ArgumentOutOfRangeException(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |