module Database.Neo4j.Label where
import Network.HTTP.Base (urlEncodeVars)
import Control.Exception.Lifted (catch)
import qualified Data.Aeson as J
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy as L
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Database.Neo4j.Types
import Database.Neo4j.Http
allLabels :: Neo4j [Label]
allLabels = Neo4j $ \conn -> httpRetrieveSure conn "/db/data/labels"
getLabels :: Node -> Neo4j [Label]
getLabels n = Neo4j $ \conn -> httpRetrieveSure conn (runNodeIdentifier n <> "/labels") `catch` proc404Exc n
getNodesByLabelAndProperty :: Label -> Maybe (T.Text, PropertyValue) -> Neo4j [Node]
getNodesByLabelAndProperty lbl prop = Neo4j $ \conn ->
httpRetrieveSure conn ("/db/data/label/" <> TE.encodeUtf8 lbl <> "/nodes" <> propUrl prop)
where propUrl Nothing = ""
propUrl (Just (name, value)) = S.pack $ '?' : urlEncodeVars [(T.unpack name, lbsToStr $ J.encode value)]
lbsToStr = S.unpack . L.toStrict
addLabels :: [Label] -> Node -> Neo4j ()
addLabels lbls n = Neo4j $ \conn -> (do
httpCreate_ conn (runNodeIdentifier n <> "/labels") (J.encode lbls)
return ()) `catch` proc404Exc n
changeLabels :: [Label] -> Node -> Neo4j ()
changeLabels lbls n = Neo4j $ \conn ->
httpModify conn (runNodeIdentifier n <> "/labels") (J.encode lbls) `catch` proc404Exc n
removeLabel :: Label -> Node -> Neo4j ()
removeLabel lbl n = Neo4j $ \conn -> (do
_ <- httpDeleteNo404 conn (runNodeIdentifier n <> "/labels/" <> TE.encodeUtf8 lbl)
return ()) `catch` proc404Exc n