2 Module : Gargantext.API.Node
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# LANGUAGE DataKinds #-}
16 {-# LANGUAGE DeriveGeneric #-}
17 {-# LANGUAGE NoImplicitPrelude #-}
18 {-# LANGUAGE OverloadedStrings #-}
19 {-# LANGUAGE TemplateHaskell #-}
20 {-# LANGUAGE TypeOperators #-}
22 -------------------------------------------------------------------
23 module Gargantext.API.Node
25 -------------------------------------------------------------------
27 import Control.Monad.IO.Class (liftIO)
28 import Control.Monad ((>>))
29 --import System.IO (putStrLn, readFile)
31 import Data.Aeson (FromJSON, ToJSON, Value())
32 --import Data.Text (Text(), pack)
33 import Data.Text (Text())
35 import Data.Time (UTCTime)
37 import Database.PostgreSQL.Simple (Connection)
39 import GHC.Generics (Generic)
41 -- import Servant.Multipart
43 import Gargantext.Prelude
44 import Gargantext.Database.Types.Node
45 import Gargantext.Database.Node ( getNodesWithParentId
46 , getNode, getNodesWith
47 , deleteNode, deleteNodes)
48 import Gargantext.Database.Facet (FacetDoc, getDocFacet
52 import Gargantext.TextFlow
53 import Gargantext.Viz.Graph (Graph)
54 import Gargantext.Core (Lang(..))
55 import Gargantext.Core.Types.Main (Tree, NodeTree)
56 import Gargantext.Text.Terms (TermType(..))
58 import Test.QuickCheck (elements)
59 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
60 -------------------------------------------------------------------
61 -- | Node API Types management
62 type Roots = Get '[JSON] [Node Value]
65 :<|> Delete '[JSON] Int
67 type NodesAPI = Delete '[JSON] Int
71 data Rename = Rename { name :: Text }
74 instance FromJSON Rename
75 instance ToJSON Rename
76 instance ToSchema Rename
77 instance Arbitrary Rename where
78 arbitrary = elements [Rename "test"]
80 type NodeAPI = Get '[JSON] (Node Value)
81 :<|> "rename" :> Summary " Rename Node"
82 :> ReqBody '[JSON] Rename
86 :<|> Delete '[JSON] Int
87 :<|> "children" :> Summary " Summary children"
88 :> QueryParam "type" NodeType
89 :> QueryParam "offset" Int
90 :> QueryParam "limit" Int
91 :> Get '[JSON] [Node Value]
92 :<|> "facet" :> Summary " Facet documents"
93 :> "documents" :> FacetDocAPI
94 -- :<|> "facet" :<|> "sources" :<|> FacetSourcesAPI
95 -- :<|> "facet" :<|> "authors" :<|> FacetAuthorsAPI
96 -- :<|> "facet" :<|> "terms" :<|> FacetTermsAPI
98 --data FacetFormat = Table | Chart
99 --data FacetType = Doc | Term | Source | Author
100 --data Facet = Facet Doc Format
103 type FacetDocAPI = "table"
104 :> Summary " Table data"
105 :> QueryParam "offset" Int
106 :> QueryParam "limit" Int
107 :> Get '[JSON] [FacetDoc]
110 :> Summary " Chart data"
111 :> QueryParam "from" UTCTime
112 :> QueryParam "to" UTCTime
113 :> Get '[JSON] [FacetChart]
115 -- Depending on the Type of the Node, we could post
116 -- New documents for a corpus
117 -- New map list terms
118 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
120 -- To launch a query and update the corpus
121 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
124 -- | Node API functions
125 roots :: Connection -> Server Roots
126 roots conn = liftIO (putStrLn ( "/user" :: Text) >> getNodesWithParentId conn 0 Nothing)
127 :<|> pure (panic "not implemented yet")
128 :<|> pure (panic "not implemented yet")
129 :<|> pure (panic "not implemented yet")
132 type GraphAPI = Get '[JSON] Graph
133 graphAPI :: Connection -> NodeId -> Server GraphAPI
134 graphAPI _ _ = liftIO $ textFlow (Mono EN) (Contexts contextText)
136 type TreeAPI = Get '[JSON] (Tree NodeTree)
137 treeAPI :: Connection -> NodeId -> Server TreeAPI
138 treeAPI _ _ = undefined
141 nodeAPI :: Connection -> NodeId -> Server NodeAPI
142 nodeAPI conn id = liftIO (putStrLn ("/node" :: Text) >> getNode conn id )
144 :<|> postNode conn id
146 :<|> deleteNode' conn id
147 :<|> getNodesWith' conn id
148 :<|> getFacet conn id
149 :<|> getChart conn id
152 -- | Check if the name is less than 255 char
153 --rename :: Connection -> NodeId -> Rename -> Server NodeAPI
154 rename :: Connection -> NodeId -> Rename -> Handler Int
157 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
158 nodesAPI conn ids = deleteNodes' conn ids
160 postNode :: Connection -> NodeId -> Handler Int
163 putNode :: Connection -> NodeId -> Handler Int
166 deleteNodes' :: Connection -> [NodeId] -> Handler Int
167 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
169 deleteNode' :: Connection -> NodeId -> Handler Int
170 deleteNode' conn id = liftIO (deleteNode conn id)
172 getNodesWith' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
173 -> Handler [Node Value]
174 getNodesWith' conn id nodeType offset limit = liftIO (getNodesWith conn id nodeType offset limit)
177 getFacet :: Connection -> NodeId -> Maybe Int -> Maybe Int
178 -> Handler [FacetDoc]
179 getFacet conn id offset limit = liftIO (putStrLn ( "/facet" :: Text)) >> liftIO (getDocFacet conn NodeCorpus id (Just Document) offset limit)
181 getChart :: Connection -> NodeId -> Maybe UTCTime -> Maybe UTCTime
182 -> Handler [FacetChart]
183 getChart _ _ _ _ = undefined
186 query :: Text -> Handler Text
191 -- TODO Is it possible to adapt the function according to iValue input ?
192 --upload :: MultipartData -> Handler Text
193 --upload multipartData = do
195 -- putStrLn "Inputs:"
196 -- forM_ (inputs multipartData) $ \input ->
197 -- putStrLn $ " " <> show (iName input)
198 -- <> " -> " <> show (iValue input)
200 -- forM_ (files multipartData) $ \file -> do
201 -- content <- readFile (fdFilePath file)
202 -- putStrLn $ "Content of " <> show (fdFileName file)
203 -- <> " at " <> fdFilePath file
205 -- pure (pack "Data loaded")