feat: added search
This commit is contained in:
parent
b54b825dad
commit
166ee5b389
7 changed files with 193 additions and 60 deletions
|
|
@ -9,6 +9,7 @@ import 'package:seshat/domain/models/book.dart';
|
|||
import 'package:seshat/domain/models/book_instance.dart';
|
||||
import 'package:seshat/domain/models/book_stack.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';
|
||||
|
||||
|
|
@ -93,7 +94,54 @@ class SellViewModel extends ChangeNotifier {
|
|||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> searchBook(String title, String author) async {
|
||||
Bal? bal = await _balRepository.ongoingBal();
|
||||
isScanLoaded = false;
|
||||
_scannedBooks.clear();
|
||||
|
||||
final result = await _bookInstanceRepository.getBySearch(
|
||||
bal!.id,
|
||||
title,
|
||||
author,
|
||||
);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
for (SearchResult searchResult in result.value) {
|
||||
if (searchResult.instance.available == false) {
|
||||
continue;
|
||||
}
|
||||
if (_soldBooks
|
||||
.where((book) => book.instance.id == searchResult.instance.id)
|
||||
.isNotEmpty) {
|
||||
continue;
|
||||
}
|
||||
Owner owner;
|
||||
final result2 = await _ownerRepository.getOwnerById(
|
||||
searchResult.instance.ownerId,
|
||||
);
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
owner = result2.value;
|
||||
break;
|
||||
case Error():
|
||||
continue;
|
||||
}
|
||||
_scannedBooks.add(
|
||||
BookStack(searchResult.book, searchResult.instance, owner),
|
||||
);
|
||||
}
|
||||
break;
|
||||
case Error():
|
||||
break;
|
||||
}
|
||||
|
||||
isScanLoaded = true;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
Future<void> scanBook(BarcodeCapture barcode) async {
|
||||
isScanLoaded = false;
|
||||
int ean = int.parse(barcode.barcodes.first.rawValue!);
|
||||
Bal? bal = await _balRepository.ongoingBal();
|
||||
_scannedBooks.clear();
|
||||
|
|
@ -101,6 +149,17 @@ class SellViewModel extends ChangeNotifier {
|
|||
final result = await _bookInstanceRepository.getByEan(bal!.id, ean);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
Book book;
|
||||
final result2 = await _bookRepository.getBookById(
|
||||
result.value.first.bookId,
|
||||
);
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
book = result2.value;
|
||||
break;
|
||||
case Error():
|
||||
return;
|
||||
}
|
||||
for (BookInstance instance in result.value) {
|
||||
if (instance.available == false) {
|
||||
continue;
|
||||
|
|
@ -110,15 +169,6 @@ class SellViewModel extends ChangeNotifier {
|
|||
.isNotEmpty) {
|
||||
continue;
|
||||
}
|
||||
Book book;
|
||||
final result2 = await _bookRepository.getBookById(instance.bookId);
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
book = result2.value;
|
||||
break;
|
||||
case Error():
|
||||
continue;
|
||||
}
|
||||
Owner owner;
|
||||
final result3 = await _ownerRepository.getOwnerById(instance.ownerId);
|
||||
switch (result3) {
|
||||
|
|
|
|||
Reference in a new issue