Commit 643bd5d5 authored by Chaitanya Pandit's avatar Chaitanya Pandit
Browse files

Merge branch '160-syncing-to-pod-getting-and-updating-edges' into 'dev'

Resolve "Syncing to POD | Getting and updating edges"

Closes #160

See merge request !47
parents 0e28b8de b021ba06
Pipeline #2825 passed with stages
in 2 minutes and 58 seconds
Showing with 71 additions and 19 deletions
+71 -19
......@@ -18,7 +18,7 @@ class PodAPIConnectionDetails {
{this.scheme = "http",
this.host = "localhost",
this.port = 3030,
this.apiVersion = "v3",
this.apiVersion = "v4",
this.ownerKey = "ownerKeyHere",
this.databaseKey = "databaseKeyHere"});
}
......@@ -39,6 +39,14 @@ class ItemEdgeRecord {
syncState = SyncStateExtension.rawValue(edge.syncState),
syncHasPriority = edge.syncHasPriority;
ItemEdgeRecord.fromSyncDict(Map<String, dynamic> dict)
: name = dict["name"],
selfUID = dict["self"],
sourceUID = dict["source"],
targetUID = dict["target"],
syncState = SyncState.noChanges,
syncHasPriority = false;
Future<EdgesCompanion> toCompanion(Database db) async {
if (selfRowID == null) {
Item self = (await db.itemRecordFetchWithUID(selfUID!))!;
......@@ -110,19 +118,27 @@ class ItemEdgeRecord {
}
Future<Map<String, dynamic>?> syncDict([DatabaseController? dbController]) async {
if (sourceRowID == null || targetRowID == null) {
if (sourceRowID == null || targetRowID == null || selfRowID == null) {
return null;
}
var sourceItem = await ItemRecord.fetchWithRowID(sourceRowID!, dbController);
var targetItem = await ItemRecord.fetchWithRowID(targetRowID!, dbController);
var selfItem = await ItemRecord.fetchWithRowID(selfRowID!, dbController);
if (sourceItem == null ||
targetItem == null ||
selfItem == null ||
sourceItem.syncState == SyncState.create ||
targetItem.syncState == SyncState.create) {
targetItem.syncState == SyncState.create ||
selfItem.syncState == SyncState.create) {
return null;
}
return {"_source": sourceItem.uid, "_target": targetItem.uid, "_name": name};
return {
"_source": sourceItem.uid,
"_target": targetItem.uid,
"_self": selfItem.uid,
"_name": name
};
}
static didSyncEdges(PodAPIPayloadBulkAction syncItems, String? error,
......@@ -146,11 +162,21 @@ class ItemEdgeRecord {
edgeRecord.syncState = SyncState.noChanges;
await edgeRecord.save(dbController.databasePool);
} else {
print("ERROR: Count not locate edge for synced edge: $syncedEdge");
print("ERROR: Could not locate edge for synced edge: $syncedEdge");
}
} else {
print("ERROR: Count not locate edge for synced edge: $syncedEdge");
print("ERROR: Could not locate edge for synced edge: $syncedEdge");
}
}
}
static Future<ItemRecord?> fromSyncEdgeDict(
{required Map<String, dynamic> dict, required DatabaseController dbController}) async {
if (dict["source"] != null && dict["target"] != null && dict["name"] != null) {
ItemEdgeRecord newEdge = ItemEdgeRecord.fromSyncDict(dict);
await newEdge.save(dbController.databasePool);
} else {
print("ERROR: Source, target or name is missing for edge");
}
}
}
......@@ -298,6 +298,7 @@ class ItemRecord with EquatableMixin {
}
factory ItemRecord.fromJson(Map<String, dynamic> json) => _$ItemRecordFromJson(json);
Map<String, dynamic> toJson() => _$ItemRecordToJson(this);
@override
......@@ -396,16 +397,41 @@ class ItemRecord with EquatableMixin {
ItemRecord newItem = ItemRecord.fromSyncDict(dict);
newItem.rowId = item?.rowId;
await newItem.save(dbController.databasePool);
if (dict["_item"] == null) {
await Future.forEach(dict.entries, (MapEntry entry) async {
var expectedType = dbController.schema.types[newItem.type]?.propertyTypes[entry.key];
if (expectedType == null) {
return;
}
var databaseValue = PropertyDatabaseValue.create(entry.value, expectedType.valueType);
await newItem.setPropertyValue(entry.key, databaseValue,
db: dbController, state: SyncState.noChanges);
});
}
await Future.forEach(dict.entries, (MapEntry entry) async {
var expectedType = dbController.schema.types[newItem.type]?.propertyTypes[entry.key];
if (expectedType == null) {
return;
}
var databaseValue = PropertyDatabaseValue.create(entry.value, expectedType.valueType);
await newItem.setPropertyValue(entry.key, databaseValue,
db: dbController, state: SyncState.noChanges);
});
var edges = dict["[[edges]]"];
if (edges is List && edges.isNotEmpty) {
await Future.forEach(edges, (edge) async {
if (edge is! Map<String, dynamic>) {
return;
}
var edgeDict = {
"self": edge["id"],
"source": dict["id"],
"name": edge["_edge"],
"target": edge["_item"]["id"]
};
ItemRecord? item = await ItemRecord.fetchWithUID(edge["_item"]["id"], dbController);
if (item == null) {
await ItemRecord.fromSyncItemDict(dict: edge["_item"], dbController: dbController);
}
ItemRecord? self = await ItemRecord.fetchWithUID(edge["id"], dbController);
if (self == null) {
await ItemRecord.fromSyncItemDict(dict: edge, dbController: dbController);
}
await ItemEdgeRecord.fromSyncEdgeDict(dict: edgeDict, dbController: dbController);
});
}
return newItem;
}
......
......@@ -304,8 +304,8 @@ class SyncController {
if (currentConnection == null) {
throw Exception("No pod connection config");
}
var request =
PodAPIStandardRequest.searchAction({"dateServerModified>=": dateServerModifiedTimestamp});
var request = PodAPIStandardRequest.searchAction(
{"dateServerModified>=": dateServerModifiedTimestamp, "[[edges]]": {}});
var networkCall = await request.execute(currentConnection!);
var error;
......
......@@ -13,8 +13,7 @@ import 'package:memri/MemriApp/Controllers/Syncing/SyncController.dart';
import 'package:uuid/uuid.dart';
/// This connection config is used to connect to the pod for the tests. You can change url scheme/path/port etc here
var connectionConfig =
PodAPIConnectionDetails(ownerKey: Uuid().v4(), databaseKey: "DBKEY", host: "192.168.88.17");
var connectionConfig = PodAPIConnectionDetails(ownerKey: Uuid().v4(), host: "192.168.88.17");
/// These are used to create test items in the pod for use in later tests
var noteRowId = 100;
......@@ -96,5 +95,6 @@ void main() {
test('testSyncing', () async {
syncController.currentConnection = connectionConfig;
await syncController.sync(connectionConfig: connectionConfig);
expect(syncController.lastError, equals(null));
});
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment