runable
This commit is contained in:
115
lib/features/products/data/models/product_model.dart
Normal file
115
lib/features/products/data/models/product_model.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import '../../domain/entities/product.dart';
|
||||
import '../../../../core/constants/storage_constants.dart';
|
||||
|
||||
part 'product_model.g.dart';
|
||||
|
||||
@HiveType(typeId: StorageConstants.productTypeId)
|
||||
class ProductModel extends HiveObject {
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
@HiveField(1)
|
||||
final String name;
|
||||
|
||||
@HiveField(2)
|
||||
final String description;
|
||||
|
||||
@HiveField(3)
|
||||
final double price;
|
||||
|
||||
@HiveField(4)
|
||||
final String? imageUrl;
|
||||
|
||||
@HiveField(5)
|
||||
final String categoryId;
|
||||
|
||||
@HiveField(6)
|
||||
final int stockQuantity;
|
||||
|
||||
@HiveField(7)
|
||||
final bool isAvailable;
|
||||
|
||||
@HiveField(8)
|
||||
final DateTime createdAt;
|
||||
|
||||
@HiveField(9)
|
||||
final DateTime updatedAt;
|
||||
|
||||
ProductModel({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.description,
|
||||
required this.price,
|
||||
this.imageUrl,
|
||||
required this.categoryId,
|
||||
required this.stockQuantity,
|
||||
required this.isAvailable,
|
||||
required this.createdAt,
|
||||
required this.updatedAt,
|
||||
});
|
||||
|
||||
/// Convert to domain entity
|
||||
Product toEntity() {
|
||||
return Product(
|
||||
id: id,
|
||||
name: name,
|
||||
description: description,
|
||||
price: price,
|
||||
imageUrl: imageUrl,
|
||||
categoryId: categoryId,
|
||||
stockQuantity: stockQuantity,
|
||||
isAvailable: isAvailable,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create from domain entity
|
||||
factory ProductModel.fromEntity(Product product) {
|
||||
return ProductModel(
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
description: product.description,
|
||||
price: product.price,
|
||||
imageUrl: product.imageUrl,
|
||||
categoryId: product.categoryId,
|
||||
stockQuantity: product.stockQuantity,
|
||||
isAvailable: product.isAvailable,
|
||||
createdAt: product.createdAt,
|
||||
updatedAt: product.updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create from JSON
|
||||
factory ProductModel.fromJson(Map<String, dynamic> json) {
|
||||
return ProductModel(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
description: json['description'] as String,
|
||||
price: (json['price'] as num).toDouble(),
|
||||
imageUrl: json['imageUrl'] as String?,
|
||||
categoryId: json['categoryId'] as String,
|
||||
stockQuantity: json['stockQuantity'] as int,
|
||||
isAvailable: json['isAvailable'] as bool,
|
||||
createdAt: DateTime.parse(json['createdAt'] as String),
|
||||
updatedAt: DateTime.parse(json['updatedAt'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert to JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'description': description,
|
||||
'price': price,
|
||||
'imageUrl': imageUrl,
|
||||
'categoryId': categoryId,
|
||||
'stockQuantity': stockQuantity,
|
||||
'isAvailable': isAvailable,
|
||||
'createdAt': createdAt.toIso8601String(),
|
||||
'updatedAt': updatedAt.toIso8601String(),
|
||||
};
|
||||
}
|
||||
}
|
||||
68
lib/features/products/data/models/product_model.g.dart
Normal file
68
lib/features/products/data/models/product_model.g.dart
Normal file
@@ -0,0 +1,68 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'product_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class ProductModelAdapter extends TypeAdapter<ProductModel> {
|
||||
@override
|
||||
final typeId = 0;
|
||||
|
||||
@override
|
||||
ProductModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return ProductModel(
|
||||
id: fields[0] as String,
|
||||
name: fields[1] as String,
|
||||
description: fields[2] as String,
|
||||
price: (fields[3] as num).toDouble(),
|
||||
imageUrl: fields[4] as String?,
|
||||
categoryId: fields[5] as String,
|
||||
stockQuantity: (fields[6] as num).toInt(),
|
||||
isAvailable: fields[7] as bool,
|
||||
createdAt: fields[8] as DateTime,
|
||||
updatedAt: fields[9] as DateTime,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, ProductModel obj) {
|
||||
writer
|
||||
..writeByte(10)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.name)
|
||||
..writeByte(2)
|
||||
..write(obj.description)
|
||||
..writeByte(3)
|
||||
..write(obj.price)
|
||||
..writeByte(4)
|
||||
..write(obj.imageUrl)
|
||||
..writeByte(5)
|
||||
..write(obj.categoryId)
|
||||
..writeByte(6)
|
||||
..write(obj.stockQuantity)
|
||||
..writeByte(7)
|
||||
..write(obj.isAvailable)
|
||||
..writeByte(8)
|
||||
..write(obj.createdAt)
|
||||
..writeByte(9)
|
||||
..write(obj.updatedAt);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is ProductModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
Reference in New Issue
Block a user