Add table creation
This commit is contained in:
parent
09c9549004
commit
4a5039cb19
19 changed files with 1132 additions and 141 deletions
|
|
@ -3,6 +3,8 @@ import 'dart:convert';
|
|||
import 'package:tuuli_app/api/model/access_token_model.dart';
|
||||
import 'package:http/browser_client.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:tuuli_app/api/model/table_field_model.dart';
|
||||
import 'package:tuuli_app/api/model/tables_list_model.dart';
|
||||
|
||||
class ErrorOrData<T> {
|
||||
final T? data;
|
||||
|
|
@ -58,6 +60,8 @@ class ApiClient {
|
|||
} else {
|
||||
data = AccessTokenModel(accessToken: body['access_token']);
|
||||
}
|
||||
} else if (response.statusCode == 422) {
|
||||
error = Exception('Invalid request parameters');
|
||||
} else {
|
||||
error = Exception('HTTP ${response.statusCode}');
|
||||
}
|
||||
|
|
@ -65,6 +69,54 @@ class ApiClient {
|
|||
return ErrorOrData(data, error);
|
||||
}
|
||||
|
||||
FutureErrorOrData<TablesListModel> tablesList() async {
|
||||
TablesListModel? data;
|
||||
Exception? error;
|
||||
|
||||
final response = await get('/api/listTables');
|
||||
if (response.statusCode == 200) {
|
||||
final body = json.decode(await response.stream.bytesToString());
|
||||
if (body['error'] != null) {
|
||||
error = Exception(body['error']);
|
||||
} else if (body['tables'] == null) {
|
||||
error = Exception('Server error');
|
||||
} else {
|
||||
data = TablesListModel.fromJson(body);
|
||||
}
|
||||
} else if (response.statusCode == 422) {
|
||||
error = Exception('Invalid request parameters');
|
||||
} else {
|
||||
error = Exception('HTTP ${response.statusCode}');
|
||||
}
|
||||
|
||||
return ErrorOrData(data, error);
|
||||
}
|
||||
|
||||
FutureErrorOrData<void> createTable(
|
||||
String tableName, List<TableField> columns) async {
|
||||
Exception? error;
|
||||
|
||||
final response =
|
||||
await post('/api/createTable/${Uri.encodeComponent(tableName)}', body: {
|
||||
'columns':
|
||||
columns.map((e) => e.toColumnDefinition()).toList(growable: false),
|
||||
}, headers: {
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
if (response.statusCode == 200) {
|
||||
final body = json.decode(await response.stream.bytesToString());
|
||||
if (body['error'] != null) {
|
||||
error = Exception(body['error']);
|
||||
}
|
||||
} else if (response.statusCode == 422) {
|
||||
error = Exception('Invalid request parameters');
|
||||
} else {
|
||||
error = Exception('HTTP ${response.statusCode}');
|
||||
}
|
||||
|
||||
return ErrorOrData(null, error);
|
||||
}
|
||||
|
||||
Future<StreamedResponse> get(
|
||||
String path, {
|
||||
Map<String, String>? headers,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue