feat: scan book to add to sell

This commit is contained in:
alzalia1 2025-08-15 12:51:36 +02:00
parent 3a013c829f
commit 07c7c98edb
11 changed files with 238 additions and 31 deletions

View file

@ -55,6 +55,13 @@ class BalRepository {
false;
}
Future<Bal?> ongoingBal() async {
if (_bals == null) {
await _getBalsNoCache();
}
return _bals!.where((bal) => bal.state == BalState.ongoing).firstOrNull;
}
Future<Result<Bal>> stopBal(int id) async {
final result = await _apiClient.stopBal(id);
_getBalsNoCache();

View file

@ -11,6 +11,10 @@ class BookInstanceRepository {
final ApiClient _apiClient;
Future<Result<List<BookInstance>>> getByEan(int balId, int ean) async {
return await _apiClient.getBookInstanceByEAN(balId, ean);
}
Future<Result<BookInstance>> sendBook(
Book book,
Owner owner,

View file

@ -8,6 +8,10 @@ class BookRepository {
final ApiClient _apiClient;
Future<Result<Book>> getBookByEAN(String ean) async {
return _apiClient.getBookByEAN(ean);
return await _apiClient.getBookByEAN(ean);
}
Future<Result<Book>> getBookById(int id) async {
return await _apiClient.getBookById(id);
}
}

View file

@ -33,6 +33,18 @@ class OwnerRepository {
return result;
}
Future<Result<Owner>> getOwnerById(int id) async {
if (_cachedOwners != null) {
final result1 = _cachedOwners!
.where((owner) => owner.id == id)
.firstOrNull;
if (result1 != null) {
return Result.ok(result1);
}
}
return await _apiClient.getOwnerById(id);
}
/// Adds an [Owner] to the database, and gets the resulting [Owner].
Future<Result<Owner>> addOwner(
String firstName,

View file

@ -236,6 +236,27 @@ class ApiClient {
* ===================
*/
Future<Result<Book>> getBookById(int id) async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/book/id/${id.toString()}"),
headers: headers,
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Book.fromJSON(json));
} else {
throw Exception("The book was not found");
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
Future<Result<Book>> getBookByEAN(String ean) async {
final client = Client();
try {
@ -263,6 +284,32 @@ class ApiClient {
* =============================
*/
Future<Result<List<BookInstance>>> getBookInstanceByEAN(
int balId,
int ean,
) async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse(
"https://$apiBasePath/bal/${balId.toString()}/ean/${ean.toString()}/book_instances",
),
headers: headers,
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>;
return Result.ok(json.map((el) => BookInstance.fromJSON(el)).toList());
} else {
throw "Unknown Error";
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
Future<Result<BookInstance>> sendBook(
Book book,
Owner owner,
@ -304,6 +351,27 @@ class ApiClient {
* ====================
*/
Future<Result<Owner>> getOwnerById(int id) async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/owner/${id.toString()}"),
headers: headers,
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Owner.fromJSON(json));
} else {
throw Exception("The owner was not found");
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
Future<Result<Owner>> getSectionOwner() async {
final client = Client();
try {