]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node.hs
[PHYLO] minimal example until FIS and clean files (hastext old repo).
[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 :<|> Delete '[JSON] Int
66 :<|> "children" :> Summary " Summary children"
67 :> QueryParam "type" NodeType
68 :> QueryParam "offset" Int
69 :> QueryParam "limit" Int
70 :> Get '[JSON] [Node Value]
71 :<|> "facet" :> Summary " Facet documents"
72 :> "documents" :> FacetDocAPI
73 -- :<|> "facet" :<|> "sources" :<|> FacetSourcesAPI
74 -- :<|> "facet" :<|> "authors" :<|> FacetAuthorsAPI
75 -- :<|> "facet" :<|> "terms" :<|> FacetTermsAPI
76
77 --data FacetFormat = Table | Chart
78 --data FacetType = Doc | Term | Source | Author
79 --data Facet = Facet Doc Format
80
81
82 type FacetDocAPI = "table"
83 :> Summary " Table data"
84 :> QueryParam "offset" Int
85 :> QueryParam "limit" Int
86 :> Get '[JSON] [FacetDoc]
87
88 :<|> "chart"
89 :> Summary " Chart data"
90 :> QueryParam "from" UTCTime
91 :> QueryParam "to" UTCTime
92 :> Get '[JSON] [FacetChart]
93
94 -- Depending on the Type of the Node, we could post
95 -- New documents for a corpus
96 -- New map list terms
97 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
98
99 -- To launch a query and update the corpus
100 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
101
102
103 -- | Node API functions
104 roots :: Connection -> Server Roots
105 roots conn = liftIO (putStrLn ( "/user" :: Text) >> getNodesWithParentId conn 0 Nothing)
106 :<|> pure (panic "not implemented yet")
107 :<|> pure (panic "not implemented yet")
108 :<|> pure (panic "not implemented yet")
109
110
111 type GraphAPI = Get '[JSON] Graph
112 graphAPI :: Connection -> NodeId -> Server GraphAPI
113 graphAPI _ _ = liftIO $ textFlow (Mono EN) (Contexts contextText)
114
115 type TreeAPI = Get '[JSON] (Tree NodeTree)
116 treeAPI :: Connection -> NodeId -> Server TreeAPI
117 treeAPI _ _ = undefined
118
119
120 nodeAPI :: Connection -> NodeId -> Server NodeAPI
121 nodeAPI conn id = liftIO (putStrLn ("/node" :: Text) >> getNode conn id )
122 :<|> deleteNode' conn id
123 :<|> getNodesWith' conn id
124 :<|> getFacet conn id
125 :<|> getChart conn id
126 -- :<|> upload
127 -- :<|> query
128
129
130
131 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
132 nodesAPI conn ids = deleteNodes' conn ids
133
134 deleteNodes' :: Connection -> [NodeId] -> Handler Int
135 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
136
137 deleteNode' :: Connection -> NodeId -> Handler Int
138 deleteNode' conn id = liftIO (deleteNode conn id)
139
140 getNodesWith' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
141 -> Handler [Node Value]
142 getNodesWith' conn id nodeType offset limit = liftIO (getNodesWith conn id nodeType offset limit)
143
144
145 getFacet :: Connection -> NodeId -> Maybe Int -> Maybe Int
146 -> Handler [FacetDoc]
147 getFacet conn id offset limit = liftIO (putStrLn ( "/facet" :: Text)) >> liftIO (getDocFacet conn NodeCorpus id (Just Document) offset limit)
148
149 getChart :: Connection -> NodeId -> Maybe UTCTime -> Maybe UTCTime
150 -> Handler [FacetChart]
151 getChart _ _ _ _ = undefined
152
153
154 query :: Text -> Handler Text
155 query s = pure s
156
157
158 -- | Upload files
159 -- TODO Is it possible to adapt the function according to iValue input ?
160 --upload :: MultipartData -> Handler Text
161 --upload multipartData = do
162 -- liftIO $ do
163 -- putStrLn "Inputs:"
164 -- forM_ (inputs multipartData) $ \input ->
165 -- putStrLn $ " " <> show (iName input)
166 -- <> " -> " <> show (iValue input)
167 --
168 -- forM_ (files multipartData) $ \file -> do
169 -- content <- readFile (fdFilePath file)
170 -- putStrLn $ "Content of " <> show (fdFileName file)
171 -- <> " at " <> fdFilePath file
172 -- putStrLn content
173 -- pure (pack "Data loaded")
174