module Database.Neo4j.Node where
import Control.Exception.Lifted (throw, catch)
import qualified Data.Aeson as J
import qualified Data.ByteString as S
import qualified Network.HTTP.Types as HT
import Database.Neo4j.Types
import Database.Neo4j.Http
nodeId :: Node -> S.ByteString
nodeId n = S.drop (pathLength + 1) (runNodeIdentifier n)
where pathLength = S.length nodeAPI
createNode :: Properties -> Neo4j Node
createNode props = Neo4j $ \conn -> httpCreate conn nodeAPI (J.encode props)
getNode :: NodeIdentifier a => a -> Neo4j (Maybe Node)
getNode n = Neo4j $ \conn -> httpRetrieve conn (runNodeIdentifier n)
deleteNode :: NodeIdentifier a => a -> Neo4j ()
deleteNode n = Neo4j $ \conn -> httpDelete conn (runNodeIdentifier n) `catch` processConflict
where processConflict e@(Neo4jUnexpectedResponseException s)
| s == HT.status409 = throw (Neo4jNonOrphanNodeDeletionException $ runNodeIdentifier n)
| otherwise = throw e
processConflict e = throw e