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 #-}
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE OverloadedStrings #-}
19 module Gargantext.API.Node
22 import Control.Monad.IO.Class (liftIO)
23 import Data.Aeson (Value())
25 -- import Servant.Multipart
26 --import System.IO (putStrLn, readFile)
27 import Data.Text (Text())
28 --import Data.Text (Text(), pack)
29 import Database.PostgreSQL.Simple (Connection)
30 import Gargantext.Prelude
31 import Gargantext.Types.Node
33 import Gargantext.Database.Node (getNodesWithParentId
34 , getNode, getNodesWith
35 , deleteNode, deleteNodes)
36 import Gargantext.Database.Facet (FacetDoc, getDocFacet)
40 -- | Node API Types management
41 type Roots = Get '[JSON] [Node Value]
43 type NodesAPI = Delete '[JSON] Int
45 type NodeAPI = Get '[JSON] (Node Value)
46 :<|> Delete '[JSON] Int
48 :<|> "children" :> QueryParam "type" NodeType
49 :> QueryParam "offset" Int
50 :> QueryParam "limit" Int
51 :> Get '[JSON] [Node Value]
54 :<|> "facet" :> QueryParam "type" NodeType
55 :> QueryParam "offset" Int
56 :> QueryParam "limit" Int
57 :> Get '[JSON] [FacetDoc]
60 -- Depending on the Type of the Node, we could post
61 -- New documents for a corpus
63 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
65 -- To launch a query and update the corpus
66 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
70 -- | Node API functions
71 roots :: Connection -> Server Roots
72 roots conn = liftIO (getNodesWithParentId conn 0 Nothing)
74 nodeAPI :: Connection -> NodeId -> Server NodeAPI
75 nodeAPI conn id = liftIO (getNode conn id)
76 :<|> deleteNode' conn id
77 :<|> getNodesWith' conn id
78 :<|> getDocFacet' conn id
82 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
83 nodesAPI conn ids = deleteNodes' conn ids
85 deleteNodes' :: Connection -> [NodeId] -> Handler Int
86 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
88 deleteNode' :: Connection -> NodeId -> Handler Int
89 deleteNode' conn id = liftIO (deleteNode conn id)
91 getNodesWith' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
92 -> Handler [Node Value]
93 getNodesWith' conn id nodeType offset limit = liftIO (getNodesWith conn id nodeType offset limit)
95 getDocFacet' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
97 getDocFacet' conn id nodeType offset limit = liftIO (getDocFacet conn id nodeType offset limit)
99 query :: Text -> Handler Text
104 -- TODO Is it possible to adapt the function according to iValue input ?
105 --upload :: MultipartData -> Handler Text
106 --upload multipartData = do
108 -- putStrLn "Inputs:"
109 -- forM_ (inputs multipartData) $ \input ->
110 -- putStrLn $ " " <> show (iName input)
111 -- <> " -> " <> show (iValue input)
113 -- forM_ (files multipartData) $ \file -> do
114 -- content <- readFile (fdFilePath file)
115 -- putStrLn $ "Content of " <> show (fdFileName file)
116 -- <> " at " <> fdFilePath file
118 -- pure (pack "Data loaded")