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.Core.Types.Node
42 import Gargantext.Database.Node ( getNodesWithParentId
43 , getNode, getNodesWith
44 , deleteNode, deleteNodes)
45 import Gargantext.Database.Facet (FacetDoc, getDocFacet
48 -------------------------------------------------------------------
49 -------------------------------------------------------------------
50 -- | Node API Types management
51 type Roots = Get '[JSON] [Node Value]
54 :<|> Delete '[JSON] Int
56 type NodesAPI = Delete '[JSON] Int
58 type NodeAPI = Get '[JSON] (Node Value)
59 :<|> Delete '[JSON] Int
60 :<|> "children" :> Summary " Summary children"
61 :> QueryParam "type" NodeType
62 :> QueryParam "offset" Int
63 :> QueryParam "limit" Int
64 :> Get '[JSON] [Node Value]
65 :<|> "facet" :> Summary " Facet documents"
66 :> "documents" :> FacetDocAPI
67 -- :<|> "facet" :<|> "sources" :<|> FacetSourcesAPI
68 -- :<|> "facet" :<|> "authors" :<|> FacetAuthorsAPI
69 -- :<|> "facet" :<|> "terms" :<|> FacetTermsAPI
71 --data FacetFormat = Table | Chart
72 --data FacetType = Doc | Term | Source | Author
73 --data Facet = Facet Doc Format
76 type FacetDocAPI = "table"
77 :> Summary " Table data"
78 :> QueryParam "offset" Int
79 :> QueryParam "limit" Int
80 :> Get '[JSON] [FacetDoc]
83 :> Summary " Chart data"
84 :> QueryParam "from" UTCTime
85 :> QueryParam "to" UTCTime
86 :> Get '[JSON] [FacetChart]
88 -- Depending on the Type of the Node, we could post
89 -- New documents for a corpus
91 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
93 -- To launch a query and update the corpus
94 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
97 -- | Node API functions
98 roots :: Connection -> Server Roots
99 roots conn = liftIO (putStrLn ( "/user" :: Text) >> getNodesWithParentId conn 0 Nothing)
100 :<|> pure (panic "not implemented yet")
101 :<|> pure (panic "not implemented yet")
102 :<|> pure (panic "not implemented yet")
104 nodeAPI :: Connection -> NodeId -> Server NodeAPI
105 nodeAPI conn id = liftIO (putStrLn ("/node" :: Text) >> getNode conn id )
106 :<|> deleteNode' conn id
107 :<|> getNodesWith' conn id
108 :<|> getFacet conn id
109 :<|> getChart conn id
113 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
114 nodesAPI conn ids = deleteNodes' conn ids
116 deleteNodes' :: Connection -> [NodeId] -> Handler Int
117 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
119 deleteNode' :: Connection -> NodeId -> Handler Int
120 deleteNode' conn id = liftIO (deleteNode conn id)
122 getNodesWith' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
123 -> Handler [Node Value]
124 getNodesWith' conn id nodeType offset limit = liftIO (getNodesWith conn id nodeType offset limit)
127 getFacet :: Connection -> NodeId -> Maybe Int -> Maybe Int
128 -> Handler [FacetDoc]
129 getFacet conn id offset limit = liftIO (putStrLn ( "/facet" :: Text)) >> liftIO (getDocFacet conn NodeCorpus id (Just Document) offset limit)
131 getChart :: Connection -> NodeId -> Maybe UTCTime -> Maybe UTCTime
132 -> Handler [FacetChart]
133 getChart _ _ _ _ = undefined
136 query :: Text -> Handler Text
141 -- TODO Is it possible to adapt the function according to iValue input ?
142 --upload :: MultipartData -> Handler Text
143 --upload multipartData = do
145 -- putStrLn "Inputs:"
146 -- forM_ (inputs multipartData) $ \input ->
147 -- putStrLn $ " " <> show (iName input)
148 -- <> " -> " <> show (iValue input)
150 -- forM_ (files multipartData) $ \file -> do
151 -- content <- readFile (fdFilePath file)
152 -- putStrLn $ "Content of " <> show (fdFileName file)
153 -- <> " at " <> fdFilePath file
155 -- pure (pack "Data loaded")