]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Node/Update.hs
MonadBase replaces MonadIO
[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 FlexibleContexts #-}
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE QuasiQuotes #-}
16 {-# LANGUAGE RankNTypes #-}
17
18
19 module Gargantext.Database.Node.Update (Update(..), update) where
20
21 import qualified Data.Text as DT
22 import Database.PostgreSQL.Simple
23
24 import Gargantext.Prelude
25 import Gargantext.Core.Types (Name)
26 import Gargantext.Database.Utils
27 import Gargantext.Database.Types.Node (NodeId, ParentId)
28
29 -- import Data.ByteString
30 --rename :: NodeId -> Text -> IO ByteString
31 --rename nodeId name = formatPGSQuery "UPDATE nodes SET name=? where id=?" (name,nodeId)
32 ------------------------------------------------------------------------
33
34 data Update = Rename NodeId Name
35 | Move NodeId ParentId
36
37 -- | Update a Node
38 -- TODO : Field as parameter
39 -- TODO jsonb values, consider this:
40 -- https://stackoverflow.com/questions/26703476/how-to-perform-update-operations-on-columns-of-type-jsonb-in-postgres-9-4
41
42 unOnly :: Only a -> a
43 unOnly (Only a) = a
44
45 -- TODO-ACCESS
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