From 5d776304d7fc2e15a9be569c5dace81fd46067ef Mon Sep 17 00:00:00 2001 From: Max Nullov Date: Sun, 9 Feb 2025 22:05:23 +0300 Subject: [PATCH] Code refactoring --- .gitignore | 3 ++- DatabaseRepository.cs | 24 ++++++++++-------------- Utils.cs | 22 ++++++++++++++++++++++ WebApi.cs | 25 ++++++++++++++++--------- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 38f95b7..e9ab1a1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /obj /.vscode *.db -*.conf \ No newline at end of file +*.conf +/publish \ No newline at end of file diff --git a/DatabaseRepository.cs b/DatabaseRepository.cs index 32319f5..827f67e 100644 --- a/DatabaseRepository.cs +++ b/DatabaseRepository.cs @@ -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>?> 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> result = new(); + var result = new Dictionary>(); 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 result = new(); + var result = new Dictionary(); 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!"); } } diff --git a/Utils.cs b/Utils.cs index 0d4bbd5..b13b310 100644 --- a/Utils.cs +++ b/Utils.cs @@ -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; + } + } } } \ No newline at end of file diff --git a/WebApi.cs b/WebApi.cs index b68444d..d6087c0 100644 --- a/WebApi.cs +++ b/WebApi.cs @@ -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(); } - - } } \ No newline at end of file