Commit 0b32959b authored by Alexandre Delanoë's avatar Alexandre Delanoë

[REST] Document View route.

parent 8c9db31d
...@@ -34,6 +34,8 @@ import Gargantext.API.Node (Roots, roots, NodeAPI, nodeAPI) ...@@ -34,6 +34,8 @@ import Gargantext.API.Node (Roots, roots, NodeAPI, nodeAPI)
import Gargantext.Database.Private (databaseParameters) import Gargantext.Database.Private (databaseParameters)
-- | startGargantext takes as parameters port number and Ini file. -- | startGargantext takes as parameters port number and Ini file.
startGargantext :: Int -> FilePath -> IO () startGargantext :: Int -> FilePath -> IO ()
startGargantext port file = do startGargantext port file = do
...@@ -46,6 +48,7 @@ startGargantext port file = do ...@@ -46,6 +48,7 @@ startGargantext port file = do
-- | Main routes of the API are typed -- | Main routes of the API are typed
type API = "roots" :> Roots type API = "roots" :> Roots
:<|> "node" :> Capture "id" Int :> NodeAPI :<|> "node" :> Capture "id" Int :> NodeAPI
-- :<|> "static"
-- :<|> "list" :> Capture "id" Int :> NodeAPI -- :<|> "list" :> Capture "id" Int :> NodeAPI
-- :<|> "ngrams" :> Capture "id" Int :> NodeAPI -- :<|> "ngrams" :> Capture "id" Int :> NodeAPI
-- :<|> "auth" :> Capture "id" Int :> NodeAPI -- :<|> "auth" :> Capture "id" Int :> NodeAPI
......
...@@ -25,37 +25,51 @@ import Servant ...@@ -25,37 +25,51 @@ import Servant
import Servant.Multipart import Servant.Multipart
import System.IO (putStrLn, readFile) import System.IO (putStrLn, readFile)
import Data.Text (Text(), pack) import Data.Text (Text(), pack)
import Database.PostgreSQL.Simple (Connection) import Database.PostgreSQL.Simple (Connection)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Types.Main (Node, NodeId) import Gargantext.Types.Main (Node, NodeId, NodeType)
import Gargantext.Database.Node (getNodesWithParentId, getNode) import Gargantext.Database.Node (getNodesWithParentId, getNode, getNodesWith)
-- | Node API Types management -- | Node API Types management
type Roots = Get '[JSON] [Node Value] type Roots = Get '[JSON] [Node Value]
type NodeAPI = Get '[JSON] (Node Value) type NodeAPI = Get '[JSON] (Node Value)
:<|> "children" :> Get '[JSON] [Node Value]
:<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text -- Example for Document Facet view, to populate the tabular:
-- http://localhost:8008/node/347476/children?type=Document&limit=3
-- /!\ FIXME : nodeType is case sensitive
-- /!\ see NodeTypes in Types/Main.hs
:<|> "children" :> QueryParam "type" NodeType
:> QueryParam "offset" Int
:> QueryParam "limit" Int
:> Get '[JSON] [Node Value]
-- Depending on the Type of the Node, we could post -- Depending on the Type of the Node, we could post
-- New documents for a corpus -- New documents for a corpus
-- New map list terms -- New map list terms
:<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
-- To launch a query and update the corpus
:<|> "query" :> Capture "string" Text :> Get '[JSON] Text :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
-- :<|> "children" :> QueryParam "type" Text :> Get '[JSON] [Node Value]
-- | Node API functions -- | Node API functions
roots :: Connection -> Server Roots roots :: Connection -> Server Roots
roots conn = liftIO (getNodesWithParentId conn 0) roots conn = liftIO (getNodesWithParentId conn 0 Nothing)
nodeAPI :: Connection -> NodeId -> Server NodeAPI nodeAPI :: Connection -> NodeId -> Server NodeAPI
nodeAPI conn id = liftIO (getNode conn id) nodeAPI conn id = liftIO (getNode conn id)
:<|> liftIO (getNodesWithParentId conn id) :<|> getNodesWith' conn id
:<|> upload :<|> upload
:<|> query :<|> query
getNodesWith' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
-> Handler [Node Value]
getNodesWith' conn id nodeType offset limit = liftIO (getNodesWith conn id nodeType offset limit)
query :: Text -> Handler Text query :: Text -> Handler Text
query s = pure s query s = pure s
......
{-|
Module : Gargantext.Database.Node
Description : Main requests of Node to the database
Copyright : (c) CNRS, 2017-Present
License : AGPL + CECILL v3
Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
...@@ -16,6 +26,7 @@ import Database.PostgreSQL.Simple.FromField ( Conversion ...@@ -16,6 +26,7 @@ import Database.PostgreSQL.Simple.FromField ( Conversion
, returnError , returnError
) )
import Prelude hiding (null, id) import Prelude hiding (null, id)
import Gargantext.Types.Main (NodeType)
import Database.PostgreSQL.Simple.Internal (Field) import Database.PostgreSQL.Simple.Internal (Field)
import Control.Arrow (returnA) import Control.Arrow (returnA)
import Control.Lens.TH (makeLensesWith, abbreviatedFields) import Control.Lens.TH (makeLensesWith, abbreviatedFields)
...@@ -106,21 +117,67 @@ nodeTable = Table "nodes" (pNode Node { node_id = optional "id" ...@@ -106,21 +117,67 @@ nodeTable = Table "nodes" (pNode Node { node_id = optional "id"
) )
queryNodeTable :: Query NodeRead
queryNodeTable = queryTable nodeTable
selectNodes :: Column PGInt4 -> Query NodeRead selectNodes :: Column PGInt4 -> Query NodeRead
selectNodes id = proc () -> do selectNodes id = proc () -> do
row <- queryNodeTable -< () row <- queryNodeTable -< ()
restrict -< node_id row .== id restrict -< node_id row .== id
returnA -< row returnA -< row
runGetNodes :: Connection -> Query NodeRead -> IO [Document] runGetNodes :: Connection -> Query NodeRead -> IO [Node Value]
runGetNodes = runQuery runGetNodes = runQuery
type ParentId = NodeId
type Limit = Int
type Offset = Int
-- | order by publication date
-- Favorites (Bool), node_ngrams
selectNodesWith :: ParentId -> Maybe NodeType -> Maybe Offset -> Maybe Limit -> Query NodeRead
selectNodesWith parentId maybeNodeType maybeOffset maybeLimit =
--offset' maybeOffset $ limit' maybeLimit $ orderBy (asc (hyperdataDocument_Publication_date . node_hyperdata)) $ selectNodesWith' parentId typeId
offset' maybeOffset $ limit' maybeLimit $ orderBy (asc node_id) $ selectNodesWith' parentId maybeNodeType
limit' :: Maybe Limit -> Query NodeRead -> Query NodeRead
limit' maybeLimit query = maybe query (\l -> limit l query) maybeLimit
offset' :: Maybe Offset -> Query NodeRead -> Query NodeRead
offset' maybeOffset query = maybe query (\o -> offset o query) maybeOffset
-- Add order by
selectNodesWith' :: ParentId -> Maybe NodeType -> Query NodeRead
selectNodesWith' parentId maybeNodeType = proc () -> do
node <- (proc () -> do
row@(Node _ typeId _ parentId' _ _ _) <- queryNodeTable -< ()
restrict -< parentId' .== (toNullable $ pgInt4 parentId)
let typeId' = maybe 0 nodeTypeId maybeNodeType
restrict -< if typeId' > 0
then typeId .== (pgInt4 (typeId' :: Int))
else (pgBool True)
returnA -< row ) -< ()
returnA -< node
--getNodesWith' :: Connection -> Int -> Maybe NodeType -> Maybe Offset' -> Maybe Limit' -> IO [Node Value]
--getNodesWith' conn parentId maybeNodeType maybeOffset maybeLimit = runQuery conn $ selectNodesWith parentId xxx maybeOffset maybeLimit
getNodesWith :: Connection -> Int -> Maybe NodeType -> Maybe Offset -> Maybe Limit -> IO [Node Value]
getNodesWith conn parentId nodeType maybeOffset maybeLimit = runQuery conn $ selectNodesWith parentId nodeType maybeOffset maybeLimit
-- NP check type -- NP check type
getNodesWithParentId :: Connection -> Int -> IO [Node Value] getNodesWithParentId :: Connection -> Int -> Maybe Text -> IO [Node Value]
getNodesWithParentId conn n = runQuery conn $ selectNodesWithParentID n getNodesWithParentId conn n _ = runQuery conn $ selectNodesWithParentID n
selectNodesWithParentID :: Int -> Query NodeRead selectNodesWithParentID :: Int -> Query NodeRead
selectNodesWithParentID n = proc () -> do selectNodesWithParentID n = proc () -> do
...@@ -132,9 +189,6 @@ selectNodesWithParentID n = proc () -> do ...@@ -132,9 +189,6 @@ selectNodesWithParentID n = proc () -> do
isNull parent_id isNull parent_id
returnA -< row returnA -< row
queryNodeTable :: Query NodeRead
queryNodeTable = queryTable nodeTable
...@@ -153,11 +207,3 @@ getNodesWithType conn type_id = do ...@@ -153,11 +207,3 @@ getNodesWithType conn type_id = do
runQuery conn $ selectNodesWithType type_id runQuery conn $ selectNodesWithType type_id
-- NP check type
getCorpusDocument :: Connection -> Int -> IO [Document]
getCorpusDocument conn n = runQuery conn (selectNodesWithParentID n)
-- NP check type
getProjectCorpora :: Connection -> Int -> IO [Corpus]
getProjectCorpora conn node_id = do
runQuery conn $ selectNodesWithParentID node_id
...@@ -11,6 +11,7 @@ Here is a longer description of this module, containing some ...@@ -11,6 +11,7 @@ Here is a longer description of this module, containing some
commentary with @some markup@. commentary with @some markup@.
-} -}
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveGeneric #-}
module Gargantext.Types.Main where module Gargantext.Types.Main where
...@@ -19,7 +20,12 @@ import Prelude ...@@ -19,7 +20,12 @@ import Prelude
import Data.Eq (Eq()) import Data.Eq (Eq())
import Data.Monoid ((<>)) import Data.Monoid ((<>))
import Protolude (fromMaybe) import Protolude (fromMaybe)
import Data.Aeson
import GHC.Generics
import Servant
import Data.Text (unpack)
import Text.Read (read)
import Data.Either (Either(Right))
--import Data.ByteString (ByteString()) --import Data.ByteString (ByteString())
import Data.Text (Text) import Data.Text (Text)
import Data.Time (UTCTime) import Data.Time (UTCTime)
...@@ -87,7 +93,11 @@ data NodeType = NodeUser | Project | Corpus | Document | DocumentCopy ...@@ -87,7 +93,11 @@ data NodeType = NodeUser | Project | Corpus | Document | DocumentCopy
| Classification | Classification
| Lists | Lists
| Metrics | Metrics
deriving (Show, Read, Eq) deriving (Show, Read, Eq, Generic)
instance FromJSON NodeType
instance ToJSON NodeType
instance FromHttpApiData NodeType where parseUrlPiece = Right . read . unpack
data Classification = Favorites | MyClassifcation data Classification = Favorites | MyClassifcation
......
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