runable
This commit is contained in:
83
lib/features/home/data/models/cart_item_model.dart
Normal file
83
lib/features/home/data/models/cart_item_model.dart
Normal file
@@ -0,0 +1,83 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import '../../domain/entities/cart_item.dart';
|
||||
import '../../../../core/constants/storage_constants.dart';
|
||||
|
||||
part 'cart_item_model.g.dart';
|
||||
|
||||
@HiveType(typeId: StorageConstants.cartItemTypeId)
|
||||
class CartItemModel extends HiveObject {
|
||||
@HiveField(0)
|
||||
final String productId;
|
||||
|
||||
@HiveField(1)
|
||||
final String productName;
|
||||
|
||||
@HiveField(2)
|
||||
final double price;
|
||||
|
||||
@HiveField(3)
|
||||
final int quantity;
|
||||
|
||||
@HiveField(4)
|
||||
final String? imageUrl;
|
||||
|
||||
@HiveField(5)
|
||||
final DateTime addedAt;
|
||||
|
||||
CartItemModel({
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
required this.price,
|
||||
required this.quantity,
|
||||
this.imageUrl,
|
||||
required this.addedAt,
|
||||
});
|
||||
|
||||
/// Convert to domain entity
|
||||
CartItem toEntity() {
|
||||
return CartItem(
|
||||
productId: productId,
|
||||
productName: productName,
|
||||
price: price,
|
||||
quantity: quantity,
|
||||
imageUrl: imageUrl,
|
||||
addedAt: addedAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create from domain entity
|
||||
factory CartItemModel.fromEntity(CartItem item) {
|
||||
return CartItemModel(
|
||||
productId: item.productId,
|
||||
productName: item.productName,
|
||||
price: item.price,
|
||||
quantity: item.quantity,
|
||||
imageUrl: item.imageUrl,
|
||||
addedAt: item.addedAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert to JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'productId': productId,
|
||||
'productName': productName,
|
||||
'price': price,
|
||||
'quantity': quantity,
|
||||
'imageUrl': imageUrl,
|
||||
'addedAt': addedAt.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
/// Create from JSON
|
||||
factory CartItemModel.fromJson(Map<String, dynamic> json) {
|
||||
return CartItemModel(
|
||||
productId: json['productId'] as String,
|
||||
productName: json['productName'] as String,
|
||||
price: (json['price'] as num).toDouble(),
|
||||
quantity: json['quantity'] as int,
|
||||
imageUrl: json['imageUrl'] as String?,
|
||||
addedAt: DateTime.parse(json['addedAt'] as String),
|
||||
);
|
||||
}
|
||||
}
|
||||
56
lib/features/home/data/models/cart_item_model.g.dart
Normal file
56
lib/features/home/data/models/cart_item_model.g.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'cart_item_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class CartItemModelAdapter extends TypeAdapter<CartItemModel> {
|
||||
@override
|
||||
final typeId = 2;
|
||||
|
||||
@override
|
||||
CartItemModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return CartItemModel(
|
||||
productId: fields[0] as String,
|
||||
productName: fields[1] as String,
|
||||
price: (fields[2] as num).toDouble(),
|
||||
quantity: (fields[3] as num).toInt(),
|
||||
imageUrl: fields[4] as String?,
|
||||
addedAt: fields[5] as DateTime,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, CartItemModel obj) {
|
||||
writer
|
||||
..writeByte(6)
|
||||
..writeByte(0)
|
||||
..write(obj.productId)
|
||||
..writeByte(1)
|
||||
..write(obj.productName)
|
||||
..writeByte(2)
|
||||
..write(obj.price)
|
||||
..writeByte(3)
|
||||
..write(obj.quantity)
|
||||
..writeByte(4)
|
||||
..write(obj.imageUrl)
|
||||
..writeByte(5)
|
||||
..write(obj.addedAt);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is CartItemModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
123
lib/features/home/data/models/transaction_model.dart
Normal file
123
lib/features/home/data/models/transaction_model.dart
Normal file
@@ -0,0 +1,123 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:retail/core/constants/storage_constants.dart';
|
||||
import 'package:retail/features/home/data/models/cart_item_model.dart';
|
||||
|
||||
part 'transaction_model.g.dart';
|
||||
|
||||
/// Transaction model with Hive CE type adapter
|
||||
@HiveType(typeId: StorageConstants.transactionTypeId)
|
||||
class TransactionModel extends HiveObject {
|
||||
/// Unique transaction identifier
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
/// List of cart items in this transaction
|
||||
@HiveField(1)
|
||||
final List<CartItemModel> items;
|
||||
|
||||
/// Subtotal amount (before tax and discount)
|
||||
@HiveField(2)
|
||||
final double subtotal;
|
||||
|
||||
/// Tax amount
|
||||
@HiveField(3)
|
||||
final double tax;
|
||||
|
||||
/// Discount amount
|
||||
@HiveField(4)
|
||||
final double discount;
|
||||
|
||||
/// Total amount (subtotal + tax - discount)
|
||||
@HiveField(5)
|
||||
final double total;
|
||||
|
||||
/// Transaction completion timestamp
|
||||
@HiveField(6)
|
||||
final DateTime completedAt;
|
||||
|
||||
/// Payment method used (e.g., 'cash', 'card', 'digital')
|
||||
@HiveField(7)
|
||||
final String paymentMethod;
|
||||
|
||||
TransactionModel({
|
||||
required this.id,
|
||||
required this.items,
|
||||
required this.subtotal,
|
||||
required this.tax,
|
||||
required this.discount,
|
||||
required this.total,
|
||||
required this.completedAt,
|
||||
required this.paymentMethod,
|
||||
});
|
||||
|
||||
/// Get total number of items in transaction
|
||||
int get totalItems => items.fold(0, (sum, item) => sum + item.quantity);
|
||||
|
||||
/// Create a copy with updated fields
|
||||
TransactionModel copyWith({
|
||||
String? id,
|
||||
List<CartItemModel>? items,
|
||||
double? subtotal,
|
||||
double? tax,
|
||||
double? discount,
|
||||
double? total,
|
||||
DateTime? completedAt,
|
||||
String? paymentMethod,
|
||||
}) {
|
||||
return TransactionModel(
|
||||
id: id ?? this.id,
|
||||
items: items ?? this.items,
|
||||
subtotal: subtotal ?? this.subtotal,
|
||||
tax: tax ?? this.tax,
|
||||
discount: discount ?? this.discount,
|
||||
total: total ?? this.total,
|
||||
completedAt: completedAt ?? this.completedAt,
|
||||
paymentMethod: paymentMethod ?? this.paymentMethod,
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert to JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'items': items.map((item) => item.toJson()).toList(),
|
||||
'subtotal': subtotal,
|
||||
'tax': tax,
|
||||
'discount': discount,
|
||||
'total': total,
|
||||
'completedAt': completedAt.toIso8601String(),
|
||||
'paymentMethod': paymentMethod,
|
||||
};
|
||||
}
|
||||
|
||||
/// Create from JSON
|
||||
factory TransactionModel.fromJson(Map<String, dynamic> json) {
|
||||
return TransactionModel(
|
||||
id: json['id'] as String,
|
||||
items: (json['items'] as List)
|
||||
.map((item) => CartItemModel.fromJson(item as Map<String, dynamic>))
|
||||
.toList(),
|
||||
subtotal: (json['subtotal'] as num).toDouble(),
|
||||
tax: (json['tax'] as num).toDouble(),
|
||||
discount: (json['discount'] as num).toDouble(),
|
||||
total: (json['total'] as num).toDouble(),
|
||||
completedAt: DateTime.parse(json['completedAt'] as String),
|
||||
paymentMethod: json['paymentMethod'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'TransactionModel(id: $id, total: $total, items: ${items.length}, method: $paymentMethod)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is TransactionModel && other.id == id;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => id.hashCode;
|
||||
}
|
||||
62
lib/features/home/data/models/transaction_model.g.dart
Normal file
62
lib/features/home/data/models/transaction_model.g.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'transaction_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class TransactionModelAdapter extends TypeAdapter<TransactionModel> {
|
||||
@override
|
||||
final typeId = 3;
|
||||
|
||||
@override
|
||||
TransactionModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return TransactionModel(
|
||||
id: fields[0] as String,
|
||||
items: (fields[1] as List).cast<CartItemModel>(),
|
||||
subtotal: (fields[2] as num).toDouble(),
|
||||
tax: (fields[3] as num).toDouble(),
|
||||
discount: (fields[4] as num).toDouble(),
|
||||
total: (fields[5] as num).toDouble(),
|
||||
completedAt: fields[6] as DateTime,
|
||||
paymentMethod: fields[7] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, TransactionModel obj) {
|
||||
writer
|
||||
..writeByte(8)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.items)
|
||||
..writeByte(2)
|
||||
..write(obj.subtotal)
|
||||
..writeByte(3)
|
||||
..write(obj.tax)
|
||||
..writeByte(4)
|
||||
..write(obj.discount)
|
||||
..writeByte(5)
|
||||
..write(obj.total)
|
||||
..writeByte(6)
|
||||
..write(obj.completedAt)
|
||||
..writeByte(7)
|
||||
..write(obj.paymentMethod);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is TransactionModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
Reference in New Issue
Block a user