recipe-manager/mobile/lib/core/models/recipe.dart

117 lines
3.3 KiB
Dart

class Recipe {
final String? id;
final String title;
final String? description;
final String? servings;
final String? prepTime;
final String? cookTime;
final String? totalTime;
final String? image;
final List<String> images;
final List<String> ingredients;
final List<String> instructions;
final String? sourceUrl;
final String? shareToken;
final String? createdAt;
final String? updatedAt;
Recipe({
this.id,
required this.title,
this.description,
this.servings,
this.prepTime,
this.cookTime,
this.totalTime,
this.image,
List<String>? images,
List<String>? ingredients,
List<String>? instructions,
this.sourceUrl,
this.shareToken,
this.createdAt,
this.updatedAt,
}) : images = images ?? [],
ingredients = ingredients ?? [],
instructions = instructions ?? [];
factory Recipe.fromJson(Map<String, dynamic> json) {
List<String> parseStringList(dynamic val) {
if (val is List) {
return val.map((e) => e.toString().trim()).where((s) => s.isNotEmpty).toList();
}
return [];
}
return Recipe(
id: json['id']?.toString(),
title: json['title']?.toString() ?? 'Untitled Recipe',
description: json['description']?.toString(),
servings: json['servings']?.toString(),
prepTime: json['prepTime']?.toString(),
cookTime: json['cookTime']?.toString(),
totalTime: json['totalTime']?.toString(),
image: json['image']?.toString(),
images: parseStringList(json['images']),
ingredients: parseStringList(json['ingredients']),
instructions: parseStringList(json['instructions']),
sourceUrl: json['sourceUrl']?.toString(),
shareToken: json['shareToken']?.toString(),
createdAt: json['createdAt']?.toString(),
updatedAt: json['updatedAt']?.toString(),
);
}
Map<String, dynamic> toJson() {
return {
if (id != null) 'id': id,
'title': title,
'description': description ?? '',
'servings': servings ?? '',
'prepTime': prepTime ?? '',
'cookTime': cookTime ?? '',
'totalTime': totalTime ?? '',
'image': image ?? '',
'images': images,
'ingredients': ingredients,
'instructions': instructions,
'sourceUrl': sourceUrl ?? '',
};
}
Recipe copyWith({
String? id,
String? title,
String? description,
String? servings,
String? prepTime,
String? cookTime,
String? totalTime,
String? image,
List<String>? images,
List<String>? ingredients,
List<String>? instructions,
String? sourceUrl,
String? shareToken,
String? createdAt,
String? updatedAt,
}) {
return Recipe(
id: id ?? this.id,
title: title ?? this.title,
description: description ?? this.description,
servings: servings ?? this.servings,
prepTime: prepTime ?? this.prepTime,
cookTime: cookTime ?? this.cookTime,
totalTime: totalTime ?? this.totalTime,
image: image ?? this.image,
images: images ?? this.images,
ingredients: ingredients ?? this.ingredients,
instructions: instructions ?? this.instructions,
sourceUrl: sourceUrl ?? this.sourceUrl,
shareToken: shareToken ?? this.shareToken,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
);
}
}