|
| 1 | +class Plant { |
| 2 | + final int id; |
| 3 | + final int userId; |
| 4 | + final int speciesId; |
| 5 | + final String? nickname; |
| 6 | + final String? imageUrl; |
| 7 | + final int wateringFrequencyDays; |
| 8 | + final DateTime? lastWateredAt; |
| 9 | + final String? note; |
| 10 | + final DateTime createdAt; |
| 11 | + final DateTime updatedAt; |
| 12 | + |
| 13 | + Plant({ |
| 14 | + required this.id, |
| 15 | + required this.userId, |
| 16 | + required this.speciesId, |
| 17 | + this.nickname, |
| 18 | + this.imageUrl, |
| 19 | + required this.wateringFrequencyDays, |
| 20 | + this.lastWateredAt, |
| 21 | + this.note, |
| 22 | + required this.createdAt, |
| 23 | + required this.updatedAt, |
| 24 | + }); |
| 25 | + |
| 26 | + factory Plant.fromJson(Map<String, dynamic> json) { |
| 27 | + return Plant( |
| 28 | + id: json['id'] as int, |
| 29 | + userId: json['user_id'] as int, |
| 30 | + speciesId: json['species_id'] as int, |
| 31 | + nickname: json['nickname'] as String?, |
| 32 | + imageUrl: json['image_url'] as String?, |
| 33 | + wateringFrequencyDays: json['watering_frequency_days'] as int, |
| 34 | + lastWateredAt: json['last_watered_at'] != null |
| 35 | + ? DateTime.parse(json['last_watered_at'] as String) |
| 36 | + : null, |
| 37 | + note: json['note'] as String?, |
| 38 | + createdAt: DateTime.parse(json['created_at'] as String), |
| 39 | + updatedAt: DateTime.parse(json['updated_at'] as String), |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + Map<String, dynamic> toJson() { |
| 44 | + return { |
| 45 | + 'id': id, |
| 46 | + 'user_id': userId, |
| 47 | + 'species_id': speciesId, |
| 48 | + 'nickname': nickname, |
| 49 | + 'image_url': imageUrl, |
| 50 | + 'watering_frequency_days': wateringFrequencyDays, |
| 51 | + 'last_watered_at': lastWateredAt?.toIso8601String(), |
| 52 | + 'note': note, |
| 53 | + 'created_at': createdAt.toIso8601String(), |
| 54 | + 'updated_at': updatedAt.toIso8601String(), |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + Plant copyWith({ |
| 59 | + int? id, |
| 60 | + int? userId, |
| 61 | + int? speciesId, |
| 62 | + String? nickname, |
| 63 | + String? imageUrl, |
| 64 | + int? wateringFrequencyDays, |
| 65 | + DateTime? lastWateredAt, |
| 66 | + String? note, |
| 67 | + DateTime? createdAt, |
| 68 | + DateTime? updatedAt, |
| 69 | + }) { |
| 70 | + return Plant( |
| 71 | + id: id ?? this.id, |
| 72 | + userId: userId ?? this.userId, |
| 73 | + speciesId: speciesId ?? this.speciesId, |
| 74 | + nickname: nickname ?? this.nickname, |
| 75 | + imageUrl: imageUrl ?? this.imageUrl, |
| 76 | + wateringFrequencyDays: wateringFrequencyDays ?? this.wateringFrequencyDays, |
| 77 | + lastWateredAt: lastWateredAt ?? this.lastWateredAt, |
| 78 | + note: note ?? this.note, |
| 79 | + createdAt: createdAt ?? this.createdAt, |
| 80 | + updatedAt: updatedAt ?? this.updatedAt, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + String toString() { |
| 86 | + return 'Plant(id: $id, userId: $userId, speciesId: $speciesId, nickname: $nickname, imageUrl: $imageUrl, wateringFrequencyDays: $wateringFrequencyDays, lastWateredAt: $lastWateredAt, note: $note, createdAt: $createdAt, updatedAt: $updatedAt)'; |
| 87 | + } |
| 88 | + |
| 89 | + @override |
| 90 | + bool operator ==(Object other) { |
| 91 | + if (identical(this, other)) return true; |
| 92 | + |
| 93 | + return other is Plant && |
| 94 | + other.id == id && |
| 95 | + other.userId == userId && |
| 96 | + other.speciesId == speciesId && |
| 97 | + other.nickname == nickname && |
| 98 | + other.imageUrl == imageUrl && |
| 99 | + other.wateringFrequencyDays == wateringFrequencyDays && |
| 100 | + other.lastWateredAt == lastWateredAt && |
| 101 | + other.note == note && |
| 102 | + other.createdAt == createdAt && |
| 103 | + other.updatedAt == updatedAt; |
| 104 | + } |
| 105 | + |
| 106 | + @override |
| 107 | + int get hashCode { |
| 108 | + return id.hashCode ^ |
| 109 | + userId.hashCode ^ |
| 110 | + speciesId.hashCode ^ |
| 111 | + nickname.hashCode ^ |
| 112 | + imageUrl.hashCode ^ |
| 113 | + wateringFrequencyDays.hashCode ^ |
| 114 | + lastWateredAt.hashCode ^ |
| 115 | + note.hashCode ^ |
| 116 | + createdAt.hashCode ^ |
| 117 | + updatedAt.hashCode; |
| 118 | + } |
| 119 | +} |
0 commit comments