Commit dd2049aa authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Add getNodes function to Database.Query.Table.Node

parent c429cbb1
......@@ -28,6 +28,7 @@ module Gargantext.Database.Query.Table.Node
, getClosestParentIdByType'
, getCorporaWithParentId
, getNode
, getNodes
, getParent
, getNodeWith
, getNodeWithType
......@@ -84,7 +85,7 @@ import Gargantext.Database.Admin.Types.Hyperdata.List ( HyperdataList )
import Gargantext.Database.Admin.Types.Hyperdata.Prelude ( Hyperdata )
import Gargantext.Database.Prelude (DBCmd, JSONB, mkCmd, execPGSQuery, runPGSQuery, runOpaQuery)
import Gargantext.Database.Query.Filter (limit', offset')
import Gargantext.Database.Query.Table.Node.Children (getChildrenById)
import Gargantext.Database.Query.Table.Node.Children (getChildrenByParentId)
import Gargantext.Database.Query.Table.Node.Error
import Gargantext.Database.Schema.Node
import Gargantext.Prelude hiding (sum, head)
......@@ -313,6 +314,15 @@ getNode nId = do
Nothing -> nodeError (DoesNotExist nId)
Just r -> pure r
-- | Get the nodes recursively, as a hierarchical tree.
getNodes :: HasNodeError err => NodeId -> DBCmd err (Tree (Node Value))
getNodes nId = do
root <- getNode nId
children <- getChildrenByParentId nId
case children of
[] -> pure $ TreeN root []
xs -> TreeN root <$> forM xs getNodes
-- | Get the parent of a given 'Node', failing if this was called
-- on a root node.
getParent :: HasNodeError err => Node a -> DBCmd err (Node Value)
......@@ -494,7 +504,7 @@ copyNode copySubtree smart idToCopy newParentId = if copySubtree
then do
-- Non-recursively copy the node itself, then recursively copy its children:
copiedNode <- copyNode False smart idToCopy newParentId
children <- getChildrenById idToCopy
children <- getChildrenByParentId idToCopy
for_ children $ \child -> copyNode True smart child copiedNode
return copiedNode
-- Single-node (non-recursive) copy:
......
......@@ -63,9 +63,9 @@ getChildren a b c d e = getChildrenNode a b c d e
-- | Get the list of (IDs of) children of a given node (ID)
getChildrenById :: NodeId -- ^ ID of the parent node
-> DBCmd err [NodeId] -- ^ List of IDs of the children nodes
getChildrenById parentId = runPGSQuery
getChildrenByParentId :: NodeId -- ^ ID of the parent node
-> DBCmd err [NodeId] -- ^ List of IDs of the children nodes
getChildrenByParentId parentId = runPGSQuery
[sql| SELECT id FROM public.nodes WHERE parent_id = ?; |]
parentId
......
Markdown is supported
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