feat: start of pending screen
This commit is contained in:
parent
019a21f00e
commit
ee9c4c3801
12 changed files with 425 additions and 67 deletions
|
|
@ -42,6 +42,38 @@ class ApiClient {
|
|||
* =================
|
||||
*/
|
||||
|
||||
Future<Result<Bal>> editBal(
|
||||
int id,
|
||||
String name,
|
||||
DateTime start,
|
||||
DateTime end,
|
||||
) async {
|
||||
final client = Client();
|
||||
try {
|
||||
final headers = await _getHeaders({"Content-Type": "application/json"});
|
||||
final body = {
|
||||
"name": name,
|
||||
"start_timestamp": (start.millisecondsSinceEpoch / 1000).round(),
|
||||
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
|
||||
};
|
||||
final response = await client.patch(
|
||||
Uri.parse("https://$apiBasePath/bal/${id.toString()}"),
|
||||
headers: headers,
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final json = jsonDecode(response.body);
|
||||
return Result.ok(Bal.fromJSON(json));
|
||||
} else {
|
||||
throw Exception("Something went wrong");
|
||||
}
|
||||
} catch (e) {
|
||||
return Result.error(Exception(e));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> getBalById(int id) async {
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
@ -54,7 +86,7 @@ class ApiClient {
|
|||
final json = jsonDecode(response.body);
|
||||
return Result.ok(Bal.fromJSON(json));
|
||||
} else if (response.statusCode == 403) {
|
||||
return Result.error(Exception("You don't own the specified bal"));
|
||||
throw Exception("You don't own the specified bal");
|
||||
} else {
|
||||
return Result.error(
|
||||
Exception("No bal wirth this id exists the database"),
|
||||
|
|
@ -67,11 +99,15 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> addBal(String name) async {
|
||||
Future<Result<Bal>> addBal(String name, DateTime start, DateTime end) async {
|
||||
final client = Client();
|
||||
try {
|
||||
final headers = await _getHeaders({"Content-Type": "application/json"});
|
||||
final body = {"name": name};
|
||||
final body = {
|
||||
"name": name,
|
||||
"start_timestamp": (start.millisecondsSinceEpoch / 1000).round(),
|
||||
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
|
||||
};
|
||||
final response = await client.post(
|
||||
Uri.parse("https://$apiBasePath/bal"),
|
||||
headers: headers,
|
||||
|
|
@ -81,10 +117,9 @@ class ApiClient {
|
|||
final json = jsonDecode(response.body);
|
||||
return Result.ok(Bal.fromJSON(json));
|
||||
} else {
|
||||
return Result.error(Exception("Something went wrong"));
|
||||
throw Exception("Something went wrong");
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("\n\n\n\n${e.toString()}\n\n\n\n");
|
||||
return Result.error(Exception(e));
|
||||
} finally {
|
||||
client.close();
|
||||
|
|
@ -101,12 +136,17 @@ class ApiClient {
|
|||
);
|
||||
if (response.statusCode == 200) {
|
||||
final json = jsonDecode(response.body) as List<dynamic>;
|
||||
debugPrint("\n\n\n\nRECEIVED $json\n\n\n\n");
|
||||
debugPrint("\n\n\n\nRECEIVED : $json\n\n\n\n");
|
||||
debugPrint(
|
||||
"\n\n\n\nFORMATTED : ${json.map((element) => Bal.fromJSON(element)).toList()}\n\n\n\n",
|
||||
);
|
||||
|
||||
return Result.ok(json.map((element) => Bal.fromJSON(element)).toList());
|
||||
} else {
|
||||
return Result.error(Exception("Something wrong happened"));
|
||||
throw Exception("Something wrong happened");
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("ERROR: ${e.toString()}");
|
||||
return Result.error(Exception(e));
|
||||
} finally {
|
||||
client.close();
|
||||
|
|
@ -127,7 +167,7 @@ class ApiClient {
|
|||
} else if (response.statusCode == 404) {
|
||||
return Result.ok(null);
|
||||
} else {
|
||||
return Result.error(Exception("Something went wrong"));
|
||||
throw Exception("Something went wrong");
|
||||
}
|
||||
} catch (e) {
|
||||
return Result.error(Exception(e));
|
||||
|
|
@ -154,7 +194,7 @@ class ApiClient {
|
|||
final json = jsonDecode(response.body);
|
||||
return Result.ok(Book.fromJSON(json));
|
||||
} else {
|
||||
return Result.error(Exception("The book was not found"));
|
||||
throw Exception("The book was not found");
|
||||
}
|
||||
} catch (e) {
|
||||
return Result.error(Exception("API $e"));
|
||||
|
|
@ -193,9 +233,9 @@ class ApiClient {
|
|||
final json = jsonDecode(response.body);
|
||||
return Result.ok(BookInstance.fromJSON(json));
|
||||
} else if (response.statusCode == 403) {
|
||||
return Result.error(Exception("You don't own that book instance"));
|
||||
throw Exception("You don't own that book instance");
|
||||
} else {
|
||||
return Result.error(Exception("Something wrong happened"));
|
||||
throw Exception("Something wrong happened");
|
||||
}
|
||||
} catch (e) {
|
||||
return Result.error(Exception(e));
|
||||
|
|
@ -225,7 +265,7 @@ class ApiClient {
|
|||
json.map((element) => Owner.fromJSON(element)).toList(),
|
||||
);
|
||||
} else {
|
||||
return Result.error(Exception("Invalid request"));
|
||||
throw Exception("Invalid request");
|
||||
}
|
||||
} on Exception catch (error) {
|
||||
return Result.error(error);
|
||||
|
|
@ -257,7 +297,7 @@ class ApiClient {
|
|||
final json = jsonDecode(response.body);
|
||||
return Result.ok(Owner.fromJSON(json));
|
||||
} else {
|
||||
return Result.error(Exception("Invalid request"));
|
||||
throw Exception("Invalid request");
|
||||
}
|
||||
} on Exception catch (error) {
|
||||
return Result.error(error);
|
||||
|
|
|
|||
Reference in a new issue