Complete recipes page
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// const String baseURL = "https://groceries.alaevens.ca";
|
||||
const String baseURL = "http://192.168.0.16:8000";
|
||||
|
||||
const int resultsPerPage = 4;
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import 'package:one_trip/api/auth.dart';
|
||||
import 'package:one_trip/api/consts.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class Ingredient {
|
||||
int id;
|
||||
String name;
|
||||
bool inStock;
|
||||
int contentType;
|
||||
int objectID;
|
||||
|
||||
Ingredient({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.inStock,
|
||||
required this.contentType,
|
||||
required this.objectID,
|
||||
});
|
||||
|
||||
factory Ingredient.fromJson(Map<String, dynamic> json) {
|
||||
return Ingredient(
|
||||
id: json["id"] as int,
|
||||
name: json["name"] as String,
|
||||
inStock: json["in_stock"] as bool,
|
||||
contentType: json["content_type"] as int,
|
||||
objectID: json["object_id"] as int);
|
||||
}
|
||||
}
|
||||
74
one_trip/lib/api/models/listingredient.dart
Normal file
74
one_trip/lib/api/models/listingredient.dart
Normal file
@@ -0,0 +1,74 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:one_trip/api/auth.dart';
|
||||
import 'package:one_trip/api/consts.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class RecipeIngredient {
|
||||
int id;
|
||||
String name;
|
||||
int list;
|
||||
bool inCart;
|
||||
|
||||
RecipeIngredient({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.list,
|
||||
required this.inCart,
|
||||
});
|
||||
|
||||
factory RecipeIngredient.fromJson(Map<String, dynamic> json) {
|
||||
return RecipeIngredient(
|
||||
id: json["id"] as int,
|
||||
name: json["name"] as String,
|
||||
list: json["list"] as int,
|
||||
inCart: json["in_cart"] as bool,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<RecipeIngredient?> create(String name, int recipeID) async {
|
||||
const String requestURL = "$baseURL/api/listingredients/";
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.post(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
body: {
|
||||
"name": name,
|
||||
"recipe": "$recipeID",
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
return RecipeIngredient.fromJson(jsonDecode(response.body));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<RecipeIngredient?> patch(String name) async {
|
||||
String requestURL = "$baseURL/api/listingredients/$id/";
|
||||
String token = TokenSingleton().getToken();
|
||||
|
||||
http.Response response = await http.patch(Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"}, body: {"name": name});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return RecipeIngredient.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> delete() async {
|
||||
String requestURL = "$baseURL/api/listingredients/$id/";
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.delete(Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"});
|
||||
|
||||
if (response.statusCode == 204) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
71
one_trip/lib/api/models/recipeingredient.dart
Normal file
71
one_trip/lib/api/models/recipeingredient.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:one_trip/api/auth.dart';
|
||||
import 'package:one_trip/api/consts.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class RecipeIngredient {
|
||||
int id;
|
||||
String name;
|
||||
int recipe;
|
||||
|
||||
RecipeIngredient({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.recipe,
|
||||
});
|
||||
|
||||
factory RecipeIngredient.fromJson(Map<String, dynamic> json) {
|
||||
return RecipeIngredient(
|
||||
id: json["id"] as int,
|
||||
name: json["name"] as String,
|
||||
recipe: json["recipe"] as int,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<RecipeIngredient?> create(String name, int recipeID) async {
|
||||
const String requestURL = "$baseURL/api/recipeingredients/";
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.post(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
body: {
|
||||
"name": name,
|
||||
"recipe": "$recipeID",
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
return RecipeIngredient.fromJson(jsonDecode(response.body));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<RecipeIngredient?> patch(String name) async {
|
||||
String requestURL = "$baseURL/api/recipeingredients/$id/";
|
||||
String token = TokenSingleton().getToken();
|
||||
|
||||
http.Response response = await http.patch(Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"}, body: {"name": name});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return RecipeIngredient.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> delete() async {
|
||||
String requestURL = "$baseURL/api/recipeingredients/$id/";
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.delete(Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"});
|
||||
|
||||
if (response.statusCode == 204) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user