rename to one trip
This commit is contained in:
72
one_trip/lib/api/models/homegroup.dart
Normal file
72
one_trip/lib/api/models/homegroup.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
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 Homegroup {
|
||||
int id;
|
||||
String name;
|
||||
List<int> recipes;
|
||||
List<int> users;
|
||||
List<int> inviteIDs;
|
||||
|
||||
Homegroup({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.recipes,
|
||||
required this.users,
|
||||
required this.inviteIDs,
|
||||
});
|
||||
|
||||
factory Homegroup.fromJson(Map<String, dynamic> json) {
|
||||
List<int> recipes =
|
||||
(json["recipes"] as List<dynamic>).map(((e) => e as int)).toList();
|
||||
List<int> users =
|
||||
(json["users"] as List<dynamic>).map(((e) => e as int)).toList();
|
||||
List<int> inviteIDs =
|
||||
(json["invites"] as List<dynamic>).map(((e) => e as int)).toList();
|
||||
|
||||
return Homegroup(
|
||||
id: json["id"] as int,
|
||||
name: json["name"] as String,
|
||||
recipes: recipes,
|
||||
users: users,
|
||||
inviteIDs: inviteIDs,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<Homegroup?> get(int id) async {
|
||||
String requestURL = "$baseURL/api/homegroups/$id/";
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.get(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
Homegroup hg = Homegroup.fromJson(jsonDecode(response.body));
|
||||
return hg;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Homegroup?> create(String title) async {
|
||||
String requestURL = "$baseURL/api/homegroups/";
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.post(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
body: {"name": title},
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
Homegroup hg = Homegroup.fromJson(jsonDecode(response.body));
|
||||
return hg;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
70
one_trip/lib/api/models/homegroupinvite.dart
Normal file
70
one_trip/lib/api/models/homegroupinvite.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
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 HomegroupInvite {
|
||||
int id;
|
||||
int homegroupID;
|
||||
int userID;
|
||||
|
||||
HomegroupInvite({
|
||||
required this.id,
|
||||
required this.homegroupID,
|
||||
required this.userID,
|
||||
});
|
||||
|
||||
factory HomegroupInvite.fromJson(Map<String, dynamic> json) {
|
||||
return HomegroupInvite(
|
||||
id: json["id"] as int,
|
||||
homegroupID: json["homegroup"] as int,
|
||||
userID: json["user"] as int,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<HomegroupInvite?> get(int id) async {
|
||||
String requestURL = "$baseURL/api/groupinvites/$id/";
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.get(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return HomegroupInvite.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<HomegroupInvite?> create(int homegroupID, int userID) async {
|
||||
String requestURL = "$baseURL/api/groupinvites/";
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.post(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
body: {"homegroup": "$homegroupID", "user": "$userID"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
return HomegroupInvite.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> delete() async {
|
||||
String requestURL = "$baseURL/api/groupinvites/$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;
|
||||
}
|
||||
}
|
||||
28
one_trip/lib/api/models/ingredient.dart
Normal file
28
one_trip/lib/api/models/ingredient.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
81
one_trip/lib/api/models/recipe.dart
Normal file
81
one_trip/lib/api/models/recipe.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
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:http/http.dart' as http;
|
||||
|
||||
class Recipe {
|
||||
int id;
|
||||
int homegroup;
|
||||
String name;
|
||||
List<Ingredient> ingredients;
|
||||
|
||||
Recipe(
|
||||
{required this.id,
|
||||
required this.name,
|
||||
required this.ingredients,
|
||||
required this.homegroup});
|
||||
|
||||
factory Recipe.fromJson(Map<String, dynamic> json) {
|
||||
List<Ingredient> ingredients = [];
|
||||
for (dynamic ingredient in json["ingredients"]) {
|
||||
ingredients.add(Ingredient.fromJson(ingredient));
|
||||
}
|
||||
return Recipe(
|
||||
id: json["id"] as int,
|
||||
homegroup: json["homegroup"] as int,
|
||||
name: json["name"] as String,
|
||||
ingredients: ingredients);
|
||||
}
|
||||
|
||||
static Future<Recipe?> get(int id) async {
|
||||
String requestURL = "$baseURL/api/recipes/$id/";
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.get(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return Recipe.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<List<Recipe>> getList(int groupID) async {
|
||||
Homegroup? group = await Homegroup.get(groupID);
|
||||
if (group == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
List<Recipe> recipes = [];
|
||||
for (int recipeID in group.recipes) {
|
||||
Recipe? recipe = await Recipe.get(recipeID);
|
||||
if (recipe != null) {
|
||||
recipes.add(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
return recipes;
|
||||
}
|
||||
|
||||
static Future<Recipe?> create(String name, int group) async {
|
||||
String requestURL = "$baseURL/api/recipes/";
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.post(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
body: {"homegroup": "$group", "name": name},
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
return Recipe.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
89
one_trip/lib/api/models/simpleuser.dart
Normal file
89
one_trip/lib/api/models/simpleuser.dart
Normal file
@@ -0,0 +1,89 @@
|
||||
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 SearchResult {
|
||||
List<SimpleUser> users;
|
||||
String? next;
|
||||
|
||||
SearchResult({required this.users, required this.next});
|
||||
}
|
||||
|
||||
class SimpleUser {
|
||||
int id;
|
||||
String username;
|
||||
String firstName;
|
||||
String lastName;
|
||||
String? imageUrl;
|
||||
int? invite;
|
||||
|
||||
SimpleUser({
|
||||
required this.id,
|
||||
required this.username,
|
||||
required this.firstName,
|
||||
required this.lastName,
|
||||
this.imageUrl,
|
||||
});
|
||||
|
||||
factory SimpleUser.fromJson(Map<String, dynamic> json) {
|
||||
return SimpleUser(
|
||||
id: json["id"] as int,
|
||||
username: json["username"] as String,
|
||||
firstName: json["first_name"] as String,
|
||||
lastName: json["last_name"] as String,
|
||||
imageUrl: json["image"] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<SimpleUser?> get({int? id}) async {
|
||||
String requestURL = "$baseURL/auth/users/${id ?? 'me'}";
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.get(
|
||||
Uri.parse(requestURL),
|
||||
headers: {"Authorization": "Token $token"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
SimpleUser u = SimpleUser.fromJson(jsonDecode(response.body));
|
||||
return u;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<SearchResult> search(String query, int page) async {
|
||||
// String requestURL = "";
|
||||
// if (url != null) {
|
||||
// requestURL = url;
|
||||
// } else if (query != null) {
|
||||
// requestURL = "$baseURL/auth/users/?search=$query";
|
||||
// } else {
|
||||
// return SearchResult(users: [], next: null);
|
||||
// }
|
||||
|
||||
String requestURL = "$baseURL/auth/users/?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<SimpleUser> users = [];
|
||||
for (var userObject in json["results"]) {
|
||||
SimpleUser u = SimpleUser.fromJson(userObject);
|
||||
users.add(u);
|
||||
}
|
||||
|
||||
return SearchResult(users: users, next: json["next"] as String?);
|
||||
}
|
||||
|
||||
return SearchResult(users: [], next: null);
|
||||
}
|
||||
}
|
||||
115
one_trip/lib/api/models/user.dart
Normal file
115
one_trip/lib/api/models/user.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:http/http.dart' as http;
|
||||
import 'dart:io' show File;
|
||||
|
||||
class User {
|
||||
int id;
|
||||
String username;
|
||||
String firstName;
|
||||
String lastName;
|
||||
List<int> homegroupInvites;
|
||||
int? homegroup;
|
||||
String? imageUrl;
|
||||
|
||||
User({
|
||||
required this.id,
|
||||
required this.username,
|
||||
required this.firstName,
|
||||
required this.lastName,
|
||||
required this.homegroupInvites,
|
||||
this.homegroup,
|
||||
this.imageUrl,
|
||||
});
|
||||
|
||||
factory User.fromJson(Map<String, dynamic> json) {
|
||||
List<dynamic> invitesDynamic = json["homegroup_invites"];
|
||||
List<int> invites = invitesDynamic.map((e) => e as int).toList();
|
||||
|
||||
return User(
|
||||
id: json["id"] as int,
|
||||
username: json["username"] as String,
|
||||
firstName: json["first_name"] as String,
|
||||
lastName: json["last_name"] as String,
|
||||
homegroup: json["homegroup"] as int?,
|
||||
imageUrl: json["image"] as String?,
|
||||
homegroupInvites: invites,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<User?> getMe() async {
|
||||
String requestURL = "$baseURL/auth/users/me";
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response =
|
||||
await http.get(Uri.parse(requestURL), headers: {
|
||||
"Authorization": "Token $token",
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
User u = User.fromJson(jsonDecode(response.body));
|
||||
return u;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<User?> patchMe({
|
||||
String? firstName,
|
||||
String? lastName,
|
||||
int? homegroup,
|
||||
}) async {
|
||||
String requestURL = "$baseURL/auth/users/me";
|
||||
|
||||
Map<String, String> body = {};
|
||||
if (firstName != null) {
|
||||
body["first_name"] = firstName;
|
||||
}
|
||||
|
||||
if (lastName != null) {
|
||||
body["last_name"] = lastName;
|
||||
}
|
||||
|
||||
if (homegroup != null) {
|
||||
body["homegroup"] = "$homegroup";
|
||||
}
|
||||
|
||||
String token = TokenSingleton().getToken();
|
||||
final http.Response response = await http.patch(
|
||||
Uri.parse(requestURL),
|
||||
headers: {
|
||||
"Authorization": "Token $token",
|
||||
},
|
||||
body: body,
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return User.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<User?> uploadImage(File file) async {
|
||||
String requestURL = "$baseURL/auth/users/me";
|
||||
String token = TokenSingleton().getToken();
|
||||
|
||||
http.MultipartRequest request =
|
||||
http.MultipartRequest("PATCH", Uri.parse(requestURL));
|
||||
|
||||
request.headers.addAll({"Authorization": "Token $token"});
|
||||
request.files.add(http.MultipartFile.fromBytes(
|
||||
"image", file.readAsBytesSync(),
|
||||
filename: file.path));
|
||||
|
||||
var multiresponse = await request.send();
|
||||
http.Response response = await http.Response.fromStream(multiresponse);
|
||||
if (response.statusCode == 200) {
|
||||
return User.fromJson(jsonDecode(response.body));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user