Browse Source

added update to usercontroller && added untested create / delete in userontroller

master
TheNils 3 months ago
parent
commit
7c55d1dd59
  1. 4
      lib/api/api.dart
  2. 0
      lib/store/schema.json
  3. 97
      lib/store/user.dart

4
lib/api/api.dart

@ -101,8 +101,8 @@ class Api {
return await handleResponse(res, path, body, tryCount, put);
}
Future<Map<dynamic, dynamic>> delete(String path, Map<dynamic, dynamic> body,
[int tryCount = 1]) async {
Future<Map<dynamic, dynamic>> delete(String path,
[Map<dynamic, dynamic> body = const {}, int tryCount = 1]) async {
await refreshJwt();
var res = client.delete(

0
lib/store/schema.json

97
lib/store/user.dart

@ -77,9 +77,96 @@ class UserController extends GetxController {
return user;
}
Future<bool> patch(Map<dynamic, dynamic> item, {String collection}) async {
print("userController patch");
Future<bool> delete(String id, String collection) async {
var res = await _api.delete("/items/$collection/$id");
if (!res.containsKey('error')) {
_deleteInCollection(collection, id);
return true;
} else {
return false;
}
}
void _deleteInCollection(String collection, String id,
[List<String> startPath]) {
var currentPath = startPath != null ? startPath : [];
var subSchema = schema;
currentPath.forEach((value) {
subSchema = subSchema[value];
});
List<String> reserved = ["collection", "kind"];
subSchema.forEach((key, value) {
if (!reserved.contains(key) && value is Map) {
_deleteInCollection(collection, id, [
...currentPath,
...[key]
]);
if (value.containsKey("collection")) {
if (value["collection"] == collection) {
updateDeeply(currentPath, _user().value, (value) {
if (value is List) {
var index =
value.indexWhere((element) => element[key]["id"] == id);
value.removeAt(index);
}
});
}
}
}
});
}
Future<bool> create(Map<dynamic, dynamic> item, String collection) async {
var res = await _api.post("/items/$collection", item);
if (!res.containsKey('error')) {
_createInCollection(collection, {
...item,
...{"id": res["data"]["id"]}
});
return true;
} else {
return false;
}
}
void _createInCollection(String collection, Map<dynamic, dynamic> item,
[List<String> startPath]) {
var currentPath = startPath != null ? startPath : [];
var subSchema = schema;
currentPath.forEach((value) {
subSchema = subSchema[value];
});
List<String> reserved = ["collection", "kind"];
subSchema.forEach((key, value) {
if (!reserved.contains(key) && value is Map) {
_updateInCollection(collection, item, [
...currentPath,
...[key]
]);
if (value.containsKey("collection")) {
if (value["collection"] == collection) {
updateDeeply(currentPath, _user().value, (value) {
if (value is List) {
value.add({key: item});
} else {
value = [item];
}
});
}
}
}
});
}
Future<bool> patch(Map<dynamic, dynamic> item, {String collection}) async {
var id = item.containsKey("id") ? item["id"] : null;
item.remove("id");
var computedPath =
@ -87,7 +174,7 @@ class UserController extends GetxController {
var res = await _api.patch(computedPath, item);
if (!res.containsKey('error')) {
_updateCollection(collection, {
_updateInCollection(collection, {
...item,
...{"id": id}
});
@ -97,7 +184,7 @@ class UserController extends GetxController {
}
}
void _updateCollection(String collection, Map<dynamic, dynamic> item,
void _updateInCollection(String collection, Map<dynamic, dynamic> item,
[List<String> startPath]) {
var currentPath = startPath != null ? startPath : [];
var subSchema = schema;
@ -110,7 +197,7 @@ class UserController extends GetxController {
subSchema.forEach((key, value) {
if (!reserved.contains(key) && value is Map) {
_updateCollection(collection, item, [
_updateInCollection(collection, item, [
...currentPath,
...[key]
]);

Loading…
Cancel
Save