runable
This commit is contained in:
136
.archive/category_local_datasource_hive.dart
Normal file
136
.archive/category_local_datasource_hive.dart
Normal file
@@ -0,0 +1,136 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:retail/core/database/hive_database.dart';
|
||||
import 'package:retail/features/categories/data/models/category_model.dart';
|
||||
import 'package:retail/features/categories/data/datasources/category_local_datasource.dart';
|
||||
import 'package:retail/features/categories/domain/entities/category.dart';
|
||||
|
||||
/// Hive implementation of CategoryLocalDataSource
|
||||
class CategoryLocalDataSourceHive implements CategoryLocalDataSource {
|
||||
final HiveDatabase _database;
|
||||
|
||||
CategoryLocalDataSourceHive(this._database);
|
||||
|
||||
Box<CategoryModel> get _box => _database.categoriesBox;
|
||||
|
||||
/// Convert CategoryModel to Category entity
|
||||
Category _toEntity(CategoryModel model) {
|
||||
return Category(
|
||||
id: model.id,
|
||||
name: model.name,
|
||||
description: model.description,
|
||||
iconPath: model.iconPath,
|
||||
color: model.color,
|
||||
createdAt: model.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert Category entity to CategoryModel
|
||||
CategoryModel _toModel(Category entity) {
|
||||
return CategoryModel(
|
||||
id: entity.id,
|
||||
name: entity.name,
|
||||
description: entity.description,
|
||||
iconPath: entity.iconPath,
|
||||
color: entity.color,
|
||||
productCount: 0, // Will be calculated from products
|
||||
createdAt: entity.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Category>> getAllCategories() async {
|
||||
try {
|
||||
return _box.values.map(_toEntity).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Failed to get categories: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Category?> getCategoryById(String id) async {
|
||||
try {
|
||||
final model = _box.get(id);
|
||||
return model != null ? _toEntity(model) : null;
|
||||
} catch (e) {
|
||||
throw Exception('Failed to get category by ID: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveCategories(List<Category> categories) async {
|
||||
try {
|
||||
final models = categories.map(_toModel).toList();
|
||||
final Map<String, CategoryModel> categoriesMap = {
|
||||
for (var model in models) model.id: model
|
||||
};
|
||||
await _box.putAll(categoriesMap);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to save categories: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteAllCategories() async {
|
||||
try {
|
||||
await _box.clear();
|
||||
} catch (e) {
|
||||
throw Exception('Failed to delete all categories: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional Hive-specific methods
|
||||
|
||||
/// Save a single category
|
||||
Future<void> saveCategory(Category category) async {
|
||||
try {
|
||||
final model = _toModel(category);
|
||||
await _box.put(model.id, model);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to save category: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Update an existing category
|
||||
Future<void> updateCategory(Category category) async {
|
||||
try {
|
||||
if (!_box.containsKey(category.id)) {
|
||||
throw Exception('Category not found: ${category.id}');
|
||||
}
|
||||
final model = _toModel(category);
|
||||
await _box.put(model.id, model);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to update category: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a specific category
|
||||
Future<void> deleteCategory(String id) async {
|
||||
try {
|
||||
await _box.delete(id);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to delete category: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if category exists
|
||||
Future<bool> categoryExists(String id) async {
|
||||
try {
|
||||
return _box.containsKey(id);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to check category existence: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Update product count for a category
|
||||
Future<void> updateProductCount(String categoryId, int count) async {
|
||||
try {
|
||||
final model = _box.get(categoryId);
|
||||
if (model != null) {
|
||||
final updated = model.copyWith(productCount: count);
|
||||
await _box.put(categoryId, updated);
|
||||
}
|
||||
} catch (e) {
|
||||
throw Exception('Failed to update product count: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user