118 lines
2.9 KiB
Dart
118 lines
2.9 KiB
Dart
import 'package:hive_ce/hive.dart';
|
|
import 'package:worker/core/constants/storage_constants.dart';
|
|
import 'package:worker/features/cart/domain/entities/cart_item.dart';
|
|
|
|
part 'cart_item_model.g.dart';
|
|
|
|
/// Cart Item Model - Type ID: 5
|
|
///
|
|
/// Includes product details from cart API to avoid fetching each product.
|
|
@HiveType(typeId: HiveTypeIds.cartItemModel)
|
|
class CartItemModel extends HiveObject {
|
|
CartItemModel({
|
|
required this.cartItemId,
|
|
required this.cartId,
|
|
required this.productId,
|
|
required this.quantity,
|
|
required this.unitPrice,
|
|
required this.subtotal,
|
|
required this.addedAt,
|
|
this.itemName,
|
|
this.image,
|
|
this.conversionOfSm,
|
|
});
|
|
|
|
@HiveField(0)
|
|
final String cartItemId;
|
|
|
|
@HiveField(1)
|
|
final String cartId;
|
|
|
|
@HiveField(2)
|
|
final String productId;
|
|
|
|
@HiveField(3)
|
|
final double quantity;
|
|
|
|
@HiveField(4)
|
|
final double unitPrice;
|
|
|
|
@HiveField(5)
|
|
final double subtotal;
|
|
|
|
@HiveField(6)
|
|
final DateTime addedAt;
|
|
|
|
/// Product name from cart API
|
|
@HiveField(7)
|
|
final String? itemName;
|
|
|
|
/// Product image URL from cart API
|
|
@HiveField(8)
|
|
final String? image;
|
|
|
|
/// Conversion factor (m² to tiles) from cart API
|
|
@HiveField(9)
|
|
final double? conversionOfSm;
|
|
|
|
factory CartItemModel.fromJson(Map<String, dynamic> json) {
|
|
return CartItemModel(
|
|
cartItemId: json['cart_item_id'] as String,
|
|
cartId: json['cart_id'] as String,
|
|
productId: json['product_id'] as String,
|
|
quantity: (json['quantity'] as num).toDouble(),
|
|
unitPrice: (json['unit_price'] as num).toDouble(),
|
|
subtotal: (json['subtotal'] as num).toDouble(),
|
|
addedAt: DateTime.parse(json['added_at'] as String),
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'cart_item_id': cartItemId,
|
|
'cart_id': cartId,
|
|
'product_id': productId,
|
|
'quantity': quantity,
|
|
'unit_price': unitPrice,
|
|
'subtotal': subtotal,
|
|
'added_at': addedAt.toIso8601String(),
|
|
};
|
|
|
|
CartItemModel copyWith({
|
|
String? cartItemId,
|
|
String? cartId,
|
|
String? productId,
|
|
double? quantity,
|
|
double? unitPrice,
|
|
double? subtotal,
|
|
DateTime? addedAt,
|
|
String? itemName,
|
|
String? image,
|
|
double? conversionOfSm,
|
|
}) => CartItemModel(
|
|
cartItemId: cartItemId ?? this.cartItemId,
|
|
cartId: cartId ?? this.cartId,
|
|
productId: productId ?? this.productId,
|
|
quantity: quantity ?? this.quantity,
|
|
unitPrice: unitPrice ?? this.unitPrice,
|
|
subtotal: subtotal ?? this.subtotal,
|
|
addedAt: addedAt ?? this.addedAt,
|
|
itemName: itemName ?? this.itemName,
|
|
image: image ?? this.image,
|
|
conversionOfSm: conversionOfSm ?? this.conversionOfSm,
|
|
);
|
|
|
|
/// Convert to domain entity
|
|
CartItem toEntity() => CartItem(
|
|
cartItemId: cartItemId,
|
|
cartId: cartId,
|
|
productId: productId,
|
|
quantity: quantity,
|
|
unitPrice: unitPrice,
|
|
subtotal: subtotal,
|
|
addedAt: addedAt,
|
|
itemName: itemName,
|
|
image: image,
|
|
conversionOfSm: conversionOfSm,
|
|
);
|
|
}
|