fix: continuing error managment and documentation
This commit is contained in:
parent
59e1c2558c
commit
dad000a1b9
24 changed files with 389 additions and 182 deletions
|
|
@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
|
|||
import 'package:seshat/data/services/auth_client.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
/// Repository to manage authentification
|
||||
class AuthRepository extends ChangeNotifier {
|
||||
AuthRepository({required AuthClient authClient}) : _authClient = authClient;
|
||||
|
||||
|
|
@ -9,6 +10,7 @@ class AuthRepository extends ChangeNotifier {
|
|||
|
||||
bool? _isAuthenticated;
|
||||
|
||||
/// Checks the validity of the token if not already checked
|
||||
Future<bool> get isLoggedIn async {
|
||||
if (_isAuthenticated != null) {
|
||||
return _isAuthenticated!;
|
||||
|
|
@ -25,6 +27,7 @@ class AuthRepository extends ChangeNotifier {
|
|||
}
|
||||
}
|
||||
|
||||
/// Logs in the user
|
||||
Future<Result<void>> login(String username, String password) async {
|
||||
try {
|
||||
final result = await _authClient.login(username, password);
|
||||
|
|
@ -33,13 +36,14 @@ class AuthRepository extends ChangeNotifier {
|
|||
_isAuthenticated = true;
|
||||
return Result.ok(());
|
||||
case Error():
|
||||
return Result.error(result.error);
|
||||
return result;
|
||||
}
|
||||
} catch (e) {
|
||||
return Result.error(Exception(e));
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the API's remote version
|
||||
Future<Result<int>> getRemoteApiVersion() async {
|
||||
return await _authClient.getRemoteApiVersion();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,19 @@ import 'package:seshat/domain/models/bal_stats.dart';
|
|||
import 'package:seshat/domain/models/enums.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
/// Repository to manage [Bal]
|
||||
class BalRepository {
|
||||
BalRepository({required ApiClient apiClient}) : _apiClient = apiClient;
|
||||
|
||||
final ApiClient _apiClient;
|
||||
List<Bal>? _bals;
|
||||
Accounting? accounting;
|
||||
|
||||
/// [List<Bal>] of all the user's [Bal]
|
||||
List<Bal>? _bals;
|
||||
|
||||
/// [Accounting] of [Bal], mapped by [Bal] id
|
||||
final Map<int, Accounting?> _accountingMap = {};
|
||||
|
||||
/// Gets a list of all [Bal] from cache or remote
|
||||
Future<Result<List<Bal>>> getBals() async {
|
||||
if (_bals != null) {
|
||||
return Result.ok(_bals!);
|
||||
|
|
@ -26,6 +32,7 @@ class BalRepository {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gets a list of all [Bal] from remote only
|
||||
Future<Result<List<Bal>>> _getBalsNoCache() async {
|
||||
final result = await _apiClient.getBals();
|
||||
switch (result) {
|
||||
|
|
@ -37,15 +44,16 @@ class BalRepository {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> balById(int id) async {
|
||||
/// Gets a [Bal] by [balId], either from cache or remote
|
||||
Future<Result<Bal>> balById(int balId) async {
|
||||
if (_bals == null) {
|
||||
await getBals();
|
||||
}
|
||||
Bal? bal = _bals!.where((bal) => bal.id == id).firstOrNull;
|
||||
Bal? bal = _bals!.where((bal) => bal.id == balId).firstOrNull;
|
||||
if (bal != null) {
|
||||
return Result.ok(bal);
|
||||
}
|
||||
final result = await _apiClient.getBalById(id);
|
||||
final result = await _apiClient.getBalById(balId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
return Result.ok(result.value);
|
||||
|
|
@ -54,11 +62,13 @@ class BalRepository {
|
|||
}
|
||||
}
|
||||
|
||||
/// Return wether or not a [Bal] is currently [BalState.ongoing]
|
||||
bool isABalOngoing() {
|
||||
return _bals?.where((bal) => bal.state == BalState.ongoing).isNotEmpty ??
|
||||
false;
|
||||
}
|
||||
|
||||
/// Gets the [Bal] that is [BalState.ongoing]
|
||||
Future<Bal?> ongoingBal() async {
|
||||
if (_bals == null) {
|
||||
await _getBalsNoCache();
|
||||
|
|
@ -66,12 +76,14 @@ class BalRepository {
|
|||
return _bals!.where((bal) => bal.state == BalState.ongoing).firstOrNull;
|
||||
}
|
||||
|
||||
/// Stops a [Bal] and refresh cache
|
||||
Future<Result<Bal>> stopBal(int id) async {
|
||||
final result = await _apiClient.stopBal(id);
|
||||
_getBalsNoCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Starts a [Bal] and refresh cache
|
||||
Future<Result<Bal>> startBal(int id) async {
|
||||
if (isABalOngoing()) {
|
||||
return Result.error(
|
||||
|
|
@ -83,52 +95,62 @@ class BalRepository {
|
|||
return result;
|
||||
}
|
||||
|
||||
/// Changes a [Bal]'s [name], [startTime] or [endTime]
|
||||
Future<Result<Bal>> editBal(
|
||||
int id,
|
||||
String name,
|
||||
DateTime start,
|
||||
DateTime end,
|
||||
DateTime startTime,
|
||||
DateTime endTime,
|
||||
) async {
|
||||
final result = await _apiClient.editBal(id, name, start, end);
|
||||
final result = await _apiClient.editBal(id, name, startTime, endTime);
|
||||
await _getBalsNoCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Result<void>> addBal(String name, DateTime start, DateTime end) async {
|
||||
final result = await _apiClient.addBal(name, start, end);
|
||||
/// Creates a [Bal] from its [name], [startTime] and [endTime]
|
||||
Future<Result<void>> addBal(
|
||||
String name,
|
||||
DateTime startTime,
|
||||
DateTime endTime,
|
||||
) async {
|
||||
final result = await _apiClient.addBal(name, startTime, endTime);
|
||||
await _getBalsNoCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Result<BalStats>> getBalStats(int id) async {
|
||||
return _apiClient.getBalStats(id);
|
||||
/// Gets a [BalStats] from its [balId]
|
||||
Future<Result<BalStats>> getBalStats(int balId) async {
|
||||
return _apiClient.getBalStats(balId);
|
||||
}
|
||||
|
||||
/// Get [Accounting] of a [Bal] from remote only
|
||||
Future<Result<Accounting>> getAccountingNoCache(int balId) async {
|
||||
final result = await _apiClient.getAccounting(balId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
accounting = result.value;
|
||||
_accountingMap[balId] = result.value;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Get [Accounting] of a [Bal] from cache or remote
|
||||
Future<Result<Accounting>> getAccounting(int balId) async {
|
||||
if (accounting != null) {
|
||||
return Result.ok(accounting!);
|
||||
if (_accountingMap[balId] != null) {
|
||||
return Result.ok(_accountingMap[balId]!);
|
||||
}
|
||||
final result = await _apiClient.getAccounting(balId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
accounting = result.value;
|
||||
_accountingMap[balId] = result.value;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Manages what returning (of type [ReturnType]) does to cache and notifies remote
|
||||
Future<Result<void>> returnToId(
|
||||
int balId,
|
||||
int ownerId,
|
||||
|
|
@ -139,26 +161,32 @@ class BalRepository {
|
|||
case Ok():
|
||||
switch (type) {
|
||||
case ReturnType.books:
|
||||
final owner = accounting?.owners
|
||||
final owner = _accountingMap[balId]?.owners
|
||||
.where((el) => el.ownerId == ownerId)
|
||||
.firstOrNull;
|
||||
if (owner?.owedMoney == 0) {
|
||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
||||
_accountingMap[balId]?.owners.removeWhere(
|
||||
(el) => el.ownerId == ownerId,
|
||||
);
|
||||
}
|
||||
owner?.owed = [];
|
||||
owner?.owedInstances = [];
|
||||
break;
|
||||
case ReturnType.money:
|
||||
final owner = accounting?.owners
|
||||
final owner = _accountingMap[balId]?.owners
|
||||
.where((el) => el.ownerId == ownerId)
|
||||
.firstOrNull;
|
||||
if (owner?.owed == null || owner!.owed.isEmpty) {
|
||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
||||
_accountingMap[balId]?.owners.removeWhere(
|
||||
(el) => el.ownerId == ownerId,
|
||||
);
|
||||
}
|
||||
owner?.owedMoney = 0;
|
||||
break;
|
||||
case ReturnType.all:
|
||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
||||
_accountingMap[balId]?.owners.removeWhere(
|
||||
(el) => el.ownerId == ownerId,
|
||||
);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -6,16 +6,19 @@ import 'package:seshat/domain/models/owner.dart';
|
|||
import 'package:seshat/domain/models/search_result.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
/// Repository to manage [BookInstance]
|
||||
class BookInstanceRepository {
|
||||
BookInstanceRepository({required ApiClient apiClient})
|
||||
: _apiClient = apiClient;
|
||||
|
||||
final ApiClient _apiClient;
|
||||
|
||||
/// Gets a [List<BookInstance>] from an [ean]
|
||||
Future<Result<List<BookInstance>>> getByEan(int balId, int ean) async {
|
||||
return await _apiClient.getBookInstanceByEAN(balId, ean);
|
||||
}
|
||||
|
||||
/// Gets a [List<BookInstance>] from a [title] and [author]
|
||||
Future<Result<List<SearchResult>>> getBySearch(
|
||||
int balId,
|
||||
String title,
|
||||
|
|
@ -24,15 +27,17 @@ class BookInstanceRepository {
|
|||
return await _apiClient.getBookInstanceBySearch(balId, title, author);
|
||||
}
|
||||
|
||||
Future<Result<BookInstance>> sendBook(
|
||||
/// Sends a new [BookInstance]'s [book], [owner], [bal] and [price]
|
||||
Future<Result<BookInstance>> sendNewBookInstance(
|
||||
Book book,
|
||||
Owner owner,
|
||||
Bal bal,
|
||||
double price,
|
||||
) async {
|
||||
return await _apiClient.sendBook(book, owner, bal, price);
|
||||
return await _apiClient.sendNewBookInstance(book, owner, bal, price);
|
||||
}
|
||||
|
||||
/// Sells a [List<BookInstance>]
|
||||
Future<Result<void>> sellBooks(List<BookInstance> books) async {
|
||||
Map<String, double?> res = {};
|
||||
for (BookInstance instance in books) {
|
||||
|
|
|
|||
|
|
@ -2,16 +2,19 @@ import 'package:seshat/data/services/api_client.dart';
|
|||
import 'package:seshat/domain/models/book.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
/// Repository to manage [Book]
|
||||
class BookRepository {
|
||||
BookRepository({required ApiClient apiClient}) : _apiClient = apiClient;
|
||||
|
||||
final ApiClient _apiClient;
|
||||
|
||||
/// Gets a [Book] by its [ean]
|
||||
Future<Result<Book>> getBookByEAN(String ean) async {
|
||||
return await _apiClient.getBookByEAN(ean);
|
||||
}
|
||||
|
||||
Future<Result<Book>> getBookById(int id) async {
|
||||
return await _apiClient.getBookById(id);
|
||||
/// Gets a [Book] by its [bookId]
|
||||
Future<Result<Book>> getBookById(int bookId) async {
|
||||
return await _apiClient.getBookById(bookId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import 'package:seshat/data/services/websocket_client.dart';
|
|||
import 'package:seshat/domain/models/owner.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
/// Repository to manage [Owner]
|
||||
class OwnerRepository {
|
||||
OwnerRepository({
|
||||
required ApiClient apiClient,
|
||||
|
|
@ -14,18 +15,25 @@ class OwnerRepository {
|
|||
|
||||
final ApiClient _apiClient;
|
||||
final WebsocketClient _wsClient;
|
||||
late final StreamSubscription sub;
|
||||
List<Owner>? _cachedOwners;
|
||||
Owner? _sectionOwner;
|
||||
|
||||
Future<Result<Owner>> get sectionOwner async {
|
||||
if (_sectionOwner != null) {
|
||||
return Result.ok(_sectionOwner!);
|
||||
/// [StreamSubscription] to the [Stream<Owner>] for [_wsClient]
|
||||
late final StreamSubscription sub;
|
||||
|
||||
/// [List<Owner>] of owners, updated by [_wsClient]
|
||||
List<Owner>? _cachedOwners;
|
||||
|
||||
/// [Owner] of the current user
|
||||
Owner? _ownerOfUser;
|
||||
|
||||
/// [Owner] of the current user
|
||||
Future<Result<Owner>> get ownerOfUser async {
|
||||
if (_ownerOfUser != null) {
|
||||
return Result.ok(_ownerOfUser!);
|
||||
}
|
||||
final result = await _apiClient.getSectionOwner();
|
||||
final result = await _apiClient.getOwnerOfUser();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_sectionOwner = result.value;
|
||||
_ownerOfUser = result.value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
@ -33,16 +41,17 @@ class OwnerRepository {
|
|||
return result;
|
||||
}
|
||||
|
||||
Future<Result<Owner>> getOwnerById(int id) async {
|
||||
/// Gets an [Owner] from its [ownerId]
|
||||
Future<Result<Owner>> getOwnerById(int ownerId) async {
|
||||
if (_cachedOwners != null) {
|
||||
final result1 = _cachedOwners!
|
||||
.where((owner) => owner.id == id)
|
||||
.where((owner) => owner.id == ownerId)
|
||||
.firstOrNull;
|
||||
if (result1 != null) {
|
||||
return Result.ok(result1);
|
||||
}
|
||||
}
|
||||
return await _apiClient.getOwnerById(id);
|
||||
return await _apiClient.getOwnerById(ownerId);
|
||||
}
|
||||
|
||||
/// Adds an [Owner] to the database, and gets the resulting [Owner].
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ import 'package:seshat/domain/models/bal.dart';
|
|||
import 'package:seshat/domain/models/bal_stats.dart';
|
||||
import 'package:seshat/domain/models/book.dart';
|
||||
import 'package:seshat/domain/models/book_instance.dart';
|
||||
import 'package:seshat/domain/models/enums.dart';
|
||||
import 'package:seshat/domain/models/owner.dart';
|
||||
import 'package:seshat/domain/models/search_result.dart';
|
||||
import 'package:seshat/utils/command.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
extension StringExtension on String {
|
||||
|
|
@ -20,12 +20,17 @@ extension StringExtension on String {
|
|||
}
|
||||
}
|
||||
|
||||
/// API Client to manage all authenticated REST routes
|
||||
class ApiClient {
|
||||
ApiClient({String? host, int? port});
|
||||
ApiClient();
|
||||
|
||||
late final Command0 load;
|
||||
/// JWT for registration
|
||||
String? token;
|
||||
|
||||
/// Readiness of the API Client
|
||||
bool isReady = false;
|
||||
|
||||
/// Storage to access JWT
|
||||
FlutterSecureStorage? _secureStorage;
|
||||
Logger log = Logger(
|
||||
printer: PrettyPrinter(
|
||||
|
|
@ -36,10 +41,12 @@ class ApiClient {
|
|||
),
|
||||
);
|
||||
|
||||
/// Initializes connection to the [_secureStorage]
|
||||
Future<void> _initStore() async {
|
||||
_secureStorage ??= const FlutterSecureStorage();
|
||||
}
|
||||
|
||||
/// Generates authorization headers and option [additionalHeaders]
|
||||
Future<Map<String, String>> _getHeaders([
|
||||
Map<String, String>? additionalHeaders,
|
||||
]) async {
|
||||
|
|
@ -55,6 +62,7 @@ class ApiClient {
|
|||
* ========================
|
||||
*/
|
||||
|
||||
/// Gets data about a BAL's needed returns
|
||||
Future<Result<Accounting>> getAccounting(int balId) async {
|
||||
final url = "https://$apiBasePath/bal/$balId/accounting";
|
||||
log.i("Fetching: getAccounting ($url)");
|
||||
|
|
@ -81,6 +89,8 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Notifies the server that either Books, Money or All has been returned to the user
|
||||
/// [type] is the stringified version of [ReturnType]
|
||||
Future<Result<void>> returnToId(int balId, int ownerId, String type) async {
|
||||
final url =
|
||||
"https://$apiBasePath/bal/${balId.toString()}/accounting/return/${ownerId.toString()}";
|
||||
|
|
@ -120,8 +130,9 @@ class ApiClient {
|
|||
* =================
|
||||
*/
|
||||
|
||||
Future<Result<BalStats>> getBalStats(int id) async {
|
||||
final url = "https://$apiBasePath/bal/${id.toString()}/stats";
|
||||
/// Get stats about a BAL's performance
|
||||
Future<Result<BalStats>> getBalStats(int balId) async {
|
||||
final url = "https://$apiBasePath/bal/${balId.toString()}/stats";
|
||||
log.i("Fetching: getBalStats ($url)");
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
@ -148,8 +159,9 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> stopBal(int id) async {
|
||||
final url = "https://$apiBasePath/bal/${id.toString()}/stop";
|
||||
/// Stops a BAL, putting it's [BalState] to [BalState.ended]
|
||||
Future<Result<Bal>> stopBal(int balId) async {
|
||||
final url = "https://$apiBasePath/bal/${balId.toString()}/stop";
|
||||
log.i("Fetching: stopBal ($url)");
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
@ -176,8 +188,9 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> startBal(int id) async {
|
||||
final url = "https://$apiBasePath/bal/${id.toString()}/start";
|
||||
/// Starts a BAL, putting it's [BalState] to [BalState.ongoing]
|
||||
Future<Result<Bal>> startBal(int balId) async {
|
||||
final url = "https://$apiBasePath/bal/${balId.toString()}/start";
|
||||
log.i("Fetching: startBal ($url)");
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
@ -204,21 +217,22 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Changes the information about a [Bal], such as its [name], [startTime] or [endTime]
|
||||
Future<Result<Bal>> editBal(
|
||||
int id,
|
||||
int balId,
|
||||
String name,
|
||||
DateTime start,
|
||||
DateTime end,
|
||||
DateTime startTime,
|
||||
DateTime endTime,
|
||||
) async {
|
||||
final url = "https://$apiBasePath/bal/${id.toString()}";
|
||||
final url = "https://$apiBasePath/bal/${balId.toString()}";
|
||||
log.i("Fetching: editBal ($url)");
|
||||
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(),
|
||||
"start_timestamp": (startTime.millisecondsSinceEpoch / 1000).round(),
|
||||
"end_timestamp": (endTime.millisecondsSinceEpoch / 1000).round(),
|
||||
};
|
||||
final response = await client.patch(
|
||||
Uri.parse(url),
|
||||
|
|
@ -244,8 +258,9 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> getBalById(int id) async {
|
||||
final url = "https://$apiBasePath/bal/${id.toString()}";
|
||||
/// Gets a [Bal] from it's [balId]
|
||||
Future<Result<Bal>> getBalById(int balId) async {
|
||||
final url = "https://$apiBasePath/bal/${balId.toString()}";
|
||||
log.i("Fetching: getBalById ($url)");
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
@ -270,7 +285,12 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> addBal(String name, DateTime start, DateTime end) async {
|
||||
/// Adds a [Bal] from it's [name], [startTime] and [endTime]
|
||||
Future<Result<Bal>> addBal(
|
||||
String name,
|
||||
DateTime startTime,
|
||||
DateTime endTime,
|
||||
) async {
|
||||
final url = "https://$apiBasePath/bal";
|
||||
log.i("Fetching: addBal ($url)");
|
||||
final client = Client();
|
||||
|
|
@ -278,8 +298,8 @@ class ApiClient {
|
|||
final headers = await _getHeaders({"Content-Type": "application/json"});
|
||||
final body = {
|
||||
"name": name,
|
||||
"start_timestamp": (start.millisecondsSinceEpoch / 1000).round(),
|
||||
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
|
||||
"start_timestamp": (startTime.millisecondsSinceEpoch / 1000).round(),
|
||||
"end_timestamp": (endTime.millisecondsSinceEpoch / 1000).round(),
|
||||
};
|
||||
final response = await client.post(
|
||||
Uri.parse(url),
|
||||
|
|
@ -303,6 +323,7 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gets a [List<Bal>] of all [Bal]
|
||||
Future<Result<List<Bal>>> getBals() async {
|
||||
final url = "https://$apiBasePath/bals";
|
||||
log.i("Fetching: getBals ($url)");
|
||||
|
|
@ -328,7 +349,8 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal?>> getCurrentBal() async {
|
||||
/// Gets the ongoing BAL for the user
|
||||
Future<Result<Bal?>> getOngoingBal() async {
|
||||
final url = "https://$apiBasePath/bal/current";
|
||||
log.i("Fetching: getCurrentBal ($url)");
|
||||
final client = Client();
|
||||
|
|
@ -356,8 +378,9 @@ class ApiClient {
|
|||
* ===================
|
||||
*/
|
||||
|
||||
Future<Result<Book>> getBookById(int id) async {
|
||||
final url = "https://$apiBasePath/book/id/${id.toString()}";
|
||||
/// Gets a [Book] by its [bookId]
|
||||
Future<Result<Book>> getBookById(int bookId) async {
|
||||
final url = "https://$apiBasePath/book/id/${bookId.toString()}";
|
||||
log.i("Fetching: getBookById ($url)");
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
@ -380,6 +403,7 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gets a [Book] from its [ean]
|
||||
Future<Result<Book>> getBookByEAN(String ean) async {
|
||||
final url = "https://$apiBasePath/book/ean/$ean";
|
||||
log.i("Fetching: getBookByEan ($url)");
|
||||
|
|
@ -410,6 +434,7 @@ class ApiClient {
|
|||
* =============================
|
||||
*/
|
||||
|
||||
/// Gets a [BookInstance] from it's [title], [author] or both
|
||||
Future<Result<List<SearchResult>>> getBookInstanceBySearch(
|
||||
int balId,
|
||||
String title,
|
||||
|
|
@ -445,6 +470,7 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gets a [BookInstance] from it's [ean]
|
||||
Future<Result<List<BookInstance>>> getBookInstanceByEAN(
|
||||
int balId,
|
||||
int ean,
|
||||
|
|
@ -475,6 +501,10 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Notifies the server of the sell of multiple [BookInstance]. [books] is in the form of
|
||||
/// ```dart
|
||||
/// final books = {"aBookInstanceId": 6.0} // and its price
|
||||
/// ```
|
||||
Future<Result<void>> sellBooks(Map<String, double?> books) async {
|
||||
final url = "https://$apiBasePath/book_instance/sell/bulk";
|
||||
log.i("Fetching: sellBooks ($url)");
|
||||
|
|
@ -507,7 +537,8 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<BookInstance>> sendBook(
|
||||
/// Creates a new [BookInstance] from it's [book], it's [owner], it's [bal] and it's [price]
|
||||
Future<Result<BookInstance>> sendNewBookInstance(
|
||||
Book book,
|
||||
Owner owner,
|
||||
Bal bal,
|
||||
|
|
@ -551,8 +582,9 @@ class ApiClient {
|
|||
* ====================
|
||||
*/
|
||||
|
||||
Future<Result<Owner>> getOwnerById(int id) async {
|
||||
final url = "https://$apiBasePath/owner/${id.toString()}";
|
||||
/// Gets an [Owner] by it's [ownerId]
|
||||
Future<Result<Owner>> getOwnerById(int ownerId) async {
|
||||
final url = "https://$apiBasePath/owner/${ownerId.toString()}";
|
||||
log.i("Fetching: getOwnerById ($url)");
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
@ -577,7 +609,8 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Result<Owner>> getSectionOwner() async {
|
||||
/// Get the owner of the current user
|
||||
Future<Result<Owner>> getOwnerOfUser() async {
|
||||
final url = "https://$apiBasePath/owner/self";
|
||||
log.i("Fetching: getSectionOwner ($url)");
|
||||
final client = Client();
|
||||
|
|
@ -599,7 +632,7 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Call on `/owners` to get a list of all [Owner]s
|
||||
/// Get a [List<Owner>] of all [Owner]
|
||||
Future<Result<List<Owner>>> getOwners() async {
|
||||
final url = "https://$apiBasePath/owners";
|
||||
log.i("Fetching: getOwners ($url)");
|
||||
|
|
@ -623,7 +656,7 @@ class ApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Adds an owner to the database
|
||||
/// Adds an [Owner] from its [firstName], [lastName] and [contact]
|
||||
Future<Result<Owner>> addOwner(
|
||||
String firstName,
|
||||
String lastName,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
|
@ -7,8 +6,11 @@ import 'package:seshat/config/constants.dart';
|
|||
import 'package:seshat/utils/result.dart';
|
||||
import "package:http/http.dart";
|
||||
|
||||
/// API Client to manage all unauthenticated REST routes
|
||||
class AuthClient {
|
||||
AuthClient();
|
||||
|
||||
/// Storage to access JWT
|
||||
FlutterSecureStorage? _secureStorage;
|
||||
Logger log = Logger(
|
||||
printer: PrettyPrinter(
|
||||
|
|
@ -19,10 +21,12 @@ class AuthClient {
|
|||
),
|
||||
);
|
||||
|
||||
/// Initializes connection to the [_secureStorage]
|
||||
Future<void> _initStore() async {
|
||||
_secureStorage ??= const FlutterSecureStorage();
|
||||
}
|
||||
|
||||
/// Verifies the validity of the token stored in [_secureStorage]
|
||||
Future<Result<bool>> hasValidToken() async {
|
||||
final url = "https://$apiBasePath/token-check";
|
||||
log.i("Fetching: hasValidToken ($url)");
|
||||
|
|
@ -51,6 +55,7 @@ class AuthClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Logs a user in from its [username] and [password]
|
||||
Future<Result<String>> login(String username, String password) async {
|
||||
final url = "https://$apiBasePath/auth";
|
||||
log.i("Logging in: $url");
|
||||
|
|
@ -85,6 +90,7 @@ class AuthClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gets the API version of the server
|
||||
Future<Result<int>> getRemoteApiVersion() async {
|
||||
final url = "https://$apiBasePath/version";
|
||||
log.i("Fetching: getRemoteApiVersion ($url)");
|
||||
|
|
|
|||
|
|
@ -8,13 +8,23 @@ import 'package:seshat/config/constants.dart';
|
|||
import 'package:seshat/domain/models/owner.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
/// API Client to manages connections to WebSockets
|
||||
class WebsocketClient {
|
||||
WebSocketChannel? _channel;
|
||||
/// Storage to access JWT
|
||||
FlutterSecureStorage? _secureStorage;
|
||||
|
||||
/// Raw channel of data from WebSocket
|
||||
WebSocketChannel? _channel;
|
||||
|
||||
/// Global WebSocket Stream
|
||||
final BehaviorSubject<dynamic> _baseController = BehaviorSubject();
|
||||
|
||||
/// WebSocket Stream dedicated to [Owner] entries
|
||||
final BehaviorSubject<Owner> _ownersController = BehaviorSubject<Owner>(
|
||||
sync: true,
|
||||
);
|
||||
|
||||
/// Subscription to [_baseController]
|
||||
late final StreamSubscription sub;
|
||||
Logger log = Logger(
|
||||
printer: PrettyPrinter(
|
||||
|
|
@ -25,12 +35,15 @@ class WebsocketClient {
|
|||
),
|
||||
);
|
||||
|
||||
/// Gets a stream of [Owner]
|
||||
Stream<Owner> get owners => _ownersController.stream;
|
||||
|
||||
/// Initializes connection to the [_secureStorage]
|
||||
Future<void> _initStore() async {
|
||||
_secureStorage ??= const FlutterSecureStorage();
|
||||
}
|
||||
|
||||
/// Connects to the websocket
|
||||
Future<void> connect() async {
|
||||
final url = "wss://$apiBasePath/ws";
|
||||
log.i("Webocket: $url");
|
||||
|
|
@ -69,11 +82,13 @@ class WebsocketClient {
|
|||
}
|
||||
}
|
||||
|
||||
/// Disconnects from the websocket
|
||||
void _handleDisconnect() {
|
||||
sub.cancel();
|
||||
_channel = null;
|
||||
}
|
||||
|
||||
/// Closes all connections
|
||||
void dispose() {
|
||||
sub.cancel();
|
||||
_channel?.sink.close();
|
||||
|
|
|
|||
Reference in a new issue