72 lines
1.8 KiB
Dart
72 lines
1.8 KiB
Dart
import 'package:appwrite/appwrite.dart';
|
|
import 'package:appwrite/models.dart';
|
|
import 'package:ltx_flutter/constants/constants.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:ltx_flutter/appwrite/appwrite.dart';
|
|
|
|
class CategoriesAPI extends ChangeNotifier {
|
|
Client client = Client();
|
|
late final Account account;
|
|
late final Databases databases;
|
|
late final String userId;
|
|
final AuthAPI auth = AuthAPI();
|
|
|
|
// Constructor
|
|
CategoriesAPI() {
|
|
init();
|
|
}
|
|
|
|
// loadUser() async {
|
|
// try {
|
|
// user = await account.get();
|
|
// notifyListeners();
|
|
// } catch (e) {
|
|
// print(e);
|
|
// notifyListeners();
|
|
// }
|
|
// }
|
|
|
|
init() {
|
|
client.setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT_ID);
|
|
account = Account(client);
|
|
databases = Databases(client);
|
|
}
|
|
|
|
Future<DocumentList> getCategories() {
|
|
print("Getting categories");
|
|
return databases.listDocuments(
|
|
databaseId: CATEGORIES_DB,
|
|
collectionId: COLLECTION,
|
|
queries: [
|
|
Query.orderAsc("number"),
|
|
]);
|
|
}
|
|
|
|
Future<Document> addCategory({
|
|
required String name,
|
|
required double number,
|
|
required String color,
|
|
String? description,
|
|
int? parentId,
|
|
}) {
|
|
return databases.createDocument(
|
|
databaseId: CATEGORIES_DB,
|
|
collectionId: COLLECTION,
|
|
documentId: "category-${number.toString()}",
|
|
data: {
|
|
'name': name,
|
|
'number': number,
|
|
'color': color,
|
|
'parent': parentId,
|
|
'description': description,
|
|
});
|
|
}
|
|
|
|
Future<dynamic> deleteCategory({required double number}) {
|
|
return databases.deleteDocument(
|
|
databaseId: CATEGORIES_DB,
|
|
collectionId: COLLECTION,
|
|
documentId: "category-${number.toString()}");
|
|
}
|
|
}
|