RestySqlite/Utils.cs

70 lines
2.2 KiB
C#
Raw Permalink Normal View History

2025-02-09 16:38:49 +03:00
using Tommy;
namespace RestySqlite
{
public class Utils
{
2025-02-09 16:47:08 +03:00
public static dynamic? ReadConfig(string key, string? category = null)
2025-02-09 16:38:49 +03:00
{
2025-02-09 16:47:08 +03:00
if (!File.Exists("conf.toml"))
{
TomlTable toml = new TomlTable
{
["title"] = "Configuration file for API",
["api"] =
{
["port"] = 5417,
["handle"] = "api",
},
["sql"] =
{
["database"] = ""
}
};
using(StreamWriter writer = File.CreateText("conf.toml"))
{
toml.WriteTo(writer);
// Remember to flush the data if needed!
writer.Flush();
}
Console.WriteLine("Config created please check it.");
return null;
}
2025-02-09 16:38:49 +03:00
using(StreamReader reader = File.OpenText("conf.toml"))
{
TomlTable table = TOML.Parse(reader);
if (category != null)
{
return table[category][key];
}
return table[key];
}
}
2025-02-09 22:05:23 +03:00
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;
}
}
2025-02-09 16:38:49 +03:00
}
}