add websock functionality
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const String baseURL = "https://groceries.alaevens.ca";
|
||||
// const String baseURL = "http://192.168.0.16:8000";
|
||||
// const String baseURL = "https://groceries.alaevens.ca";
|
||||
const String baseURL = "http://192.168.0.16:8000";
|
||||
const String baseWsURL = "ws://192.168.0.16:8000";
|
||||
|
||||
const int resultsPerPage = 4;
|
||||
|
||||
115
one_trip/lib/api/models/list.dart
Normal file
115
one_trip/lib/api/models/list.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:one_trip/api/auth.dart';
|
||||
import 'package:one_trip/api/consts.dart';
|
||||
import 'package:one_trip/api/models/listingredient.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:one_trip/api/models/recipe.dart';
|
||||
import 'package:one_trip/api/models/recipeingredient.dart';
|
||||
|
||||
class ShoppingList {
|
||||
List<ListIngredient> ingredients;
|
||||
int updates;
|
||||
int homegroup;
|
||||
|
||||
ShoppingList({
|
||||
required this.ingredients,
|
||||
required this.updates,
|
||||
required this.homegroup,
|
||||
});
|
||||
|
||||
factory ShoppingList.fromJson(Map<String, dynamic> json) {
|
||||
List<ListIngredient> ingredients = [];
|
||||
for (dynamic ingredient in json["ingredients"]) {
|
||||
ingredients.add(ListIngredient.fromJson(ingredient));
|
||||
}
|
||||
|
||||
return ShoppingList(
|
||||
ingredients: ingredients,
|
||||
updates: json["updates"] as int,
|
||||
homegroup: json["homegroup"] as int);
|
||||
}
|
||||
|
||||
static Future<ShoppingList?> get(int id) async {
|
||||
String requestURL = "$baseURL/api/lists/$id/";
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.get(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return ShoppingList.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<ShoppingList?> patch({int? updates}) async {
|
||||
Map<String, String> body = {};
|
||||
if (updates != null) {
|
||||
body["updates"] = "$updates";
|
||||
}
|
||||
|
||||
String requestURL = "$baseURL/api/lists/$homegroup/";
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.patch(Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"}, body: body);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return ShoppingList.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<ShoppingList?> addRecipe(int recipeID) async {
|
||||
Recipe? recipe = await Recipe.get(recipeID);
|
||||
|
||||
if (recipe == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
bool anySuccesses = false;
|
||||
for (RecipeIngredient ingredient in recipe.ingredients) {
|
||||
ListIngredient? newIngredient =
|
||||
await ListIngredient.create(ingredient.name, homegroup);
|
||||
|
||||
if (newIngredient != null) {
|
||||
anySuccesses = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (anySuccesses) {
|
||||
return patch(updates: updates + 1);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<ShoppingList?> clear() async {
|
||||
bool anySuccess = false;
|
||||
for (ListIngredient ingredient in ingredients) {
|
||||
bool success = await ingredient.delete();
|
||||
|
||||
if (success) {
|
||||
anySuccess = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (anySuccess) {
|
||||
return patch(updates: updates + 1);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is ShoppingList &&
|
||||
other.homegroup == homegroup &&
|
||||
other.ingredients == ingredients;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hashAll(ingredients);
|
||||
}
|
||||
@@ -4,21 +4,21 @@ import 'package:one_trip/api/auth.dart';
|
||||
import 'package:one_trip/api/consts.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class RecipeIngredient {
|
||||
class ListIngredient {
|
||||
int id;
|
||||
String name;
|
||||
int list;
|
||||
bool inCart;
|
||||
|
||||
RecipeIngredient({
|
||||
ListIngredient({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.list,
|
||||
required this.inCart,
|
||||
});
|
||||
|
||||
factory RecipeIngredient.fromJson(Map<String, dynamic> json) {
|
||||
return RecipeIngredient(
|
||||
factory ListIngredient.fromJson(Map<String, dynamic> json) {
|
||||
return ListIngredient(
|
||||
id: json["id"] as int,
|
||||
name: json["name"] as String,
|
||||
list: json["list"] as int,
|
||||
@@ -26,7 +26,7 @@ class RecipeIngredient {
|
||||
);
|
||||
}
|
||||
|
||||
static Future<RecipeIngredient?> create(String name, int recipeID) async {
|
||||
static Future<ListIngredient?> create(String name, int list) async {
|
||||
const String requestURL = "$baseURL/api/listingredients/";
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.post(
|
||||
@@ -34,26 +34,35 @@ class RecipeIngredient {
|
||||
headers: {"Authorization": "Token $token"},
|
||||
body: {
|
||||
"name": name,
|
||||
"recipe": "$recipeID",
|
||||
"list": "$list",
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
return RecipeIngredient.fromJson(jsonDecode(response.body));
|
||||
return ListIngredient.fromJson(jsonDecode(response.body));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<RecipeIngredient?> patch(String name) async {
|
||||
Future<ListIngredient?> patch({String? name, bool? inCart}) async {
|
||||
String requestURL = "$baseURL/api/listingredients/$id/";
|
||||
String token = TokenSingleton().getToken();
|
||||
|
||||
Map<String, String> body = {};
|
||||
if (name != null) {
|
||||
body["name"] = name;
|
||||
}
|
||||
|
||||
if (inCart != null) {
|
||||
body["in_cart"] = "$inCart";
|
||||
}
|
||||
|
||||
http.Response response = await http.patch(Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"}, body: {"name": name});
|
||||
headers: {"Authorization": "Token $token"}, body: body);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return RecipeIngredient.fromJson(jsonDecode(response.body));
|
||||
return ListIngredient.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -71,4 +80,14 @@ class RecipeIngredient {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is ListIngredient &&
|
||||
other.id == id &&
|
||||
other.name == name &&
|
||||
other.inCart == inCart;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(id, name, inCart);
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ 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/recipeingredient.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:one_trip/api/searchresult.dart';
|
||||
|
||||
class Recipe {
|
||||
int id;
|
||||
@@ -46,26 +46,51 @@ class Recipe {
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<List<Recipe>> getList(int groupID) async {
|
||||
Homegroup? group = await Homegroup.get(groupID);
|
||||
if (group == null) {
|
||||
return [];
|
||||
}
|
||||
static Future<List<Recipe>> getList() async {
|
||||
const String requestURL = "$baseURL/api/recipes/";
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
http.Response response = await http.get(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
);
|
||||
|
||||
List<Recipe> recipes = [];
|
||||
for (int recipeID in group.recipes) {
|
||||
Recipe? recipe = await Recipe.get(recipeID);
|
||||
if (recipe != null) {
|
||||
// TODO: implement sorted insert
|
||||
recipes.add(recipe);
|
||||
if (response.statusCode == 200) {
|
||||
var body = jsonDecode(response.body);
|
||||
for (var recipe in body) {
|
||||
recipes.add(Recipe.fromJson(recipe));
|
||||
}
|
||||
}
|
||||
|
||||
recipes.sort(((a, b) => a.name.compareTo(b.name)));
|
||||
|
||||
return recipes;
|
||||
}
|
||||
|
||||
static Future<SearchResult<Recipe>> search(String query, int page) async {
|
||||
String requestURL = "$baseURL/api/searchrecipes/?page=$page&search=$query";
|
||||
requestURL = requestURL.replaceAll(RegExp(r"\s+"), "+");
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.get(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
Map<String, dynamic> json = jsonDecode(response.body);
|
||||
List<Recipe> recipes = [];
|
||||
for (var recipeObject in json["results"]) {
|
||||
Recipe r = Recipe.fromJson(recipeObject);
|
||||
recipes.add(r);
|
||||
}
|
||||
|
||||
return SearchResult<Recipe>(
|
||||
results: recipes, next: json["next"] as String?);
|
||||
}
|
||||
|
||||
return SearchResult<Recipe>(results: [], next: null);
|
||||
}
|
||||
|
||||
static Future<Recipe?> create(String name, int group) async {
|
||||
String requestURL = "$baseURL/api/recipes/";
|
||||
String token = TokenSingleton().getToken();
|
||||
|
||||
@@ -3,13 +3,14 @@ import 'dart:convert';
|
||||
import 'package:one_trip/api/auth.dart';
|
||||
import 'package:one_trip/api/consts.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:one_trip/api/searchresult.dart';
|
||||
|
||||
class SearchResult {
|
||||
List<SimpleUser> users;
|
||||
String? next;
|
||||
// class SearchResult {
|
||||
// List<SimpleUser> users;
|
||||
// String? next;
|
||||
|
||||
SearchResult({required this.users, required this.next});
|
||||
}
|
||||
// SearchResult({required this.users, required this.next});
|
||||
// }
|
||||
|
||||
class SimpleUser {
|
||||
int id;
|
||||
@@ -38,7 +39,8 @@ class SimpleUser {
|
||||
}
|
||||
|
||||
static Future<SimpleUser?> get({int? id}) async {
|
||||
String requestURL = "$baseURL/auth/users/${id ?? 'me'}";
|
||||
String requestURL =
|
||||
id == null ? "$baseURL/auth/users/me" : "$baseURL/auth/users/$id/";
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.get(
|
||||
@@ -54,7 +56,7 @@ class SimpleUser {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<SearchResult> search(String query, int page) async {
|
||||
static Future<SearchResult<SimpleUser>> search(String query, int page) async {
|
||||
// String requestURL = "";
|
||||
// if (url != null) {
|
||||
// requestURL = url;
|
||||
@@ -81,9 +83,10 @@ class SimpleUser {
|
||||
users.add(u);
|
||||
}
|
||||
|
||||
return SearchResult(users: users, next: json["next"] as String?);
|
||||
return SearchResult<SimpleUser>(
|
||||
results: users, next: json["next"] as String?);
|
||||
}
|
||||
|
||||
return SearchResult(users: [], next: null);
|
||||
return SearchResult<SimpleUser>(results: [], next: null);
|
||||
}
|
||||
}
|
||||
|
||||
6
one_trip/lib/api/searchresult.dart
Normal file
6
one_trip/lib/api/searchresult.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
class SearchResult<T> {
|
||||
List<T> results;
|
||||
String? next;
|
||||
|
||||
SearchResult({required this.results, required this.next});
|
||||
}
|
||||
Reference in New Issue
Block a user