Code refactoring

This commit is contained in:
Max Chaev 2025-02-09 22:05:23 +03:00
parent 051143abd0
commit 5d776304d7
4 changed files with 50 additions and 24 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
/.vscode /.vscode
*.db *.db
*.conf *.conf
/publish

View File

@ -3,14 +3,9 @@ using Microsoft.AspNetCore.Mvc;
namespace RestySqlite namespace RestySqlite
{ {
public class DatabaseRepository public class DatabaseRepository(DatabaseWrapper db)
{ {
private DatabaseWrapper _db; private readonly DatabaseWrapper _db = db;
public DatabaseRepository(DatabaseWrapper db)
{
_db = db;
}
public async Task<Dictionary<string, List<string>>?> ListTables() public async Task<Dictionary<string, List<string>>?> ListTables()
{ {
@ -37,10 +32,10 @@ namespace RestySqlite
var scheme = _db.MapColumns(); var scheme = _db.MapColumns();
var filtered = from tbl in scheme.Keys where tbl == table select scheme[tbl]; var filtered = from tbl in scheme.Keys where tbl == table select scheme[tbl];
Dictionary<string, List<string>> result = new(); var result = new Dictionary<string, List<string>>();
foreach (var column in filtered.Single()) foreach (var column in filtered.Single())
{ {
result.Add(column, _db.MapColumn(column, table)); result.Add(column, _db.MapColumn(column, table) ?? throw new NullReferenceException("Column value is null!"));
} }
return result; return result;
@ -69,25 +64,26 @@ namespace RestySqlite
return await Task.Run(() => { return await Task.Run(() => {
try try
{ {
var reader = _db.GetReader($"SELECT * FROM {table} WHERE id = {id}"); var reader = _db.GetReader($"SELECT * FROM {table} WHERE id = {id}") ?? throw new NullReferenceException("Reader is null!");
/* var sqlCommand = "SELECT * FROM $table WHERE id = $id"; /* var sqlCommand = "SELECT * FROM $table WHERE id = $id";
var reader = _db.GetReader(sqlCommand, var reader = _db.GetReader(sqlCommand,
new Parameter("$table", table), new Parameter("$table", table),
new Parameter("$id", id) new Parameter("$id", id)
); */ ); */
Dictionary<string, string> result = new(); var result = new Dictionary<string, string>();
using (reader) using (reader)
{ {
while (reader.Read()) while (reader.Read())
{ {
foreach (var column in _db.MapColumns()[table]) foreach (var column in _db.MapColumns()[table])
{ {
if (column == null) throw new NullReferenceException("Column value is null!");
if (!result.ContainsKey(column)) if (!result.ContainsKey(column))
{ {;
result.Add(column, reader[column].ToString()); result.Add(column, reader[column].ToString() ?? throw new NullReferenceException("Readed value is null!"));
continue; continue;
} }
result[column] = reader[column].ToString(); result[column] = reader[column].ToString() ?? throw new NullReferenceException("Readed value is null!");
} }
} }

View File

@ -44,5 +44,27 @@ namespace RestySqlite
return table[key]; return table[key];
} }
} }
public static void CreateDatabase(string databaseName)
{
Console.WriteLine("Would you like to create database? Y/N");
var input = Console.ReadKey().Key.ToString() ?? throw new NullReferenceException("Can't read key from user!") ;
if (input.ToUpper() != "Y") return;
File.Create($"{databaseName}.db");
Console.WriteLine("Now please, create first table inside database.");
Console.WriteLine("Input a valid SQL command: ");
var command = Console.ReadLine() ?? throw new NullReferenceException("Can't read command from user!");
try
{
new DatabaseWrapper(databaseName).ExecuteCommand(command);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
}
} }
} }

View File

@ -4,13 +4,12 @@ namespace RestySqlite
{ {
public class WebApi public class WebApi
{ {
struct Command
{
public string Sql;
}
public static void Main() public static void Main()
{ {
string dbName = Utils.ReadConfig("database", "sql"); string? dbName = Utils.ReadConfig("database", "sql") ?? throw new NullReferenceException("Database name is null!");
if (dbName.Length == 0) if (dbName.Length == 0)
{ {
Console.WriteLine("You should specify database name in conf.toml!"); Console.WriteLine("You should specify database name in conf.toml!");
@ -20,12 +19,22 @@ namespace RestySqlite
if (!File.Exists($"{dbName}.db")) if (!File.Exists($"{dbName}.db"))
{ {
Console.WriteLine($"Database {dbName}.db not found in folder!"); Console.WriteLine($"Database {dbName}.db not found in folder!");
return; Utils.CreateDatabase(dbName);
} }
string handle = Utils.ReadConfig("handle", "api"); string? handle = Utils.ReadConfig("handle", "api") ?? throw new NullReferenceException("API handle is null!");
int port = Utils.ReadConfig("port", "api"); int port = Utils.ReadConfig("port", "api");
RunServer(dbName, handle, port);
}
static public void RunServer(string dbName, string handle, int port)
{
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.WebHost.ConfigureKestrel(serverOptions => builder.WebHost.ConfigureKestrel(serverOptions =>
@ -62,7 +71,5 @@ namespace RestySqlite
app.Run(); app.Run();
} }
} }
} }