add favorite

This commit is contained in:
Phuoc Nguyen
2025-11-18 11:23:07 +07:00
parent 192c322816
commit a5eb95fa64
25 changed files with 2506 additions and 978 deletions

View File

@@ -292,6 +292,59 @@ class ProductModel extends HiveObject {
);
}
/// Create ProductModel from Wishlist API JSON
///
/// The wishlist API returns a simplified product structure:
/// - name: Item code (e.g., "GIB20 G04")
/// - item_code: Item code (duplicate of name)
/// - item_name: Display name (e.g., "Gibellina GIB20 G04")
/// - item_group_name: Category (e.g., "OUTDOOR [20mm]")
/// - custom_link_360: 360 view link
/// - thumbnail: Thumbnail image URL
/// - price: Price (usually 0 from wishlist)
/// - currency: Currency code
/// - conversion_of_sm: Conversion factor
factory ProductModel.fromWishlistApi(Map<String, dynamic> json) {
final now = DateTime.now();
// Handle thumbnail URL
String thumbnailUrl = '';
if (json['thumbnail'] != null && (json['thumbnail'] as String).isNotEmpty) {
final thumbnailPath = json['thumbnail'] as String;
if (thumbnailPath.startsWith('http')) {
thumbnailUrl = thumbnailPath;
} else if (thumbnailPath.startsWith('/')) {
thumbnailUrl = '${ApiConstants.baseUrl}$thumbnailPath';
} else {
thumbnailUrl = '${ApiConstants.baseUrl}/$thumbnailPath';
}
}
return ProductModel(
productId: json['item_code'] as String? ?? json['name'] as String,
name: json['item_name'] as String? ?? json['name'] as String,
description: null, // Not provided by wishlist API
basePrice: (json['price'] as num?)?.toDouble() ?? 0.0,
images: null, // Not provided by wishlist API
thumbnail: thumbnailUrl,
imageCaptions: null,
customLink360: json['custom_link_360'] as String?,
specifications: null,
category: json['item_group_name'] as String?,
brand: null, // Not provided by wishlist API
unit: json['currency'] as String? ?? '',
conversionOfSm: json['conversion_of_sm'] != null
? (json['conversion_of_sm'] as num).toDouble()
: null,
introAttributes: null,
isActive: true, // Assume active if in wishlist
isFeatured: false,
erpnextItemCode: json['item_code'] as String? ?? json['name'] as String,
createdAt: now,
updatedAt: null,
);
}
/// Convert ProductModel to JSON
Map<String, dynamic> toJson() {
return {