feat: honestly forgot

This commit is contained in:
Alzalia 2025-08-11 22:41:15 +02:00
parent 48bcf0b1f8
commit da953ba651
19 changed files with 1097 additions and 244 deletions

View file

@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:http/http.dart';
import 'package:seshat/config/constants.dart';
@ -35,6 +36,106 @@ class ApiClient {
return headers;
}
/*
* =================
* =====[ BAL ]=====
* =================
*/
Future<Result<Bal>> getBalById(int id) async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/bal/${id.toString()}"),
headers: headers,
);
if (response.statusCode == 200) {
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"));
} else {
return Result.error(
Exception("No bal wirth this id exists the database"),
);
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal>> addBal(String name) async {
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = {"name": name};
final response = await client.post(
Uri.parse("https://$apiBasePath/bal"),
headers: headers,
body: jsonEncode(body),
);
if (response.statusCode == 201) {
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
} else {
return Result.error(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();
}
}
Future<Result<List<Bal>>> getBals() async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/bals"),
headers: headers,
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>;
debugPrint("\n\n\n\nRECEIVED $json\n\n\n\n");
return Result.ok(json.map((element) => Bal.fromJSON(element)).toList());
} else {
return Result.error(Exception("Something wrong happened"));
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal?>> getCurrentBal() async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/bal/current"),
headers: headers,
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
} else if (response.statusCode == 404) {
return Result.ok(null);
} else {
return Result.error(Exception("Something went wrong"));
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
/*
* ===================
* =====[ BOOKS ]=====