Complete recipes page

This commit is contained in:
Alexander Laevens
2022-11-27 20:29:39 -07:00
parent 373eabe002
commit 9b1fc4e0e2
71 changed files with 806 additions and 251 deletions

View File

@@ -3,14 +3,14 @@ import 'dart:convert';
import 'package:one_trip/api/auth.dart';
import 'package:one_trip/api/consts.dart';
import 'package:one_trip/api/models/homegroup.dart';
import 'package:one_trip/api/models/ingredient.dart';
import 'package:one_trip/api/models/recipeingredient.dart';
import 'package:http/http.dart' as http;
class Recipe {
int id;
int homegroup;
String name;
List<Ingredient> ingredients;
List<RecipeIngredient> ingredients;
Recipe(
{required this.id,
@@ -19,9 +19,9 @@ class Recipe {
required this.homegroup});
factory Recipe.fromJson(Map<String, dynamic> json) {
List<Ingredient> ingredients = [];
List<RecipeIngredient> ingredients = [];
for (dynamic ingredient in json["ingredients"]) {
ingredients.add(Ingredient.fromJson(ingredient));
ingredients.add(RecipeIngredient.fromJson(ingredient));
}
return Recipe(
id: json["id"] as int,
@@ -56,10 +56,13 @@ class Recipe {
for (int recipeID in group.recipes) {
Recipe? recipe = await Recipe.get(recipeID);
if (recipe != null) {
// TODO: implement sorted insert
recipes.add(recipe);
}
}
recipes.sort(((a, b) => a.name.compareTo(b.name)));
return recipes;
}
@@ -78,4 +81,19 @@ class Recipe {
return null;
}
Future<bool> delete() async {
String requestURL = "$baseURL/api/recipes/$id/";
String token = TokenSingleton().getToken();
final http.Response response = await http.delete(
Uri.parse(requestURL),
headers: {"Authorization": "Token $token"},
);
if (response.statusCode == 204) {
return true;
}
return false;
}
}