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

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
/obj
/.vscode
*.db
*.conf
*.conf
/publish

View File

@ -3,14 +3,9 @@ using Microsoft.AspNetCore.Mvc;
namespace RestySqlite
{
public class DatabaseRepository
public class DatabaseRepository(DatabaseWrapper db)
{
private DatabaseWrapper _db;
public DatabaseRepository(DatabaseWrapper db)
{
_db = db;
}
private readonly DatabaseWrapper _db = db;
public async Task<Dictionary<string, List<string>>?> ListTables()
{
@ -37,10 +32,10 @@ namespace RestySqlite
var scheme = _db.MapColumns();
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())
{
result.Add(column, _db.MapColumn(column, table));
result.Add(column, _db.MapColumn(column, table) ?? throw new NullReferenceException("Column value is null!"));
}
return result;
@ -69,25 +64,26 @@ namespace RestySqlite
return await Task.Run(() => {
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 reader = _db.GetReader(sqlCommand,
new Parameter("$table", table),
new Parameter("$id", id)
); */
Dictionary<string, string> result = new();
var result = new Dictionary<string, string>();
using (reader)
{
while (reader.Read())
{
foreach (var column in _db.MapColumns()[table])
{
if (column == null) throw new NullReferenceException("Column value is null!");
if (!result.ContainsKey(column))
{
result.Add(column, reader[column].ToString());
{;
result.Add(column, reader[column].ToString() ?? throw new NullReferenceException("Readed value is null!"));
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];
}
}
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
{
struct Command
{
public string Sql;
}
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)
{
Console.WriteLine("You should specify database name in conf.toml!");
@ -20,12 +19,22 @@ namespace RestySqlite
if (!File.Exists($"{dbName}.db"))
{
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");
RunServer(dbName, handle, port);
}
static public void RunServer(string dbName, string handle, int port)
{
var builder = WebApplication.CreateBuilder();
builder.WebHost.ConfigureKestrel(serverOptions =>
@ -62,7 +71,5 @@ namespace RestySqlite
app.Run();
}
}
}