]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Node/Update.hs
[MERGE] master and masterAfterDemo.
[gargantext.git] / src / Gargantext / Database / Node / Update.hs
1 {-|
2 Module : Gargantext.Database.Node.Update
3 Description : Update Node in Database (Postgres)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# LANGUAGE NoImplicitPrelude #-}
13 {-# LANGUAGE OverloadedStrings #-}
14 {-# LANGUAGE QuasiQuotes #-}
15 {-# LANGUAGE RankNTypes #-}
16
17
18 module Gargantext.Database.Node.Update (Update(..), update) where
19
20 import Data.Text (Text)
21 import qualified Data.Text as DT
22 import Database.PostgreSQL.Simple
23
24 import Gargantext.Prelude
25 import Gargantext.Database.Utils
26
27 -- import Data.ByteString
28 --rename :: NodeId -> Text -> IO ByteString
29 --rename nodeId name = formatPGSQuery "UPDATE nodes SET name=? where id=?" (name,nodeId)
30 ------------------------------------------------------------------------
31 type NodeId = Int
32 type Name = Text
33 type ParentId = Int
34
35 data Update = Rename NodeId Name
36 | Move NodeId ParentId
37
38 -- | Update a Node
39 -- TODO : Field as parameter
40 -- TODO jsonb values, consider this:
41 -- https://stackoverflow.com/questions/26703476/how-to-perform-update-operations-on-columns-of-type-jsonb-in-postgres-9-4
42
43 unOnly :: Only a -> a
44 unOnly (Only a) = a
45
46 update :: Update -> Cmd err [Int]
47 update (Rename nId name) = map unOnly <$> runPGSQuery "UPDATE nodes SET name=? where id=? returning id"
48 (DT.take 255 name,nId)
49 update (Move nId pId) = map unOnly <$> runPGSQuery "UPDATE nodes SET parent_id= ? where id=? returning id"
50 (pId, nId)
51
52