Initialer Commit: Projekt Start
This commit is contained in:
96
lib/models/product.dart
Normal file
96
lib/models/product.dart
Normal file
@@ -0,0 +1,96 @@
|
||||
class Product {
|
||||
final int id;
|
||||
final String name;
|
||||
final String description;
|
||||
final String price;
|
||||
final String? regularPrice;
|
||||
final String? salePrice;
|
||||
final String? imageUrl;
|
||||
final List<String> images;
|
||||
final bool inStock;
|
||||
final int stockQuantity;
|
||||
final String? sku;
|
||||
final List<Map<String, dynamic>>? categories;
|
||||
final double? rating;
|
||||
final int? ratingCount;
|
||||
|
||||
Product({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.description,
|
||||
required this.price,
|
||||
this.regularPrice,
|
||||
this.salePrice,
|
||||
this.imageUrl,
|
||||
this.images = const [],
|
||||
this.inStock = true,
|
||||
this.stockQuantity = 0,
|
||||
this.sku,
|
||||
this.categories,
|
||||
this.rating,
|
||||
this.ratingCount,
|
||||
});
|
||||
|
||||
factory Product.fromJson(Map<String, dynamic> json) {
|
||||
final images = <String>[];
|
||||
|
||||
if (json['images'] != null && json['images'] is List) {
|
||||
for (var img in json['images']) {
|
||||
if (img['src'] != null) {
|
||||
images.add(img['src']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Product(
|
||||
id: json['id'] ?? 0,
|
||||
name: json['name'] ?? '',
|
||||
description: json['description'] ?? '',
|
||||
price: json['price'] ?? '0',
|
||||
regularPrice: json['regular_price'],
|
||||
salePrice: json['sale_price'],
|
||||
imageUrl: images.isNotEmpty ? images[0] : null,
|
||||
images: images,
|
||||
inStock: json['stock_status'] == 'instock',
|
||||
stockQuantity: json['stock_quantity'] ?? 0,
|
||||
sku: json['sku'],
|
||||
categories: json['categories'] != null
|
||||
? List<Map<String, dynamic>>.from(json['categories'])
|
||||
: null,
|
||||
rating: json['average_rating'] != null
|
||||
? double.tryParse(json['average_rating'].toString())
|
||||
: null,
|
||||
ratingCount: json['rating_count'],
|
||||
);
|
||||
}
|
||||
|
||||
bool get isOnSale => salePrice != null && salePrice!.isNotEmpty;
|
||||
|
||||
double get priceValue {
|
||||
try {
|
||||
return double.parse(price);
|
||||
} catch (e) {
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'description': description,
|
||||
'price': price,
|
||||
'regular_price': regularPrice,
|
||||
'sale_price': salePrice,
|
||||
'image_url': imageUrl,
|
||||
'images': images,
|
||||
'in_stock': inStock,
|
||||
'stock_quantity': stockQuantity,
|
||||
'sku': sku,
|
||||
'categories': categories,
|
||||
'average_rating': rating,
|
||||
'rating_count': ratingCount,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user