list orders

This commit is contained in:
Phuoc Nguyen
2025-11-24 16:25:54 +07:00
parent 354df3ad01
commit 75d6507719
24 changed files with 1004 additions and 982 deletions

View File

@@ -4,22 +4,67 @@
library;
import 'package:worker/features/orders/data/datasources/order_remote_datasource.dart';
import 'package:worker/features/orders/data/datasources/order_status_local_datasource.dart';
import 'package:worker/features/orders/data/models/order_model.dart';
import 'package:worker/features/orders/domain/entities/order.dart';
import 'package:worker/features/orders/domain/entities/order_status.dart';
import 'package:worker/features/orders/domain/entities/payment_term.dart';
import 'package:worker/features/orders/domain/repositories/order_repository.dart';
/// Order Repository Implementation
class OrderRepositoryImpl implements OrderRepository {
const OrderRepositoryImpl(this._remoteDataSource);
const OrderRepositoryImpl(
this._remoteDataSource,
this._statusLocalDataSource,
);
final OrderRemoteDataSource _remoteDataSource;
final OrderStatusLocalDataSource _statusLocalDataSource;
@override
Future<List<Order>> getOrdersList({
int limitStart = 0,
int limitPageLength = 0,
}) async {
try {
final ordersData = await _remoteDataSource.getOrdersList(
limitStart: limitStart,
limitPageLength: limitPageLength,
);
// Convert JSON → Model → Entity
return ordersData
.map((json) => OrderModel.fromJson(json).toEntity())
.toList();
} catch (e) {
throw Exception('Failed to get orders list: $e');
}
}
@override
Future<List<OrderStatus>> getOrderStatusList() async {
try {
// Try to get from cache first
if (_statusLocalDataSource.hasCachedData()) {
final cachedModels = _statusLocalDataSource.getCachedStatusList();
if (cachedModels.isNotEmpty) {
return cachedModels.map((model) => model.toEntity()).toList();
}
}
// Fetch from API
final models = await _remoteDataSource.getOrderStatusList();
// Cache the results
await _statusLocalDataSource.cacheStatusList(models);
// Return entities
return models.map((model) => model.toEntity()).toList();
} catch (e) {
// If API fails, try to return cached data
final cachedModels = _statusLocalDataSource.getCachedStatusList();
if (cachedModels.isNotEmpty) {
return cachedModels.map((model) => model.toEntity()).toList();
}
throw Exception('Failed to get order status list: $e');
}
}