sync bloc

This commit is contained in:
efrilm
2025-10-24 22:25:01 +07:00
parent 4fdd1e44f8
commit 6892895021
10 changed files with 965 additions and 0 deletions
@@ -349,4 +349,17 @@ class CategoryRepository implements ICategoryRepository {
log('🧹 Clearing category cache', name: _logName);
_localDataProvider.clearCache();
}
@override
Future<void> clearAllCategories() async {
try {
log('🗑️ Clearing all categories from repository...', name: _logName);
await _localDataProvider.clearAllCategories();
clearCache();
log('✅ All categories cleared successfully', name: _logName);
} catch (e) {
log('❌ Error clearing all categories: $e', name: _logName);
rethrow;
}
}
}
@@ -112,6 +112,27 @@ class ProductDto with _$ProductDto {
updatedAt: map['updated_at'] as String?,
variants: variants,
);
factory ProductDto.fromDomain(Product product) => ProductDto(
id: product.id,
organizationId: product.organizationId,
categoryId: product.categoryId,
sku: product.sku,
name: product.name,
description: product.description,
price: product.price,
cost: product.cost,
businessType: product.businessType,
imageUrl: product.imageUrl,
printerType: product.printerType,
metadata: product.metadata,
isActive: product.isActive,
createdAt: product.createdAt,
updatedAt: product.updatedAt,
variants: product.variants
.map((v) => ProductVariantDto.fromDomain(v))
.toList(),
);
}
@freezed
@@ -170,4 +191,16 @@ class ProductVariantDto with _$ProductVariantDto {
createdAt: map['created_at'] as String?,
updatedAt: map['updated_at'] as String?,
);
factory ProductVariantDto.fromDomain(ProductVariant variant) =>
ProductVariantDto(
id: variant.id,
productId: variant.productId,
name: variant.name,
priceModifier: variant.priceModifier,
cost: variant.cost,
metadata: variant.metadata,
createdAt: variant.createdAt,
updatedAt: variant.updatedAt,
);
}
@@ -6,6 +6,7 @@ import 'package:injectable/injectable.dart';
import '../../../domain/product/product.dart';
import '../datasources/local_data_provider.dart';
import '../datasources/remote_data_provider.dart';
import '../product_dtos.dart';
@Injectable(as: IProductRepository)
class ProductRepository implements IProductRepository {
@@ -328,4 +329,22 @@ class ProductRepository implements IProductRepository {
rethrow;
}
}
@override
Future<Either<ProductFailure, Unit>> saveProductsBatch(
List<Product> products,
) async {
try {
final productDtos = products.map(ProductDto.fromDomain).toList();
// Simpan batch ke local DB
await _localDataProvider.saveProductsBatch(productDtos);
log('💾 Saved ${products.length} products to local DB', name: _logName);
return right(unit);
} catch (e, stack) {
log('❌ Failed to save products batch: $e\n$stack', name: _logName);
return left(ProductFailure.dynamicErrorMessage(e.toString()));
}
}
}