Commit 3a21c591 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[DB] getRootUser function.

parent 1dada4f5
...@@ -187,7 +187,7 @@ nodesAPI :: Connection -> [NodeId] -> Server NodesAPI ...@@ -187,7 +187,7 @@ nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
nodesAPI conn ids = deleteNodes' conn ids nodesAPI conn ids = deleteNodes' conn ids
postNode :: Connection -> NodeId -> PostNode -> Handler Int postNode :: Connection -> NodeId -> PostNode -> Handler Int
postNode c pId (PostNode name nt) = liftIO $ mk c pId nt name postNode c pId (PostNode name nt) = liftIO $ mk c nt pId name
putNode :: Connection -> NodeId -> Handler Int putNode :: Connection -> NodeId -> Handler Int
putNode = undefined -- TODO putNode = undefined -- TODO
......
...@@ -69,7 +69,7 @@ module Gargantext.Database.Bashql ( get ...@@ -69,7 +69,7 @@ module Gargantext.Database.Bashql ( get
, put , put
, rename , rename
, tree , tree
, mkCorpus, postAnnuaire , mkCorpus, mkAnnuaire
, runCmd' , runCmd'
) )
where where
...@@ -162,8 +162,8 @@ mkCorpus name title ns = do ...@@ -162,8 +162,8 @@ mkCorpus name title ns = do
-- | -- |
-- import IMTClient as C -- import IMTClient as C
-- postAnnuaire "Annuaire IMT" (\n -> (maybe "" identity (C.prenom n)) <> " " <> (maybe "" identity (C.nom n))) (take 30 annuaire) -- postAnnuaire "Annuaire IMT" (\n -> (maybe "" identity (C.prenom n)) <> " " <> (maybe "" identity (C.nom n))) (take 30 annuaire)
postAnnuaire :: ToJSON a => Name -> (a -> Text) -> [a] -> Cmd NewNode mkAnnuaire :: ToJSON a => Name -> (a -> Text) -> [a] -> Cmd NewNode
postAnnuaire name title ns = do mkAnnuaire name title ns = do
pid <- last <$> home pid <- last <$> home
let uid = 1 let uid = 1
postNode uid pid ( Node' Annuaire name emptyObject postNode uid pid ( Node' Annuaire name emptyObject
......
{-|
Module : Gargantext.Database.Flow
Description : Database Flow
Copyright : (c) CNRS, 2017-Present
License : AGPL + CECILL v3
Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
add :: Corpus -> [Documents] -> IO Int
if new id -> extractNgrams + extract Authors + extract Sources
Map (Ngrams, NodeId)
insert Ngrams -> NgramsId
Map (NgramsId, NodeId) -> insert
data NgramsType = Sources | Authors | Terms
nodes_ngrams : column type, column list
documents
sources
authors
-}
{-# LANGUAGE NoImplicitPrelude #-}
module Gargantext.Database.Flow
where
import Data.Maybe (Maybe(..))
import Gargantext.Prelude
import Gargantext.Database.Bashql (runCmd')
import Gargantext.Database.Node (Cmd(..), getRootUser)
import Gargantext.Database.User (getUser, UserLight(..))
import Gargantext.Database.Node.Document.Import (insertDocuments)
flow = do
gargantua_id <- runCmd' (getUser "gargantua")
-- createUser
userNode <- case gargantua_id of
Nothing -> panic "no user"
Just userId -> runCmd' (getRootUser $ userLight_id userId)
case userNode of
[] -> pure ()
_ -> pure ()
-- getOrMk
--rootId <- runCmd' (getNodeWith userId nodeType)
{-
rootId <- mk NodeUser gargantua_id "Node Gargantua"
--folderId <- mk Folder parentId (Name "Data") (Descr "All corpora DATA here")
folderId <- mk Folder rootId "Data"
corpusId <- mk Corpus folderId (Name "WOS") (Descr "WOS database description")
docs <- parseDocuments WOS "doc/.."
ids <- addDocuments corpusId docs
user_id <- runCmd' (get RootUser "alexandre")
-}
...@@ -62,19 +62,10 @@ import Database.PostgreSQL.Simple (Connection) ...@@ -62,19 +62,10 @@ import Database.PostgreSQL.Simple (Connection)
import Opaleye hiding (FromField) import Opaleye hiding (FromField)
import Opaleye.Internal.QueryArr (Query) import Opaleye.Internal.QueryArr (Query)
import qualified Data.Profunctor.Product as PP import qualified Data.Profunctor.Product as PP
-- | Types for Node Database Management
data PGTSVector
newtype Cmd a = Cmd (ReaderT Connection IO a) ------------------------------------------------------------------------
deriving (Functor, Applicative, Monad, MonadReader Connection, MonadIO) {- | Reader Monad reinvented here:
runCmd :: Connection -> Cmd a -> IO a
runCmd c (Cmd f) = runReaderT f c
mkCmd :: (Connection -> IO a) -> Cmd a
mkCmd = Cmd . ReaderT
{-
newtype Cmd a = Cmd { unCmd :: Connection -> IO a } newtype Cmd a = Cmd { unCmd :: Connection -> IO a }
instance Monad Cmd where instance Monad Cmd where
...@@ -84,9 +75,19 @@ instance Monad Cmd where ...@@ -84,9 +75,19 @@ instance Monad Cmd where
a <- unCmd m c a <- unCmd m c
unCmd (f a) c unCmd (f a) c
-} -}
newtype Cmd a = Cmd (ReaderT Connection IO a)
deriving (Functor, Applicative, Monad, MonadReader Connection, MonadIO)
runCmd :: Connection -> Cmd a -> IO a
runCmd c (Cmd f) = runReaderT f c
mkCmd :: (Connection -> IO a) -> Cmd a
mkCmd = Cmd . ReaderT
------------------------------------------------------------------------ ------------------------------------------------------------------------
type CorpusId = Int type CorpusId = Int
type UserId = NodeId
type TypeId = Int
------------------------------------------------------------------------ ------------------------------------------------------------------------
instance FromField HyperdataCorpus where instance FromField HyperdataCorpus where
...@@ -107,6 +108,7 @@ instance FromField HyperdataUser where ...@@ -107,6 +108,7 @@ instance FromField HyperdataUser where
instance QueryRunnerColumnDefault PGJsonb HyperdataDocument where instance QueryRunnerColumnDefault PGJsonb HyperdataDocument where
queryRunnerColumnDefault = fieldQueryRunnerColumn queryRunnerColumnDefault = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGJsonb HyperdataDocumentV3 where instance QueryRunnerColumnDefault PGJsonb HyperdataDocumentV3 where
queryRunnerColumnDefault = fieldQueryRunnerColumn queryRunnerColumnDefault = fieldQueryRunnerColumn
...@@ -179,7 +181,6 @@ nodeTable' = Table "nodes" (PP.p7 ( optional "id" ...@@ -179,7 +181,6 @@ nodeTable' = Table "nodes" (PP.p7 ( optional "id"
queryNodeTable :: Query NodeRead queryNodeTable :: Query NodeRead
queryNodeTable = queryTable nodeTable queryNodeTable = queryTable nodeTable
selectNode :: Column PGInt4 -> Query NodeRead selectNode :: Column PGInt4 -> Query NodeRead
selectNode id = proc () -> do selectNode id = proc () -> do
row <- queryNodeTable -< () row <- queryNodeTable -< ()
...@@ -189,6 +190,18 @@ selectNode id = proc () -> do ...@@ -189,6 +190,18 @@ selectNode id = proc () -> do
runGetNodes :: Query NodeRead -> Cmd [Node Value] runGetNodes :: Query NodeRead -> Cmd [Node Value]
runGetNodes q = mkCmd $ \conn -> runQuery conn q runGetNodes q = mkCmd $ \conn -> runQuery conn q
------------------------------------------------------------------------
selectRootUser :: UserId -> Query NodeRead
selectRootUser userId = proc () -> do
row <- queryNodeTable -< ()
restrict -< node_userId row .== (pgInt4 userId)
restrict -< node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
returnA -< row
getRootUser :: UserId -> Cmd [Node HyperdataUser]
getRootUser userId = mkCmd $ \conn -> runQuery conn (selectRootUser userId)
------------------------------------------------------------------------
-- | order by publication date -- | order by publication date
-- Favorites (Bool), node_ngrams -- Favorites (Bool), node_ngrams
selectNodesWith :: ParentId -> Maybe NodeType selectNodesWith :: ParentId -> Maybe NodeType
...@@ -283,9 +296,6 @@ getNodesWithType :: Connection -> Column PGInt4 -> IO [Node HyperdataDocument] ...@@ -283,9 +296,6 @@ getNodesWithType :: Connection -> Column PGInt4 -> IO [Node HyperdataDocument]
getNodesWithType conn type_id = do getNodesWithType conn type_id = do
runQuery conn $ selectNodesWithType type_id runQuery conn $ selectNodesWithType type_id
type UserId = NodeId
type TypeId = Int
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- Quick and dirty -- Quick and dirty
...@@ -302,8 +312,7 @@ node userId parentId nodeType name nodeData = Node Nothing typeId userId parentI ...@@ -302,8 +312,7 @@ node userId parentId nodeType name nodeData = Node Nothing typeId userId parentI
node2write :: (Functor f2, Functor f1) => node2write :: (Functor f2, Functor f1) =>
Int Int -> NodePoly (f1 Int) Int Int parentId Text (f2 UTCTime) ByteString
-> NodePoly (f1 Int) Int Int parentId Text (f2 UTCTime) ByteString
-> (f1 (Column PGInt4), Column PGInt4, Column PGInt4, -> (f1 (Column PGInt4), Column PGInt4, Column PGInt4,
Column PGInt4, Column PGText, f2 (Column PGTimestamptz), Column PGInt4, Column PGText, f2 (Column PGTimestamptz),
Column PGJsonb) Column PGJsonb)
...@@ -397,8 +406,7 @@ childWith uId pId (Node' UserPage txt v []) = node2table uId pId (Node' UserPage ...@@ -397,8 +406,7 @@ childWith uId pId (Node' UserPage txt v []) = node2table uId pId (Node' UserPage
childWith _ _ (Node' _ _ _ _) = panic "This NodeType can not be a child" childWith _ _ (Node' _ _ _ _) = panic "This NodeType can not be a child"
mk :: Connection -> ParentId -> NodeType -> Text -> IO Int mk :: Connection -> NodeType -> ParentId -> Text -> IO Int
mk c pId nt name = fromIntegral <$> mkNode pId [node 1 pId nt name ""] c mk c nt pId name = fromIntegral <$> mkNode pId [node 1 pId nt name ""] c
...@@ -13,7 +13,6 @@ Functions to deal with users, database side. ...@@ -13,7 +13,6 @@ Functions to deal with users, database side.
{-# OPTIONS_GHC -fno-warn-name-shadowing #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
...@@ -23,22 +22,20 @@ Functions to deal with users, database side. ...@@ -23,22 +22,20 @@ Functions to deal with users, database side.
module Gargantext.Database.User where module Gargantext.Database.User where
import GHC.Show(Show(..)) import Control.Arrow (returnA)
import Control.Lens.TH (makeLensesWith, abbreviatedFields)
import Data.Eq(Eq(..)) import Data.Eq(Eq(..))
import Data.Time (UTCTime) import Data.List (find)
import Data.Text (Text)
import Data.Maybe (Maybe) import Data.Maybe (Maybe)
import Data.Profunctor.Product.TH (makeAdaptorAndInstance) import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
import Control.Lens.TH (makeLensesWith, abbreviatedFields) import Data.Text (Text)
import Control.Arrow (returnA) import Data.Time (UTCTime)
import qualified Database.PostgreSQL.Simple as PGS import GHC.Show(Show(..))
import Opaleye import Opaleye
import qualified Database.PostgreSQL.Simple as PGS
-- Functions only
import Data.List (find)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Database.Node (Cmd(..), mkCmd, runCmd)
data UserLight = UserLight { userLight_id :: Int data UserLight = UserLight { userLight_id :: Int
...@@ -135,14 +132,14 @@ instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where ...@@ -135,14 +132,14 @@ instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where
queryRunnerColumnDefault = fieldQueryRunnerColumn queryRunnerColumnDefault = fieldQueryRunnerColumn
users :: PGS.Connection -> IO [User] users :: Cmd [User]
users conn = runQuery conn queryUserTable users = mkCmd $ \conn -> runQuery conn queryUserTable
usersLight :: PGS.Connection -> IO [UserLight] usersLight :: Cmd [UserLight]
usersLight conn = map toUserLight <$> runQuery conn queryUserTable usersLight = mkCmd $ \conn -> map toUserLight <$> runQuery conn queryUserTable
type Username = Text type Username = Text
user :: PGS.Connection -> Username -> IO (Maybe UserLight) getUser :: Username -> Cmd (Maybe UserLight)
user c u = userLightWithUsername u <$> usersLight c getUser u = mkCmd $ \c -> userLightWithUsername u <$> runCmd c usersLight
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