update cart/favorite
This commit is contained in:
@@ -190,15 +190,21 @@ class CartRemoteDataSourceImpl implements CartRemoteDataSource {
|
||||
try {
|
||||
// Map API response to CartItemModel
|
||||
// API fields: name, item, quantity, amount, item_code, item_name, image, conversion_of_sm
|
||||
final quantity = (item['quantity'] as num?)?.toDouble() ?? 0.0;
|
||||
final unitPrice = (item['amount'] as num?)?.toDouble() ?? 0.0;
|
||||
|
||||
final cartItem = CartItemModel(
|
||||
cartItemId: item['name'] as String? ?? '',
|
||||
cartId: 'user_cart', // Fixed cart ID for user's cart
|
||||
productId: item['item_code'] as String? ?? item['item'] as String? ?? '',
|
||||
quantity: (item['quantity'] as num?)?.toDouble() ?? 0.0,
|
||||
unitPrice: (item['amount'] as num?)?.toDouble() ?? 0.0,
|
||||
subtotal: ((item['quantity'] as num?)?.toDouble() ?? 0.0) *
|
||||
((item['amount'] as num?)?.toDouble() ?? 0.0),
|
||||
quantity: quantity,
|
||||
unitPrice: unitPrice,
|
||||
subtotal: quantity * unitPrice,
|
||||
addedAt: DateTime.now(), // API doesn't provide timestamp
|
||||
// Product details from cart API - no need to fetch separately
|
||||
itemName: item['item_name'] as String?,
|
||||
image: item['image'] as String?,
|
||||
conversionOfSm: (item['conversion_of_sm'] as num?)?.toDouble(),
|
||||
);
|
||||
|
||||
cartItems.add(cartItem);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
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({
|
||||
@@ -14,6 +17,9 @@ class CartItemModel extends HiveObject {
|
||||
required this.unitPrice,
|
||||
required this.subtotal,
|
||||
required this.addedAt,
|
||||
this.itemName,
|
||||
this.image,
|
||||
this.conversionOfSm,
|
||||
});
|
||||
|
||||
@HiveField(0)
|
||||
@@ -37,6 +43,18 @@ class CartItemModel extends HiveObject {
|
||||
@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,
|
||||
@@ -67,6 +85,9 @@ class CartItemModel extends HiveObject {
|
||||
double? unitPrice,
|
||||
double? subtotal,
|
||||
DateTime? addedAt,
|
||||
String? itemName,
|
||||
String? image,
|
||||
double? conversionOfSm,
|
||||
}) => CartItemModel(
|
||||
cartItemId: cartItemId ?? this.cartItemId,
|
||||
cartId: cartId ?? this.cartId,
|
||||
@@ -75,5 +96,22 @@ class CartItemModel extends HiveObject {
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -24,13 +24,16 @@ class CartItemModelAdapter extends TypeAdapter<CartItemModel> {
|
||||
unitPrice: (fields[4] as num).toDouble(),
|
||||
subtotal: (fields[5] as num).toDouble(),
|
||||
addedAt: fields[6] as DateTime,
|
||||
itemName: fields[7] as String?,
|
||||
image: fields[8] as String?,
|
||||
conversionOfSm: (fields[9] as num?)?.toDouble(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, CartItemModel obj) {
|
||||
writer
|
||||
..writeByte(7)
|
||||
..writeByte(10)
|
||||
..writeByte(0)
|
||||
..write(obj.cartItemId)
|
||||
..writeByte(1)
|
||||
@@ -44,7 +47,13 @@ class CartItemModelAdapter extends TypeAdapter<CartItemModel> {
|
||||
..writeByte(5)
|
||||
..write(obj.subtotal)
|
||||
..writeByte(6)
|
||||
..write(obj.addedAt);
|
||||
..write(obj.addedAt)
|
||||
..writeByte(7)
|
||||
..write(obj.itemName)
|
||||
..writeByte(8)
|
||||
..write(obj.image)
|
||||
..writeByte(9)
|
||||
..write(obj.conversionOfSm);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -36,6 +36,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
required List<String> itemIds,
|
||||
required List<double> quantities,
|
||||
required List<double> prices,
|
||||
List<double?>? conversionFactors,
|
||||
}) async {
|
||||
try {
|
||||
// Validate input
|
||||
@@ -48,11 +49,16 @@ class CartRepositoryImpl implements CartRepository {
|
||||
// Build API request items
|
||||
final items = <Map<String, dynamic>>[];
|
||||
for (int i = 0; i < itemIds.length; i++) {
|
||||
items.add({
|
||||
final item = <String, dynamic>{
|
||||
'item_id': itemIds[i],
|
||||
'quantity': quantities[i],
|
||||
'amount': prices[i],
|
||||
});
|
||||
};
|
||||
// Add conversion_of_sm if provided
|
||||
if (conversionFactors != null && i < conversionFactors.length) {
|
||||
item['conversion_of_sm'] = conversionFactors[i] ?? 0.0;
|
||||
}
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
// Try API first
|
||||
@@ -66,6 +72,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
productId: itemIds[i],
|
||||
quantity: quantities[i],
|
||||
unitPrice: prices[i],
|
||||
conversionOfSm: conversionFactors?[i],
|
||||
);
|
||||
await _localDataSource.addCartItem(cartItemModel);
|
||||
}
|
||||
@@ -80,6 +87,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
productId: itemIds[i],
|
||||
quantity: quantities[i],
|
||||
unitPrice: prices[i],
|
||||
conversionOfSm: conversionFactors?[i],
|
||||
);
|
||||
await _localDataSource.addCartItem(cartItemModel);
|
||||
}
|
||||
@@ -176,6 +184,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
required String itemId,
|
||||
required double quantity,
|
||||
required double price,
|
||||
double? conversionFactor,
|
||||
}) async {
|
||||
try {
|
||||
// API doesn't have update endpoint, use add with new quantity
|
||||
@@ -184,6 +193,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
itemIds: [itemId],
|
||||
quantities: [quantity],
|
||||
prices: [price],
|
||||
conversionFactors: conversionFactor != null ? [conversionFactor] : null,
|
||||
);
|
||||
} catch (e) {
|
||||
throw UnknownException('Failed to update cart item quantity', e);
|
||||
@@ -268,6 +278,9 @@ class CartRepositoryImpl implements CartRepository {
|
||||
unitPrice: model.unitPrice,
|
||||
subtotal: model.subtotal,
|
||||
addedAt: model.addedAt,
|
||||
itemName: model.itemName,
|
||||
image: model.image,
|
||||
conversionOfSm: model.conversionOfSm,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -276,6 +289,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
required String productId,
|
||||
required double quantity,
|
||||
required double unitPrice,
|
||||
double? conversionOfSm,
|
||||
}) {
|
||||
return CartItemModel(
|
||||
cartItemId: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
@@ -285,6 +299,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
unitPrice: unitPrice,
|
||||
subtotal: quantity * unitPrice,
|
||||
addedAt: DateTime.now(),
|
||||
conversionOfSm: conversionOfSm,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user