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 NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE OverloadedStrings #-}
21 -------------------------------------------------------------------
22 module Gargantext.API.Node
24 -------------------------------------------------------------------
26 import Control.Monad.IO.Class (liftIO)
27 import Control.Monad ((>>))
28 --import System.IO (putStrLn, readFile)
30 import Data.Aeson (Value())
31 --import Data.Text (Text(), pack)
32 import Data.Text (Text())
33 import Data.Time (UTCTime)
35 import Database.PostgreSQL.Simple (Connection)
38 -- import Servant.Multipart
40 import Gargantext.Prelude
41 import Gargantext.Database.Types.Node
42 import Gargantext.Database.Node ( getNodesWithParentId
43 , getNode, getNodesWith
44 , deleteNode, deleteNodes)
45 import Gargantext.Database.Facet (FacetDoc, getDocFacet
49 import Gargantext.TextFlow
50 import Gargantext.Viz.Graph (Graph)
51 import Gargantext.Core (Lang(..))
52 import Gargantext.Core.Types.Main (Tree, NodeTree)
53 import Gargantext.Text.Terms (TermType(..))
54 -------------------------------------------------------------------
55 -------------------------------------------------------------------
56 -- | Node API Types management
57 type Roots = Get '[JSON] [Node Value]
60 :<|> Delete '[JSON] Int
62 type NodesAPI = Delete '[JSON] Int
64 type NodeAPI = Get '[JSON] (Node Value)
67 :<|> Delete '[JSON] Int
68 :<|> "children" :> Summary " Summary children"
69 :> QueryParam "type" NodeType
70 :> QueryParam "offset" Int
71 :> QueryParam "limit" Int
72 :> Get '[JSON] [Node Value]
73 :<|> "facet" :> Summary " Facet documents"
74 :> "documents" :> FacetDocAPI
75 -- :<|> "facet" :<|> "sources" :<|> FacetSourcesAPI
76 -- :<|> "facet" :<|> "authors" :<|> FacetAuthorsAPI
77 -- :<|> "facet" :<|> "terms" :<|> FacetTermsAPI
79 --data FacetFormat = Table | Chart
80 --data FacetType = Doc | Term | Source | Author
81 --data Facet = Facet Doc Format
84 type FacetDocAPI = "table"
85 :> Summary " Table data"
86 :> QueryParam "offset" Int
87 :> QueryParam "limit" Int
88 :> Get '[JSON] [FacetDoc]
91 :> Summary " Chart data"
92 :> QueryParam "from" UTCTime
93 :> QueryParam "to" UTCTime
94 :> Get '[JSON] [FacetChart]
96 -- Depending on the Type of the Node, we could post
97 -- New documents for a corpus
99 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
101 -- To launch a query and update the corpus
102 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
105 -- | Node API functions
106 roots :: Connection -> Server Roots
107 roots conn = liftIO (putStrLn ( "/user" :: Text) >> getNodesWithParentId conn 0 Nothing)
108 :<|> pure (panic "not implemented yet")
109 :<|> pure (panic "not implemented yet")
110 :<|> pure (panic "not implemented yet")
113 type GraphAPI = Get '[JSON] Graph
114 graphAPI :: Connection -> NodeId -> Server GraphAPI
115 graphAPI _ _ = liftIO $ textFlow (Mono EN) (Contexts contextText)
117 type TreeAPI = Get '[JSON] (Tree NodeTree)
118 treeAPI :: Connection -> NodeId -> Server TreeAPI
119 treeAPI _ _ = undefined
122 nodeAPI :: Connection -> NodeId -> Server NodeAPI
123 nodeAPI conn id = liftIO (putStrLn ("/node" :: Text) >> getNode conn id )
124 :<|> postNode conn id
126 :<|> deleteNode' conn id
127 :<|> getNodesWith' conn id
128 :<|> getFacet conn id
129 :<|> getChart conn id
133 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
134 nodesAPI conn ids = deleteNodes' conn ids
136 postNode :: Connection -> NodeId -> Handler Int
139 putNode :: Connection -> NodeId -> Handler Int
142 deleteNodes' :: Connection -> [NodeId] -> Handler Int
143 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
145 deleteNode' :: Connection -> NodeId -> Handler Int
146 deleteNode' conn id = liftIO (deleteNode conn id)
148 getNodesWith' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
149 -> Handler [Node Value]
150 getNodesWith' conn id nodeType offset limit = liftIO (getNodesWith conn id nodeType offset limit)
153 getFacet :: Connection -> NodeId -> Maybe Int -> Maybe Int
154 -> Handler [FacetDoc]
155 getFacet conn id offset limit = liftIO (putStrLn ( "/facet" :: Text)) >> liftIO (getDocFacet conn NodeCorpus id (Just Document) offset limit)
157 getChart :: Connection -> NodeId -> Maybe UTCTime -> Maybe UTCTime
158 -> Handler [FacetChart]
159 getChart _ _ _ _ = undefined
162 query :: Text -> Handler Text
167 -- TODO Is it possible to adapt the function according to iValue input ?
168 --upload :: MultipartData -> Handler Text
169 --upload multipartData = do
171 -- putStrLn "Inputs:"
172 -- forM_ (inputs multipartData) $ \input ->
173 -- putStrLn $ " " <> show (iName input)
174 -- <> " -> " <> show (iValue input)
176 -- forM_ (files multipartData) $ \file -> do
177 -- content <- readFile (fdFilePath file)
178 -- putStrLn $ "Content of " <> show (fdFileName file)
179 -- <> " at " <> fdFilePath file
181 -- pure (pack "Data loaded")