feat: honestly forgot
This commit is contained in:
parent
48bcf0b1f8
commit
da953ba651
19 changed files with 1097 additions and 244 deletions
51
lib/data/repositories/bal_repository.dart
Normal file
51
lib/data/repositories/bal_repository.dart
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import 'package:seshat/data/services/api_client.dart';
|
||||
import 'package:seshat/domain/models/bal.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
class BalRepository {
|
||||
BalRepository({required ApiClient apiClient}) : _apiClient = apiClient;
|
||||
|
||||
final ApiClient _apiClient;
|
||||
List<Bal>? _bals;
|
||||
|
||||
Future<Result<List<Bal>>> getBals() async {
|
||||
if (_bals != null) {
|
||||
return Result.ok(_bals!);
|
||||
}
|
||||
final result = await _apiClient.getBals();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_bals = result.value;
|
||||
return Result.ok(result.value);
|
||||
case Error():
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> balById(int id) async {
|
||||
if (_bals == null) {
|
||||
await getBals();
|
||||
}
|
||||
Bal? bal = _bals!.where((bal) => bal.id == id).firstOrNull;
|
||||
if (bal != null) {
|
||||
return Result.ok(bal);
|
||||
}
|
||||
final result = await _apiClient.getBalById(id);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
return Result.ok(result.value);
|
||||
case Error():
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<void>> addBal(String name) async {
|
||||
final result = await _apiClient.addBal(name);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
return result;
|
||||
case Error():
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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 ]=====
|
||||
|
|
|
|||
Reference in a new issue