Base user workflow (create/update)

This commit is contained in:
Andrew 2023-04-12 03:03:48 +07:00
parent eb5d3e2b70
commit 45be2c80ff
8 changed files with 528 additions and 17 deletions

View file

@ -6,6 +6,7 @@ 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';
import 'package:tuuli_app/api/model/user_model.dart';
class ErrorOrData<T> {
final T? data;
@ -292,6 +293,69 @@ class ApiClient {
return ErrorOrData(ignored, error);
}
FutureErrorOrData<bool> createUser(
TableModel table, String username, String password) async {
bool? ignored;
Exception? error;
final response = await post(
'/api/createUser',
body: {
"username": username,
"password": password,
},
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 {
ignored = true;
}
} else if (response.statusCode == 422) {
error = Exception('Invalid request parameters');
} else {
error = Exception('HTTP ${response.statusCode}');
}
return ErrorOrData(ignored, error);
}
FutureErrorOrData<bool> updateUser(
TableModel table, int userId, String password, String accessToken) async {
bool? ignored;
Exception? error;
final response = await post(
'/api/updateUser',
body: {
"user_id": userId,
"password": password,
"access_token": accessToken,
},
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 {
ignored = true;
}
} else if (response.statusCode == 422) {
error = Exception('Invalid request parameters');
} else {
error = Exception('HTTP ${response.statusCode}');
}
return ErrorOrData(ignored, error);
}
// REGION: HTTP Methods implementation
Future<StreamedResponse> get(

View file

@ -0,0 +1,27 @@
class UserModel {
final int id;
final String username;
final String? password;
final String? accessToken;
UserModel({
required this.id,
required this.username,
this.password,
this.accessToken,
});
factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
id: json["id"],
username: json["username"],
password: json["password"],
accessToken: json["access_token"],
);
Map<String, dynamic> toJson() => {
"id": id,
"username": username,
"password": password,
"access_token": accessToken,
};
}