]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node.hs
[REST] Tree.
[gargantext.git] / src / Gargantext / API / Node.hs
1 {-|
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
8 Portability : POSIX
9
10 Node API
11 -}
12
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE OverloadedStrings #-}
20
21 -------------------------------------------------------------------
22 module Gargantext.API.Node
23 where
24 -------------------------------------------------------------------
25
26 import Control.Monad.IO.Class (liftIO)
27 import Control.Monad ((>>))
28 --import System.IO (putStrLn, readFile)
29
30 import Data.Aeson (Value())
31 --import Data.Text (Text(), pack)
32 import Data.Text (Text())
33 import Data.Time (UTCTime)
34
35 import Database.PostgreSQL.Simple (Connection)
36
37 import Servant
38 -- import Servant.Multipart
39
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
46 ,FacetChart)
47
48 -- Graph
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]
58 :<|> Post '[JSON] Int
59 :<|> Put '[JSON] Int
60 :<|> Delete '[JSON] Int
61
62 type NodesAPI = Delete '[JSON] Int
63
64 type NodeAPI = Get '[JSON] (Node Value)
65 :<|> Post '[JSON] Int
66 :<|> Put '[JSON] Int
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
78
79 --data FacetFormat = Table | Chart
80 --data FacetType = Doc | Term | Source | Author
81 --data Facet = Facet Doc Format
82
83
84 type FacetDocAPI = "table"
85 :> Summary " Table data"
86 :> QueryParam "offset" Int
87 :> QueryParam "limit" Int
88 :> Get '[JSON] [FacetDoc]
89
90 :<|> "chart"
91 :> Summary " Chart data"
92 :> QueryParam "from" UTCTime
93 :> QueryParam "to" UTCTime
94 :> Get '[JSON] [FacetChart]
95
96 -- Depending on the Type of the Node, we could post
97 -- New documents for a corpus
98 -- New map list terms
99 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
100
101 -- To launch a query and update the corpus
102 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
103
104
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")
111
112
113 type GraphAPI = Get '[JSON] Graph
114 graphAPI :: Connection -> NodeId -> Server GraphAPI
115 graphAPI _ _ = liftIO $ textFlow (Mono EN) (Contexts contextText)
116
117 type TreeAPI = Get '[JSON] (Tree NodeTree)
118 treeAPI :: Connection -> NodeId -> Server TreeAPI
119 treeAPI _ _ = undefined
120
121
122 nodeAPI :: Connection -> NodeId -> Server NodeAPI
123 nodeAPI conn id = liftIO (putStrLn ("/node" :: Text) >> getNode conn id )
124 :<|> postNode conn id
125 :<|> putNode conn id
126 :<|> deleteNode' conn id
127 :<|> getNodesWith' conn id
128 :<|> getFacet conn id
129 :<|> getChart conn id
130 -- :<|> upload
131 -- :<|> query
132
133 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
134 nodesAPI conn ids = deleteNodes' conn ids
135
136 postNode :: Connection -> NodeId -> Handler Int
137 postNode = undefined
138
139 putNode :: Connection -> NodeId -> Handler Int
140 putNode = undefined
141
142 deleteNodes' :: Connection -> [NodeId] -> Handler Int
143 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
144
145 deleteNode' :: Connection -> NodeId -> Handler Int
146 deleteNode' conn id = liftIO (deleteNode conn id)
147
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)
151
152
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)
156
157 getChart :: Connection -> NodeId -> Maybe UTCTime -> Maybe UTCTime
158 -> Handler [FacetChart]
159 getChart _ _ _ _ = undefined
160
161
162 query :: Text -> Handler Text
163 query s = pure s
164
165
166 -- | Upload files
167 -- TODO Is it possible to adapt the function according to iValue input ?
168 --upload :: MultipartData -> Handler Text
169 --upload multipartData = do
170 -- liftIO $ do
171 -- putStrLn "Inputs:"
172 -- forM_ (inputs multipartData) $ \input ->
173 -- putStrLn $ " " <> show (iName input)
174 -- <> " -> " <> show (iValue input)
175 --
176 -- forM_ (files multipartData) $ \file -> do
177 -- content <- readFile (fdFilePath file)
178 -- putStrLn $ "Content of " <> show (fdFileName file)
179 -- <> " at " <> fdFilePath file
180 -- putStrLn content
181 -- pure (pack "Data loaded")
182