forked from eatvio/eatvio_app
109 lines
3.5 KiB
Dart
109 lines
3.5 KiB
Dart
import 'package:dio/dio.dart';
|
||
import 'package:eatvio_app/features/barcode/models/barcode_product_model.dart';
|
||
import 'package:eatvio_app/features/barcode/services/barcode_service.dart';
|
||
|
||
class BarcodeRepository {
|
||
final BarcodeService barcodeService;
|
||
final Dio dio;
|
||
|
||
BarcodeRepository({
|
||
required this.barcodeService,
|
||
required this.dio,
|
||
});
|
||
|
||
/// Получить информацию о продукте по штрихкоду
|
||
/// Возвращает либо данные продукта, либо статус задачи обработки
|
||
Future<BarcodeResult> getProductByBarcode(String barcode) async {
|
||
try {
|
||
final response = await dio.get('/api/gsone/barcode/$barcode');
|
||
|
||
if (response.statusCode == 200) {
|
||
// Данные найдены в БД
|
||
final product = BarcodeProductModel.fromJson(response.data as Map<String, dynamic>);
|
||
return BarcodeResult.success(product);
|
||
} else if (response.statusCode == 202) {
|
||
// Запущена задача обработки
|
||
final taskStatus = BarcodeTaskStatusModel.fromJson(response.data as Map<String, dynamic>);
|
||
return BarcodeResult.processing(taskStatus.taskId ?? '');
|
||
}
|
||
|
||
throw Exception('Unexpected status code: ${response.statusCode}');
|
||
} on DioException catch (e) {
|
||
if (e.response?.statusCode == 202) {
|
||
// Если получен статус 202, значит запущена задача обработки
|
||
final taskStatus = BarcodeTaskStatusModel.fromJson(e.response!.data as Map<String, dynamic>);
|
||
return BarcodeResult.processing(taskStatus.taskId ?? '');
|
||
}
|
||
rethrow;
|
||
} catch (e) {
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
/// Получить статус задачи обработки
|
||
Future<BarcodeTaskStatus> getTaskStatus(String taskId) async {
|
||
try {
|
||
final statusModel = await barcodeService.getTaskStatus(taskId: taskId);
|
||
|
||
if (statusModel.status == 'completed' && statusModel.data != null) {
|
||
return BarcodeTaskStatus.completed(statusModel.data!);
|
||
} else if (statusModel.status == 'failed') {
|
||
return BarcodeTaskStatus.failed();
|
||
} else {
|
||
return BarcodeTaskStatus.processing();
|
||
}
|
||
} catch (e) {
|
||
rethrow;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Результат запроса информации о продукте по штрихкоду
|
||
class BarcodeResult {
|
||
final BarcodeResultType type;
|
||
final BarcodeProductModel? product;
|
||
final String? taskId;
|
||
|
||
BarcodeResult._({required this.type, this.product, this.taskId});
|
||
|
||
factory BarcodeResult.success(BarcodeProductModel product) {
|
||
return BarcodeResult._(type: BarcodeResultType.success, product: product);
|
||
}
|
||
|
||
factory BarcodeResult.processing(String taskId) {
|
||
return BarcodeResult._(type: BarcodeResultType.processing, taskId: taskId);
|
||
}
|
||
}
|
||
|
||
enum BarcodeResultType {
|
||
success,
|
||
processing,
|
||
}
|
||
|
||
/// Статус задачи обработки
|
||
class BarcodeTaskStatus {
|
||
final BarcodeTaskStatusType type;
|
||
final BarcodeProductModel? product;
|
||
|
||
BarcodeTaskStatus._({required this.type, this.product});
|
||
|
||
factory BarcodeTaskStatus.completed(BarcodeProductModel product) {
|
||
return BarcodeTaskStatus._(type: BarcodeTaskStatusType.completed, product: product);
|
||
}
|
||
|
||
factory BarcodeTaskStatus.processing() {
|
||
return BarcodeTaskStatus._(type: BarcodeTaskStatusType.processing);
|
||
}
|
||
|
||
factory BarcodeTaskStatus.failed() {
|
||
return BarcodeTaskStatus._(type: BarcodeTaskStatusType.failed);
|
||
}
|
||
}
|
||
|
||
enum BarcodeTaskStatusType {
|
||
completed,
|
||
processing,
|
||
failed,
|
||
}
|
||
|