feat: add a book by scanning

This commit is contained in:
Alzalia 2025-08-09 01:42:22 +02:00
parent 72fd0b66a9
commit 981dce5bfe
14 changed files with 264 additions and 59 deletions

View file

@ -1,8 +1,12 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:http/http.dart';
import 'package:seshat/config/constants.dart';
import 'package:seshat/domain/models/bal.dart';
import 'package:seshat/domain/models/book.dart';
import 'package:seshat/domain/models/book_instance.dart';
import 'package:seshat/domain/models/owner.dart';
import 'package:seshat/utils/command.dart';
import 'package:seshat/utils/result.dart';
@ -23,18 +27,101 @@ class ApiClient {
);
}
Future<Map<String, String>> _getHeaders([
Map<String, String>? additionalHeaders,
]) async {
await _initStore();
final token = await _secureStorage!.read(key: "token");
final headers = {"Authorization": "Bearer $token", ...?additionalHeaders};
return headers;
}
/*
* ===================
* =====[ BOOKS ]=====
* ===================
*/
Future<Result<Book>> getBookByEAN(String ean) async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/book/ean/$ean"),
headers: headers,
);
debugPrint("\n\n\n\nGOT : ${response.statusCode}\n\n\n\n");
if (response.statusCode == 200) {
debugPrint("\n\n\n\nWITH : ${response.body}\n\n\n\n");
final json = jsonDecode(response.body);
return Result.ok(Book.fromJSON(json));
} else {
debugPrintStack();
return Result.error(Exception("The book was not found"));
}
} catch (e, stackTrace) {
debugPrintStack(stackTrace: stackTrace);
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
/*
* =============================
* =====[ BOOKS INSTANCES ]=====
* =============================
*/
Future<Result<BookInstance>> sendBook(
Book book,
Owner owner,
Bal bal,
double price,
) async {
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = jsonEncode({
"bal_id": bal.id,
"book_id": book.id,
"owner_id": owner.id,
"price": price,
});
debugPrint("\n\n\n\nSENDING : ${body}\n\n\n\n");
final response = await client.post(
Uri.parse("https://$apiBasePath/book_instance"),
headers: headers,
body: body,
);
if (response.statusCode == 201) {
final json = jsonDecode(response.body);
debugPrint("\n\n\n\nRECEIVED : ${json}\n\n\n\n");
return Result.ok(BookInstance.fromJSON(json));
} else if (response.statusCode == 403) {
return Result.error(Exception("You don't own that book instance"));
} else {
return Result.error(Exception("Something wrong happened"));
}
} catch (e, stack) {
debugPrintStack(stackTrace: stack);
return Result.error(Exception(e));
} finally {
client.close();
}
}
/*
* ====================
* =====[ OWNERS ]=====
* ====================
*/
/// Call on `/owners` to get a list of all [Owner]s
Future<Result<List<Owner>>> getOwners() async {
final client = Client();
try {
await _initStore();
final token = await _secureStorage!.read(key: "token");
final headers = {"Authorization": "Bearer $token"};
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/owners"),
headers: headers,
@ -54,6 +141,7 @@ class ApiClient {
}
}
/// Adds an owner to the database
Future<Result<Owner>> addOwner(
String firstName,
String lastName,
@ -61,12 +149,7 @@ class ApiClient {
) async {
final client = Client();
try {
await _initStore();
final token = await _secureStorage!.read(key: "token");
final headers = {
"Authorization": "Bearer $token",
"Content-Type": "application/json",
};
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = {
"first_name": firstName,
"last_name": lastName,

View file

@ -20,7 +20,6 @@ class AuthClient {
try {
await _initStore();
bool hasToken = await _secureStorage!.containsKey(key: "token");
debugPrint("\n\n\n${hasToken == true} => HAS_TOKEN\n\n\n");
if (hasToken) {
var token = await _secureStorage!.read(key: "token");
var url = Uri.parse("https://$apiBasePath/token-check");
@ -29,9 +28,6 @@ class AuthClient {
headers: {"Content-Type": "application/json"},
body: jsonEncode({"token": token}),
);
debugPrint(
"\n\n\n${response.body is String} => ${response.body}\n\n\n",
);
if (response.body == "true") {
return Result.ok(true);
@ -39,7 +35,6 @@ class AuthClient {
}
return Result.ok(false);
} catch (e) {
debugPrint(e.toString());
return Result.error(Exception(e));
}
}
@ -67,8 +62,6 @@ class AuthClient {
return Result.error(Exception("Token creation error"));
}
} catch (e, stackTrace) {
debugPrint(e.toString());
debugPrintStack(stackTrace: stackTrace);
return Result.error(Exception(e));
} finally {
client.close();