using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; namespace RestySqlite { public class DatabaseRepository(DatabaseWrapper db) { private readonly DatabaseWrapper _db = db; public async Task>?> ListTables() { return await Task.Run(() => { try { return _db.MapColumns(); } catch (Exception e) { Console.WriteLine(e); return null; } }); } public async Task>?> ListColumns(string table) { return await Task.Run(() => { try { var scheme = _db.MapColumns(); var filtered = from tbl in scheme.Keys where tbl == table select scheme[tbl]; var result = new Dictionary>(); foreach (var column in filtered.Single()) { result.Add(column, _db.MapColumn(column, table) ?? throw new NullReferenceException("Column value is null!")); } return result; } catch (Exception e) { Console.WriteLine(e); return null; } }); } public async Task?> ListColumnValues(string table, string column) { return await Task.Run(() => { var values = _db.MapColumn(column, table); return values; }); } public async Task?> FetchValue(string table, int id) { return await Task.Run(() => { try { 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) ); */ 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() ?? throw new NullReferenceException("Readed value is null!")); continue; } result[column] = reader[column].ToString() ?? throw new NullReferenceException("Readed value is null!"); } } } return result; } catch (Exception e) { Console.WriteLine(e); return null; } }); } public async Task ProcessCommand(string sqlCommand) { return await Task.Run(() => { try { _db.ExecuteCommand(sqlCommand); return "succses"; } catch (Exception e) { return e.Message; } }); } } }