From d054ec5c10f95943daa951724461690b16a59b97 Mon Sep 17 00:00:00 2001 From: Vargha Csongor Date: Sat, 12 Apr 2025 11:32:15 +0200 Subject: [PATCH] implement family tree query --- api/openapi.json | 81 ++++++++++++++----- apps/db-adapter/internal/memgraph/queries.go | 14 ++++ .../queries/get_blood_relations_by_id.cypher | 19 ++++- .../queries/get_family_tree_by_id.cypher | 8 -- .../get_family_tree_with_spouses.cypher | 22 +++++ .../get_limited_blood_relations_by_id.cypher | 4 - 6 files changed, 115 insertions(+), 33 deletions(-) delete mode 100644 apps/db-adapter/internal/memgraph/queries/get_family_tree_by_id.cypher create mode 100644 apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher delete mode 100644 apps/db-adapter/internal/memgraph/queries/get_limited_blood_relations_by_id.cypher diff --git a/api/openapi.json b/api/openapi.json index 7e4d13d..294004c 100644 --- a/api/openapi.json +++ b/api/openapi.json @@ -749,6 +749,64 @@ } } }, + "/person/{id}/family-tree-with-spouses": { + "get": { + "summary": "Get family tree by person ID with spouses included", + "operationId": "getFamilyTreeById", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer" + } + } + ], + "responses": { + "200": { + "description": "Family tree retrieved", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/FamilyTree" + } + } + } + }, + "400": { + "description": "Bad request", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "msg": { + "type": "string" + } + } + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "msg": { + "type": "string" + } + } + } + } + } + } + } + } + }, "/person/{id}/recipes": { "get": { "summary": "Get recipes by person ID", @@ -2519,32 +2577,17 @@ "FamilyTree": { "type": "object", "properties": { - "ancestors": { + "people": { "type": "array", "items": { "$ref": "#/components/schemas/OptimizedPersonNode" } }, - "prel1": { - "$ref": "#/components/schemas/Relationship" - }, - "children": { + "relationships": { "type": "array", - "items": { - "$ref": "#/components/schemas/OptimizedPersonNode" + "items":{ + "$ref": "#/components/schemas/Relationship" } - }, - "prel2": { - "$ref": "#/components/schemas/Relationship" - }, - "spouses": { - "type": "string" - }, - "srel": { - "$ref": "#/components/schemas/Relationship" - }, - "user": { - "type": "string" } } }, diff --git a/apps/db-adapter/internal/memgraph/queries.go b/apps/db-adapter/internal/memgraph/queries.go index bbc00ae..2b6598f 100644 --- a/apps/db-adapter/internal/memgraph/queries.go +++ b/apps/db-adapter/internal/memgraph/queries.go @@ -125,3 +125,17 @@ var GetProfileAdminsCypherQuery string // //go:embed queries/get_managed_profiles.cypher var GetManagedProfilesCypherQuery string + +// Requires id parameter. +// +// returns people, relationships +// +//go:embed queries/get_blood_relations_by_id.cypher +var GetBloodRelativesCypherQuery string + +// Requires id parameter. +// +// returns people, relationships +// +//go:embed queries/get_family_tree_with_spouses.cypher +var GetFamilyTreeWithSpousesCypherQuery string diff --git a/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher b/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher index 6078627..e771f18 100644 --- a/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher +++ b/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher @@ -1,4 +1,19 @@ -MATCH (n:Person)-[p:Parent*1..]->(family:Person) +MATCH (n:Person) WHERE id(n) = $id +OPTIONAL MATCH (n)-[p:Parent*..]->(family:Person) OPTIONAL MATCH (family)-[c:Child*1..4]->(children:Person) -RETURN collect(family) + collect(children) + collect(n), collect(c) + collect(p) +OPTIONAL MATCH (family)-[s:Sibling]->(siblings:Person) +OPTIONAL MATCH (n)-[ds:Sibling]->(direct_siblings:Person) +WITH collections.to_set(collect(n)+collect(family)+collect(children)+collect(direct_siblings)) as people, +collections.to_set(collect(c) + collect(p) + collect(s) + collect(ds)) as relationships +UNWIND people as ppl +RETURN collect({ + id: id(ppl), + first_name: ppl.first_name, + middle_name: ppl.middle_name, + last_name: ppl.last_name, + born: ppl.born, + died: ppl.died, + profile_picture: ppl.profile_picture +}) as people, +relationships; \ No newline at end of file diff --git a/apps/db-adapter/internal/memgraph/queries/get_family_tree_by_id.cypher b/apps/db-adapter/internal/memgraph/queries/get_family_tree_by_id.cypher deleted file mode 100644 index b546783..0000000 --- a/apps/db-adapter/internal/memgraph/queries/get_family_tree_by_id.cypher +++ /dev/null @@ -1,8 +0,0 @@ -MATCH (n:Person)-[p:Parent*1..]->(family:Person) -WHERE id(n) = $id -OPTIONAL MATCH (family)-[c:Child*..4]->(children:Person) -WITH family, p, children, c, n -OPTIONAL MATCH (children)<-[p2:Parent]-(OtherParents:Person) -WITH family, p, children, c, OtherParents, p2, n -OPTIONAL MATCH (family)-[s:Spouse]-(spouse:Person) -RETURN family, p, children, c, OtherParents, p2, spouse, s, n; diff --git a/apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher b/apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher new file mode 100644 index 0000000..f63c46e --- /dev/null +++ b/apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher @@ -0,0 +1,22 @@ +MATCH (n:Person) +WHERE id(n) = $id +OPTIONAL MATCH (n)-[p:Parent*..]->(family:Person) +OPTIONAL MATCH (family)-[c:Child*1..4]->(children:Person) +OPTIONAL MATCH (family)-[s:Sibling]->(siblings:Person) +OPTIONAL MATCH (n)-[ds:Sibling]->(direct_siblings:Person) +OPTIONAL MATCH (family)-[fsp:Spouse]->(fspouse:Person) +OPTIONAL MATCH (children)-[csp:Spouse]->(cspouse:Person) +OPTIONAL MATCH (n)-[sp:Spouse]->(spouse:Person) +WITH collections.to_set(collect(n) + collect(family) + collect(children) + collect(direct_siblings) + collect(fspouse) + collect(cspouse) + collect(spouse)) as people, +collections.to_set(collect(c) + collect(p) + collect(s) + collect(ds) + collect(fsp) + collect(csp) + collect(sp)) as relationships +UNWIND people as ppl +RETURN collect({ + id: id(ppl), + first_name: ppl.first_name, + middle_name: ppl.middle_name, + last_name: ppl.last_name, + born: ppl.born, + died: ppl.died, + profile_picture: ppl.profile_picture +}) as people, +relationships; \ No newline at end of file diff --git a/apps/db-adapter/internal/memgraph/queries/get_limited_blood_relations_by_id.cypher b/apps/db-adapter/internal/memgraph/queries/get_limited_blood_relations_by_id.cypher deleted file mode 100644 index 9607089..0000000 --- a/apps/db-adapter/internal/memgraph/queries/get_limited_blood_relations_by_id.cypher +++ /dev/null @@ -1,4 +0,0 @@ -MATCH (n:Person)-[p:Parent*1..]->(family:Person) -WHERE id(n) = $id -OPTIONAL MATCH (family)-[c:Child*..4]->(children:Person) -RETURN family, p, children, c, n