[BASHQL] refactor Connection argument

parent ea23aa3f
...@@ -75,6 +75,7 @@ import Control.Monad.Reader -- (Reader, ask) ...@@ -75,6 +75,7 @@ import Control.Monad.Reader -- (Reader, ask)
import Database.PostgreSQL.Simple (Connection) import Database.PostgreSQL.Simple (Connection)
import Data.Text (Text, pack) import Data.Text (Text, pack)
import Data.Aeson import Data.Aeson
import Data.Aeson.Types
import Data.List (last, concat) import Data.List (last, concat)
import Gargantext.Core.Types import Gargantext.Core.Types
...@@ -91,32 +92,34 @@ import Opaleye hiding (FromField) ...@@ -91,32 +92,34 @@ import Opaleye hiding (FromField)
type PWD = [NodeId] type PWD = [NodeId]
--data PWD' a = a | PWD' [a] --data PWD' a = a | PWD' [a]
type Cmd a = Connection -> IO a
-- | TODO get Children or Node -- | TODO get Children or Node
get :: Connection -> PWD -> IO [Node Value] get :: PWD -> Cmd [Node Value]
get _ [] = pure [] get [] _ = pure []
get conn pwd = runQuery conn $ selectNodesWithParentID (last pwd) get pwd conn = runQuery conn $ selectNodesWithParentID (last pwd)
-- | Home, need to filter with UserId -- | Home, need to filter with UserId
home :: Connection -> IO PWD home :: Cmd PWD
home c = map node_id <$> getNodesWithParentId c 0 Nothing home c = map node_id <$> getNodesWithParentId c 0 Nothing
-- | ls == get Children -- | ls == get Children
ls :: Connection -> PWD -> IO [Node Value] ls :: PWD -> Cmd [Node Value]
ls = get ls = get
tree :: Connection -> PWD -> IO [Node Value] tree :: PWD -> Cmd [Node Value]
tree c p = do tree p c = do
ns <- get c p ns <- get p c
children <- mapM (\p' -> get c [p']) $ map node_id ns children <- mapM (\p' -> get [p'] c) $ map node_id ns
pure $ ns <> (concat children) pure $ ns <> (concat children)
-- | TODO -- | TODO
post :: Connection -> PWD -> [NodeWrite'] -> IO Int64 post :: PWD -> [NodeWrite'] -> Cmd Int64
post [] _ _ = pure 0
post _ [] _ = pure 0 post _ [] _ = pure 0
post _ _ [] = pure 0 post pth ns c = mkNode c (last pth) ns
post c pth ns = mkNode c (last pth) ns
--postR :: Connection -> PWD -> [NodeWrite'] -> IO [Int] --postR :: Connection -> PWD -> [NodeWrite'] -> IO [Int]
--postR _ [] _ = pure [0] --postR _ [] _ = pure [0]
...@@ -127,9 +130,9 @@ post c pth ns = mkNode c (last pth) ns ...@@ -127,9 +130,9 @@ post c pth ns = mkNode c (last pth) ns
--rm :: Connection -> PWD -> [NodeId] -> IO Int --rm :: Connection -> PWD -> [NodeId] -> IO Int
--rm = del --rm = del
del :: Connection -> [NodeId] -> IO Int del :: [NodeId] -> Cmd Int
del _ [] = pure 0 del [] _ = pure 0
del c ns = deleteNodes c ns del ns c = deleteNodes c ns
-- | TODO -- | TODO
--put :: Connection -> PWD -> [a] -> IO Int64 --put :: Connection -> PWD -> [a] -> IO Int64
...@@ -141,84 +144,70 @@ del c ns = deleteNodes c ns ...@@ -141,84 +144,70 @@ del c ns = deleteNodes c ns
-- jump NodeId -- jump NodeId
-- touch Dir -- touch Dir
type CorpusName = Text
postCorpus :: ToJSON a => CorpusName -> (a -> Text) -> [a] -> Cmd [Int]
postCorpus corpusName title ns c = do
pid <- last <$> home c
let uid = 1
postNode c uid pid ( Node' NodeCorpus corpusName emptyObject
(map (\n -> Node' Document (title n) (toJSON n) []) ns)
)
-- |
-- import IMTClient as C
-- postAnnuaire "Annuaire IMT" (\n -> (maybe "" identity (C.prenom n)) <> " " <> (maybe "" identity (C.nom n))) (take 30 annuaire)
postAnnuaire :: ToJSON a => CorpusName -> (a -> Text) -> [a] -> Cmd [Int]
postAnnuaire corpusName title ns c = do
pid <- last <$> home c
let uid = 1
postNode c uid pid ( Node' Annuaire corpusName emptyObject
(map (\n -> Node' UserPage (title n) (toJSON n) []) ns)
)
-------------------------------------------------------------- --------------------------------------------------------------
-- Tests -- Tests
-------------------------------------------------------------- --------------------------------------------------------------
get' :: PWD -> Reader Connection (IO [Node Value]) get' :: PWD -> IO [Node Value]
get' [] = pure $ pure [] get' = runCmd . get
get' pwd = do
connection <- ask
pure $ runQuery connection $ selectNodesWithParentID (last pwd)
home' :: IO PWD home' :: IO PWD
home' = do home' = runCmd home
c <- connectGargandb "gargantext.ini"
home c
--home'' :: Reader Connection (IO PWD)
--home'' = do
-- c <- ask
-- liftIO $ home c
ls' :: IO [Node Value] ls' :: IO [Node Value]
ls' = do ls' = runCmd $ \c -> do
c <- connectGargandb "gargantext.ini"
h <- home c h <- home c
ls c h ls h c
tree' :: IO [Node Value] tree' :: IO [Node Value]
tree' = do tree' = runCmd $ \c -> do
c <- connectGargandb "gargantext.ini"
h <- home c h <- home c
tree c h tree h c
post' :: IO [Int] post' :: IO [Int]
post' = do post' = runCmd $ \c -> do
c <- connectGargandb "gargantext.ini"
pid <- last <$> home c pid <- last <$> home c
let uid = 1 let uid = 1
postNode c uid pid ( Node' NodeCorpus (pack "Premier corpus") (toJSON (pack "{}"::Text)) [ Node' Document (pack "Doc1") (toJSON (pack "{}" :: Text)) [] postNode c uid pid ( Node' NodeCorpus (pack "Premier corpus") emptyObject [ Node' Document (pack "Doc1") emptyObject []
, Node' Document (pack "Doc2") (toJSON (pack "{}" :: Text)) [] , Node' Document (pack "Doc2") emptyObject []
, Node' Document (pack "Doc3") (toJSON (pack "{}" :: Text)) [] , Node' Document (pack "Doc3") emptyObject []
] ]
) )
type CorpusName = Text
-- | -- |
-- myCorpus <- Prelude.map doc2hyperdataDocument <$> toDocs <$> snd <$> readCsv "doc/corpus_imt/Gargantext_Corpus_small.csv" -- myCorpus <- Prelude.map doc2hyperdataDocument <$> toDocs <$> snd <$> readCsv "doc/corpus_imt/Gargantext_Corpus_small.csv"
-- There is an error in the CSV parsing... -- There is an error in the CSV parsing...
-- let myCorpus' = Prelude.filter (\n -> T.length (maybe "" identity (hyperdataDocument_title n)) > 30) myCorpus -- let myCorpus' = Prelude.filter (\n -> T.length (maybe "" identity (hyperdataDocument_title n)) > 30) myCorpus
postCorpus :: ToJSON a => CorpusName -> (a -> Text) -> [a] -> IO [Int]
postCorpus corpusName title ns = do
c <- connectGargandb "gargantext.ini"
pid <- last <$> home c
let uid = 1
postNode c uid pid ( Node' NodeCorpus corpusName (toJSON (pack "{}"::Text))
(map (\n -> Node' Document (title n) (toJSON n) []) ns)
)
-- |
-- import IMTClient as C
-- postAnnuaire "Annuaire IMT" (\n -> (maybe "" identity (C.prenom n)) <> " " <> (maybe "" identity (C.nom n))) (take 30 annuaire)
postAnnuaire :: ToJSON a => CorpusName -> (a -> Text) -> [a] -> IO [Int]
postAnnuaire corpusName title ns = do
c <- connectGargandb "gargantext.ini"
pid <- last <$> home c
let uid = 1
postNode c uid pid ( Node' Annuaire corpusName (toJSON (pack "{}"::Text))
(map (\n -> Node' UserPage (title n) (toJSON n) []) ns)
)
del' :: [NodeId] -> IO Int del' :: [NodeId] -> IO Int
del' ns = do del' ns = runCmd $ del ns
c <- connectGargandb "gargantext.ini"
del c ns
-- corporaOf :: Username -> IO [Corpus] -- corporaOf :: Username -> IO [Corpus]
runCmd :: Cmd a -> IO a
runCmd f = do
c <- connectGargandb "gargantext.ini"
f c
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