mirror of
https://gitlab.com/nullmax17/RestySqlite.git
synced 2025-03-14 16:01:13 +03:00
Feature | web api created
This commit is contained in:
parent
09907a6cb6
commit
afa67b3d3b
121
DatabaseRepository.cs
Normal file
121
DatabaseRepository.cs
Normal file
@ -0,0 +1,121 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace RestySqlite
|
||||
{
|
||||
public class DatabaseRepository
|
||||
{
|
||||
private DatabaseWrapper _db;
|
||||
|
||||
public DatabaseRepository(DatabaseWrapper db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, List<string>>?> ListTables()
|
||||
{
|
||||
return await Task.Run(() => {
|
||||
try
|
||||
{
|
||||
return _db.MapColumns();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return null;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, List<string>>?> 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];
|
||||
|
||||
Dictionary<string, List<string>> result = new();
|
||||
foreach (var column in filtered.Single())
|
||||
{
|
||||
result.Add(column, _db.MapColumn(column, table));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async Task<List<string>?> ListColumnValues(string table, string column)
|
||||
{
|
||||
return await Task.Run(() => {
|
||||
var values = _db.MapColumn(column, table);
|
||||
return values;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, string>?> FetchValue(string table, int id)
|
||||
{
|
||||
return await Task.Run(() => {
|
||||
try
|
||||
{
|
||||
var reader = _db.GetReader($"SELECT * FROM {table} WHERE id = {id}");
|
||||
/* 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();
|
||||
using (reader)
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
foreach (var column in _db.MapColumns()[table])
|
||||
{
|
||||
if (!result.ContainsKey(column))
|
||||
{
|
||||
result.Add(column, reader[column].ToString());
|
||||
continue;
|
||||
}
|
||||
result[column] = reader[column].ToString();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<string> ProcessCommand(string sqlCommand)
|
||||
{
|
||||
return await Task.Run(() => {
|
||||
try
|
||||
{
|
||||
_db.ExecuteCommand(sqlCommand);
|
||||
return "succses";
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.Message;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
13
README.md
13
README.md
@ -2,7 +2,7 @@
|
||||
|
||||
WIP online sqlite database managment system
|
||||
|
||||
- WebAPI is not implemented yet.
|
||||
- WebAPI implemented.
|
||||
- Database abstraction is almost done
|
||||
|
||||
## How it will work?
|
||||
@ -12,13 +12,16 @@ easy database manipulation.
|
||||
|
||||
### Example endpoints
|
||||
|
||||
`GET:localhost/table_name/column_name/` - will provide json of objects inside column
|
||||
`GET:localhost/api"` - will provide json of tables inside database
|
||||
|
||||
`GET:localhost/table_name/column_name/id` - will provide json object specific to this id
|
||||
`GET:localhost/table_name/` - will provide json of columns inside table
|
||||
|
||||
`POST:localhost/` - will allow execute global query to database. For example creating new table.
|
||||
`GET:localhost/api/table_name/column_name/` - will provide json of objects inside column
|
||||
|
||||
`GET:localhost/api/table_name/id` - will provide json object specific to this id
|
||||
|
||||
`POST:localhost/api/sqlcommand` - will allow execute global query to database. For example creating new table.
|
||||
|
||||
`POST:localhost/table_name` - will allow execute query on specific table. For example add new field.
|
||||
|
||||
### Manifest of goals
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.1" />
|
||||
<PackageReference Include="Tommy" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
21
Utils.cs
Normal file
21
Utils.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using Tommy;
|
||||
|
||||
namespace RestySqlite
|
||||
{
|
||||
public class Utils
|
||||
{
|
||||
public static dynamic ReadConfig(string key, string? category = null)
|
||||
{
|
||||
using(StreamReader reader = File.OpenText("conf.toml"))
|
||||
{
|
||||
TomlTable table = TOML.Parse(reader);
|
||||
|
||||
if (category != null)
|
||||
{
|
||||
return table[category][key];
|
||||
}
|
||||
return table[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
58
WebApi.cs
58
WebApi.cs
@ -1,24 +1,62 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace RestySqlite
|
||||
{
|
||||
public class WebApi
|
||||
{
|
||||
struct Command
|
||||
{
|
||||
public string Sql;
|
||||
}
|
||||
public static void Main()
|
||||
{
|
||||
DatabaseWrapper db = new("test");
|
||||
string dbName = Utils.ReadConfig("database", "sql");
|
||||
if (dbName.Length == 0)
|
||||
{
|
||||
Console.WriteLine("You should specify database name in conf.toml!");
|
||||
return;
|
||||
}
|
||||
|
||||
//string createTableSql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)";
|
||||
/* string insertSql = "INSERT INTO users (name, email) VALUES (@name, @email)";
|
||||
string handle = Utils.ReadConfig("handle", "api");
|
||||
int port = Utils.ReadConfig("port", "api");
|
||||
|
||||
var builder = WebApplication.CreateBuilder();
|
||||
|
||||
builder.WebHost.ConfigureKestrel(serverOptions =>
|
||||
{
|
||||
serverOptions.Listen(System.Net.IPAddress.Any, port); // Listen on all network interfaces on port 5000
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
var repo = new DatabaseRepository(new DatabaseWrapper(dbName));
|
||||
|
||||
var group = app.MapGroup(handle);
|
||||
|
||||
db.ExecuteCommand(insertSql, new Parameter("@name", "Max Nullov"),
|
||||
new Parameter("@email", "[not specified]"));
|
||||
*/
|
||||
group.MapGet($"{handle}/", async() => { return await repo.ListTables(); });
|
||||
|
||||
group.MapGet("/{tableName}", async(string tableName) =>
|
||||
{ return await repo.ListColumns(tableName); });
|
||||
|
||||
group.MapGet("/{tableName}/{columnName}", async(string tableName, string columnName) =>
|
||||
{ return await repo.ListColumnValues(tableName, columnName); } );
|
||||
|
||||
db.MapColumns();
|
||||
group.MapGet("/{tableName}/{id:int}", async(string tableName, int id) =>
|
||||
{ return await repo.FetchValue(tableName, id); });
|
||||
|
||||
group.MapPost("/sqlcommand", async(HttpContext context) =>
|
||||
{
|
||||
using var reader = new StreamReader(context.Request.Body);
|
||||
string command = await reader.ReadToEndAsync();
|
||||
|
||||
return await repo.ProcessCommand(command);
|
||||
|
||||
});
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
|
||||
var usersnames = db.MapColumn("name", "users");
|
||||
|
||||
usersnames.ForEach(name => Console.WriteLine(name));
|
||||
}
|
||||
}
|
||||
}
|
8
conf.toml
Normal file
8
conf.toml
Normal file
@ -0,0 +1,8 @@
|
||||
title = "Configuration file for API"
|
||||
|
||||
[api]
|
||||
port=5417
|
||||
handle="api"
|
||||
|
||||
[sql]
|
||||
database="test"
|
Loading…
x
Reference in New Issue
Block a user