Commit 18ceac9c authored by Alexandre Delanoë's avatar Alexandre Delanoë

[API][DB] rename connection.

parent 62eb01c8
...@@ -47,6 +47,7 @@ import Gargantext.Database.Node ( runCmd ...@@ -47,6 +47,7 @@ import Gargantext.Database.Node ( runCmd
, getNodesWithParentId , getNodesWithParentId
, getNode, getNodesWith , getNode, getNodesWith
, deleteNode, deleteNodes) , deleteNode, deleteNodes)
import qualified Gargantext.Database.Node.Update as U (update, Update(..))
import Gargantext.Database.Facet (FacetDoc, getDocFacet import Gargantext.Database.Facet (FacetDoc, getDocFacet
,FacetChart) ,FacetChart)
import Gargantext.Database.Tree (treeDB, HasTreeError(..), TreeError(..)) import Gargantext.Database.Tree (treeDB, HasTreeError(..), TreeError(..))
...@@ -83,7 +84,7 @@ instance Arbitrary Rename where ...@@ -83,7 +84,7 @@ instance Arbitrary Rename where
type NodeAPI = Get '[JSON] (Node Value) type NodeAPI = Get '[JSON] (Node Value)
:<|> "rename" :> Summary " Rename Node" :<|> "rename" :> Summary " Rename Node"
:> ReqBody '[JSON] Rename :> ReqBody '[JSON] Rename
:> Put '[JSON] Int :> Put '[JSON] [Int]
:<|> Post '[JSON] Int :<|> Post '[JSON] Int
:<|> Put '[JSON] Int :<|> Put '[JSON] Int
:<|> Delete '[JSON] Int :<|> Delete '[JSON] Int
...@@ -162,8 +163,8 @@ nodeAPI conn id = liftIO (putStrLn ("/node" :: Text) >> getNode co ...@@ -162,8 +163,8 @@ nodeAPI conn id = liftIO (putStrLn ("/node" :: Text) >> getNode co
-- :<|> query -- :<|> query
-- | Check if the name is less than 255 char -- | Check if the name is less than 255 char
--rename :: Connection -> NodeId -> Rename -> Server NodeAPI --rename :: Connection -> NodeId -> Rename -> Server NodeAPI
rename :: Connection -> NodeId -> Rename -> Handler Int rename :: Connection -> NodeId -> Rename -> Handler [Int]
rename = undefined rename c nId (Rename name) = liftIO $ U.update (U.Rename nId name) c
nodesAPI :: Connection -> [NodeId] -> Server NodesAPI nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
nodesAPI conn ids = deleteNodes' conn ids nodesAPI conn ids = deleteNodes' conn ids
......
...@@ -80,7 +80,6 @@ import Data.Text (Text) ...@@ -80,7 +80,6 @@ import Data.Text (Text)
import Data.Aeson import Data.Aeson
import Data.Aeson.Types import Data.Aeson.Types
import Data.List (last, concat) import Data.List (last, concat)
import Database.PostgreSQL.Simple (Only)
import Gargantext.Core.Types import Gargantext.Core.Types
import Gargantext.Database.Utils (connectGargandb) import Gargantext.Database.Utils (connectGargandb)
...@@ -96,10 +95,10 @@ import Opaleye hiding (FromField) ...@@ -96,10 +95,10 @@ import Opaleye hiding (FromField)
type PWD = [NodeId] type PWD = [NodeId]
--data PWD' a = a | PWD' [a] --data PWD' a = a | PWD' [a]
rename :: NodeId -> Text -> Cmd [Only Int] rename :: NodeId -> Text -> Cmd [Int]
rename n t = mkCmd $ \conn -> U.update (U.Rename n t) conn rename n t = mkCmd $ \conn -> U.update (U.Rename n t) conn
mv :: NodeId -> ParentId -> Cmd [Only Int] mv :: NodeId -> ParentId -> Cmd [Int]
mv n p = mkCmd $ \conn -> U.update (U.Move n p) conn mv n p = mkCmd $ \conn -> U.update (U.Move n p) conn
-- | TODO get Children or Node -- | TODO get Children or Node
...@@ -140,7 +139,7 @@ del [] = pure 0 ...@@ -140,7 +139,7 @@ del [] = pure 0
del ns = deleteNodes ns del ns = deleteNodes ns
-- | TODO -- | TODO
put :: U.Update -> Cmd [Only Int] put :: U.Update -> Cmd [Int]
put u = mkCmd $ U.update u put u = mkCmd $ U.update u
-- | TODO -- | TODO
......
...@@ -37,10 +37,14 @@ data Update = Rename NodeId Name ...@@ -37,10 +37,14 @@ data Update = Rename NodeId Name
-- TODO : Field as parameter -- TODO : Field as parameter
-- TODO jsonb values, consider this: -- TODO jsonb values, consider this:
-- https://stackoverflow.com/questions/26703476/how-to-perform-update-operations-on-columns-of-type-jsonb-in-postgres-9-4 -- https://stackoverflow.com/questions/26703476/how-to-perform-update-operations-on-columns-of-type-jsonb-in-postgres-9-4
update :: Update -> Connection -> IO [Only Int]
update (Rename nId name) conn = query conn "UPDATE nodes SET name=? where id=? returning id" unOnly :: Only a -> a
unOnly (Only a) = a
update :: Update -> Connection -> IO [Int]
update (Rename nId name) conn = map unOnly <$> query conn "UPDATE nodes SET name=? where id=? returning id"
(DT.take 255 name,nId) (DT.take 255 name,nId)
update (Move nId pId) conn = query conn "UPDATE nodes SET parent_id= ? where id=? returning id" update (Move nId pId) conn = map unOnly <$> query conn "UPDATE nodes SET parent_id= ? where id=? returning id"
(pId, nId) (pId, nId)
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