favorite
This commit is contained in:
120
lib/features/favorites/data/models/favorite_model.dart
Normal file
120
lib/features/favorites/data/models/favorite_model.dart
Normal file
@@ -0,0 +1,120 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
|
||||
import 'package:worker/core/constants/storage_constants.dart';
|
||||
import 'package:worker/features/favorites/domain/entities/favorite.dart';
|
||||
|
||||
part 'favorite_model.g.dart';
|
||||
|
||||
/// Favorite Model
|
||||
///
|
||||
/// Hive CE model for storing user's favorite products locally.
|
||||
/// Maps to the 'favorites' table in the database.
|
||||
///
|
||||
/// Type ID: 28
|
||||
@HiveType(typeId: HiveTypeIds.favoriteModel)
|
||||
class FavoriteModel extends HiveObject {
|
||||
FavoriteModel({
|
||||
required this.favoriteId,
|
||||
required this.productId,
|
||||
required this.userId,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
/// Favorite ID (Primary Key)
|
||||
@HiveField(0)
|
||||
final String favoriteId;
|
||||
|
||||
/// Product ID (Foreign Key)
|
||||
@HiveField(1)
|
||||
final String productId;
|
||||
|
||||
/// User ID (Foreign Key)
|
||||
@HiveField(2)
|
||||
final String userId;
|
||||
|
||||
/// Created timestamp
|
||||
@HiveField(3)
|
||||
final DateTime createdAt;
|
||||
|
||||
// =========================================================================
|
||||
// JSON SERIALIZATION
|
||||
// =========================================================================
|
||||
|
||||
/// Create FavoriteModel from JSON
|
||||
factory FavoriteModel.fromJson(Map<String, dynamic> json) {
|
||||
return FavoriteModel(
|
||||
favoriteId: json['favorite_id'] as String,
|
||||
productId: json['product_id'] as String,
|
||||
userId: json['user_id'] as String,
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert FavoriteModel to JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'favorite_id': favoriteId,
|
||||
'product_id': productId,
|
||||
'user_id': userId,
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// COPY WITH
|
||||
// =========================================================================
|
||||
|
||||
/// Create a copy with updated fields
|
||||
FavoriteModel copyWith({
|
||||
String? favoriteId,
|
||||
String? productId,
|
||||
String? userId,
|
||||
DateTime? createdAt,
|
||||
}) {
|
||||
return FavoriteModel(
|
||||
favoriteId: favoriteId ?? this.favoriteId,
|
||||
productId: productId ?? this.productId,
|
||||
userId: userId ?? this.userId,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'FavoriteModel(favoriteId: $favoriteId, productId: $productId, userId: $userId)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is FavoriteModel && other.favoriteId == favoriteId;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => favoriteId.hashCode;
|
||||
|
||||
// =========================================================================
|
||||
// ENTITY CONVERSION
|
||||
// =========================================================================
|
||||
|
||||
/// Convert FavoriteModel to Favorite entity
|
||||
Favorite toEntity() {
|
||||
return Favorite(
|
||||
favoriteId: favoriteId,
|
||||
productId: productId,
|
||||
userId: userId,
|
||||
createdAt: createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create FavoriteModel from Favorite entity
|
||||
factory FavoriteModel.fromEntity(Favorite favorite) {
|
||||
return FavoriteModel(
|
||||
favoriteId: favorite.favoriteId,
|
||||
productId: favorite.productId,
|
||||
userId: favorite.userId,
|
||||
createdAt: favorite.createdAt,
|
||||
);
|
||||
}
|
||||
}
|
||||
50
lib/features/favorites/data/models/favorite_model.g.dart
Normal file
50
lib/features/favorites/data/models/favorite_model.g.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'favorite_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class FavoriteModelAdapter extends TypeAdapter<FavoriteModel> {
|
||||
@override
|
||||
final typeId = 28;
|
||||
|
||||
@override
|
||||
FavoriteModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return FavoriteModel(
|
||||
favoriteId: fields[0] as String,
|
||||
productId: fields[1] as String,
|
||||
userId: fields[2] as String,
|
||||
createdAt: fields[3] as DateTime,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, FavoriteModel obj) {
|
||||
writer
|
||||
..writeByte(4)
|
||||
..writeByte(0)
|
||||
..write(obj.favoriteId)
|
||||
..writeByte(1)
|
||||
..write(obj.productId)
|
||||
..writeByte(2)
|
||||
..write(obj.userId)
|
||||
..writeByte(3)
|
||||
..write(obj.createdAt);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is FavoriteModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
Reference in New Issue
Block a user