84 lines
1.9 KiB
Dart
84 lines
1.9 KiB
Dart
import 'package:hive_ce/hive.dart';
|
|
import '../../domain/entities/cart_item.dart';
|
|
import '../../../../core/constants/storage_constants.dart';
|
|
|
|
part 'cart_item_model.g.dart';
|
|
|
|
@HiveType(typeId: StorageConstants.cartItemTypeId)
|
|
class CartItemModel extends HiveObject {
|
|
@HiveField(0)
|
|
final String productId;
|
|
|
|
@HiveField(1)
|
|
final String productName;
|
|
|
|
@HiveField(2)
|
|
final double price;
|
|
|
|
@HiveField(3)
|
|
final int quantity;
|
|
|
|
@HiveField(4)
|
|
final String? imageUrl;
|
|
|
|
@HiveField(5)
|
|
final DateTime addedAt;
|
|
|
|
CartItemModel({
|
|
required this.productId,
|
|
required this.productName,
|
|
required this.price,
|
|
required this.quantity,
|
|
this.imageUrl,
|
|
required this.addedAt,
|
|
});
|
|
|
|
/// Convert to domain entity
|
|
CartItem toEntity() {
|
|
return CartItem(
|
|
productId: productId,
|
|
productName: productName,
|
|
price: price,
|
|
quantity: quantity,
|
|
imageUrl: imageUrl,
|
|
addedAt: addedAt,
|
|
);
|
|
}
|
|
|
|
/// Create from domain entity
|
|
factory CartItemModel.fromEntity(CartItem item) {
|
|
return CartItemModel(
|
|
productId: item.productId,
|
|
productName: item.productName,
|
|
price: item.price,
|
|
quantity: item.quantity,
|
|
imageUrl: item.imageUrl,
|
|
addedAt: item.addedAt,
|
|
);
|
|
}
|
|
|
|
/// Convert to JSON
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'productId': productId,
|
|
'productName': productName,
|
|
'price': price,
|
|
'quantity': quantity,
|
|
'imageUrl': imageUrl,
|
|
'addedAt': addedAt.toIso8601String(),
|
|
};
|
|
}
|
|
|
|
/// Create from JSON
|
|
factory CartItemModel.fromJson(Map<String, dynamic> json) {
|
|
return CartItemModel(
|
|
productId: json['productId'] as String,
|
|
productName: json['productName'] as String,
|
|
price: (json['price'] as num).toDouble(),
|
|
quantity: json['quantity'] as int,
|
|
imageUrl: json['imageUrl'] as String?,
|
|
addedAt: DateTime.parse(json['addedAt'] as String),
|
|
);
|
|
}
|
|
}
|