update database
This commit is contained in:
79
lib/features/cart/data/models/cart_item_model.dart
Normal file
79
lib/features/cart/data/models/cart_item_model.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:worker/core/constants/storage_constants.dart';
|
||||
|
||||
part 'cart_item_model.g.dart';
|
||||
|
||||
/// Cart Item Model - Type ID: 5
|
||||
@HiveType(typeId: HiveTypeIds.cartItemModel)
|
||||
class CartItemModel extends HiveObject {
|
||||
CartItemModel({
|
||||
required this.cartItemId,
|
||||
required this.cartId,
|
||||
required this.productId,
|
||||
required this.quantity,
|
||||
required this.unitPrice,
|
||||
required this.subtotal,
|
||||
required this.addedAt,
|
||||
});
|
||||
|
||||
@HiveField(0)
|
||||
final String cartItemId;
|
||||
|
||||
@HiveField(1)
|
||||
final String cartId;
|
||||
|
||||
@HiveField(2)
|
||||
final String productId;
|
||||
|
||||
@HiveField(3)
|
||||
final double quantity;
|
||||
|
||||
@HiveField(4)
|
||||
final double unitPrice;
|
||||
|
||||
@HiveField(5)
|
||||
final double subtotal;
|
||||
|
||||
@HiveField(6)
|
||||
final DateTime addedAt;
|
||||
|
||||
factory CartItemModel.fromJson(Map<String, dynamic> json) {
|
||||
return CartItemModel(
|
||||
cartItemId: json['cart_item_id'] as String,
|
||||
cartId: json['cart_id'] as String,
|
||||
productId: json['product_id'] as String,
|
||||
quantity: (json['quantity'] as num).toDouble(),
|
||||
unitPrice: (json['unit_price'] as num).toDouble(),
|
||||
subtotal: (json['subtotal'] as num).toDouble(),
|
||||
addedAt: DateTime.parse(json['added_at'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'cart_item_id': cartItemId,
|
||||
'cart_id': cartId,
|
||||
'product_id': productId,
|
||||
'quantity': quantity,
|
||||
'unit_price': unitPrice,
|
||||
'subtotal': subtotal,
|
||||
'added_at': addedAt.toIso8601String(),
|
||||
};
|
||||
|
||||
CartItemModel copyWith({
|
||||
String? cartItemId,
|
||||
String? cartId,
|
||||
String? productId,
|
||||
double? quantity,
|
||||
double? unitPrice,
|
||||
double? subtotal,
|
||||
DateTime? addedAt,
|
||||
}) => CartItemModel(
|
||||
cartItemId: cartItemId ?? this.cartItemId,
|
||||
cartId: cartId ?? this.cartId,
|
||||
productId: productId ?? this.productId,
|
||||
quantity: quantity ?? this.quantity,
|
||||
unitPrice: unitPrice ?? this.unitPrice,
|
||||
subtotal: subtotal ?? this.subtotal,
|
||||
addedAt: addedAt ?? this.addedAt,
|
||||
);
|
||||
}
|
||||
59
lib/features/cart/data/models/cart_item_model.g.dart
Normal file
59
lib/features/cart/data/models/cart_item_model.g.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'cart_item_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class CartItemModelAdapter extends TypeAdapter<CartItemModel> {
|
||||
@override
|
||||
final typeId = 5;
|
||||
|
||||
@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(
|
||||
cartItemId: fields[0] as String,
|
||||
cartId: fields[1] as String,
|
||||
productId: fields[2] as String,
|
||||
quantity: (fields[3] as num).toDouble(),
|
||||
unitPrice: (fields[4] as num).toDouble(),
|
||||
subtotal: (fields[5] as num).toDouble(),
|
||||
addedAt: fields[6] as DateTime,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, CartItemModel obj) {
|
||||
writer
|
||||
..writeByte(7)
|
||||
..writeByte(0)
|
||||
..write(obj.cartItemId)
|
||||
..writeByte(1)
|
||||
..write(obj.cartId)
|
||||
..writeByte(2)
|
||||
..write(obj.productId)
|
||||
..writeByte(3)
|
||||
..write(obj.quantity)
|
||||
..writeByte(4)
|
||||
..write(obj.unitPrice)
|
||||
..writeByte(5)
|
||||
..write(obj.subtotal)
|
||||
..writeByte(6)
|
||||
..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;
|
||||
}
|
||||
71
lib/features/cart/data/models/cart_model.dart
Normal file
71
lib/features/cart/data/models/cart_model.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:worker/core/constants/storage_constants.dart';
|
||||
|
||||
part 'cart_model.g.dart';
|
||||
|
||||
/// Cart Model - Type ID: 4
|
||||
@HiveType(typeId: HiveTypeIds.cartModel)
|
||||
class CartModel extends HiveObject {
|
||||
CartModel({
|
||||
required this.cartId,
|
||||
required this.userId,
|
||||
required this.totalAmount,
|
||||
required this.isSynced,
|
||||
required this.lastModified,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
@HiveField(0)
|
||||
final String cartId;
|
||||
|
||||
@HiveField(1)
|
||||
final String userId;
|
||||
|
||||
@HiveField(2)
|
||||
final double totalAmount;
|
||||
|
||||
@HiveField(3)
|
||||
final bool isSynced;
|
||||
|
||||
@HiveField(4)
|
||||
final DateTime lastModified;
|
||||
|
||||
@HiveField(5)
|
||||
final DateTime createdAt;
|
||||
|
||||
factory CartModel.fromJson(Map<String, dynamic> json) {
|
||||
return CartModel(
|
||||
cartId: json['cart_id'] as String,
|
||||
userId: json['user_id'] as String,
|
||||
totalAmount: (json['total_amount'] as num).toDouble(),
|
||||
isSynced: json['is_synced'] as bool? ?? false,
|
||||
lastModified: DateTime.parse(json['last_modified'] as String),
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'cart_id': cartId,
|
||||
'user_id': userId,
|
||||
'total_amount': totalAmount,
|
||||
'is_synced': isSynced,
|
||||
'last_modified': lastModified.toIso8601String(),
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
};
|
||||
|
||||
CartModel copyWith({
|
||||
String? cartId,
|
||||
String? userId,
|
||||
double? totalAmount,
|
||||
bool? isSynced,
|
||||
DateTime? lastModified,
|
||||
DateTime? createdAt,
|
||||
}) => CartModel(
|
||||
cartId: cartId ?? this.cartId,
|
||||
userId: userId ?? this.userId,
|
||||
totalAmount: totalAmount ?? this.totalAmount,
|
||||
isSynced: isSynced ?? this.isSynced,
|
||||
lastModified: lastModified ?? this.lastModified,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
);
|
||||
}
|
||||
56
lib/features/cart/data/models/cart_model.g.dart
Normal file
56
lib/features/cart/data/models/cart_model.g.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'cart_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class CartModelAdapter extends TypeAdapter<CartModel> {
|
||||
@override
|
||||
final typeId = 4;
|
||||
|
||||
@override
|
||||
CartModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return CartModel(
|
||||
cartId: fields[0] as String,
|
||||
userId: fields[1] as String,
|
||||
totalAmount: (fields[2] as num).toDouble(),
|
||||
isSynced: fields[3] as bool,
|
||||
lastModified: fields[4] as DateTime,
|
||||
createdAt: fields[5] as DateTime,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, CartModel obj) {
|
||||
writer
|
||||
..writeByte(6)
|
||||
..writeByte(0)
|
||||
..write(obj.cartId)
|
||||
..writeByte(1)
|
||||
..write(obj.userId)
|
||||
..writeByte(2)
|
||||
..write(obj.totalAmount)
|
||||
..writeByte(3)
|
||||
..write(obj.isSynced)
|
||||
..writeByte(4)
|
||||
..write(obj.lastModified)
|
||||
..writeByte(5)
|
||||
..write(obj.createdAt);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is CartModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
92
lib/features/cart/domain/entities/cart.dart
Normal file
92
lib/features/cart/domain/entities/cart.dart
Normal file
@@ -0,0 +1,92 @@
|
||||
/// Domain Entity: Cart
|
||||
///
|
||||
/// Represents a shopping cart for a user.
|
||||
library;
|
||||
|
||||
/// Cart Entity
|
||||
///
|
||||
/// Contains cart-level information:
|
||||
/// - User ID
|
||||
/// - Total amount
|
||||
/// - Sync status
|
||||
/// - Timestamps
|
||||
class Cart {
|
||||
/// Unique cart identifier
|
||||
final String cartId;
|
||||
|
||||
/// User ID who owns this cart
|
||||
final String userId;
|
||||
|
||||
/// Total cart amount
|
||||
final double totalAmount;
|
||||
|
||||
/// Whether cart is synced with backend
|
||||
final bool isSynced;
|
||||
|
||||
/// Last modification timestamp
|
||||
final DateTime lastModified;
|
||||
|
||||
/// Cart creation timestamp
|
||||
final DateTime createdAt;
|
||||
|
||||
const Cart({
|
||||
required this.cartId,
|
||||
required this.userId,
|
||||
required this.totalAmount,
|
||||
required this.isSynced,
|
||||
required this.lastModified,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
/// Check if cart is empty
|
||||
bool get isEmpty => totalAmount == 0;
|
||||
|
||||
/// Check if cart needs sync
|
||||
bool get needsSync => !isSynced;
|
||||
|
||||
/// Copy with method for immutability
|
||||
Cart copyWith({
|
||||
String? cartId,
|
||||
String? userId,
|
||||
double? totalAmount,
|
||||
bool? isSynced,
|
||||
DateTime? lastModified,
|
||||
DateTime? createdAt,
|
||||
}) {
|
||||
return Cart(
|
||||
cartId: cartId ?? this.cartId,
|
||||
userId: userId ?? this.userId,
|
||||
totalAmount: totalAmount ?? this.totalAmount,
|
||||
isSynced: isSynced ?? this.isSynced,
|
||||
lastModified: lastModified ?? this.lastModified,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is Cart &&
|
||||
other.cartId == cartId &&
|
||||
other.userId == userId &&
|
||||
other.totalAmount == totalAmount &&
|
||||
other.isSynced == isSynced;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hash(
|
||||
cartId,
|
||||
userId,
|
||||
totalAmount,
|
||||
isSynced,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Cart(cartId: $cartId, userId: $userId, totalAmount: $totalAmount, '
|
||||
'isSynced: $isSynced, lastModified: $lastModified)';
|
||||
}
|
||||
}
|
||||
98
lib/features/cart/domain/entities/cart_item.dart
Normal file
98
lib/features/cart/domain/entities/cart_item.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
/// Domain Entity: Cart Item
|
||||
///
|
||||
/// Represents a single item in a shopping cart.
|
||||
library;
|
||||
|
||||
/// Cart Item Entity
|
||||
///
|
||||
/// Contains item-level information:
|
||||
/// - Product reference
|
||||
/// - Quantity
|
||||
/// - Pricing
|
||||
class CartItem {
|
||||
/// Unique cart item identifier
|
||||
final String cartItemId;
|
||||
|
||||
/// Cart ID this item belongs to
|
||||
final String cartId;
|
||||
|
||||
/// Product ID
|
||||
final String productId;
|
||||
|
||||
/// Quantity ordered
|
||||
final double quantity;
|
||||
|
||||
/// Unit price at time of adding to cart
|
||||
final double unitPrice;
|
||||
|
||||
/// Subtotal (quantity * unitPrice)
|
||||
final double subtotal;
|
||||
|
||||
/// Timestamp when item was added
|
||||
final DateTime addedAt;
|
||||
|
||||
const CartItem({
|
||||
required this.cartItemId,
|
||||
required this.cartId,
|
||||
required this.productId,
|
||||
required this.quantity,
|
||||
required this.unitPrice,
|
||||
required this.subtotal,
|
||||
required this.addedAt,
|
||||
});
|
||||
|
||||
/// Calculate subtotal (for verification)
|
||||
double get calculatedSubtotal => quantity * unitPrice;
|
||||
|
||||
/// Copy with method for immutability
|
||||
CartItem copyWith({
|
||||
String? cartItemId,
|
||||
String? cartId,
|
||||
String? productId,
|
||||
double? quantity,
|
||||
double? unitPrice,
|
||||
double? subtotal,
|
||||
DateTime? addedAt,
|
||||
}) {
|
||||
return CartItem(
|
||||
cartItemId: cartItemId ?? this.cartItemId,
|
||||
cartId: cartId ?? this.cartId,
|
||||
productId: productId ?? this.productId,
|
||||
quantity: quantity ?? this.quantity,
|
||||
unitPrice: unitPrice ?? this.unitPrice,
|
||||
subtotal: subtotal ?? this.subtotal,
|
||||
addedAt: addedAt ?? this.addedAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is CartItem &&
|
||||
other.cartItemId == cartItemId &&
|
||||
other.cartId == cartId &&
|
||||
other.productId == productId &&
|
||||
other.quantity == quantity &&
|
||||
other.unitPrice == unitPrice &&
|
||||
other.subtotal == subtotal;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hash(
|
||||
cartItemId,
|
||||
cartId,
|
||||
productId,
|
||||
quantity,
|
||||
unitPrice,
|
||||
subtotal,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'CartItem(cartItemId: $cartItemId, productId: $productId, '
|
||||
'quantity: $quantity, unitPrice: $unitPrice, subtotal: $subtotal)';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user