]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node.hs
[SPECS/DOC] Adding Swagger Documentation, first draft POC.
[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 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE OverloadedStrings #-}
18
19 -------------------------------------------------------------------
20 module Gargantext.API.Node
21 where
22 -------------------------------------------------------------------
23
24 import Control.Monad.IO.Class (liftIO)
25 --import System.IO (putStrLn, readFile)
26
27 -- import Data.Aeson (Value())
28 --import Data.Text (Text(), pack)
29 import Data.Text (Text())
30
31 import Database.PostgreSQL.Simple (Connection)
32
33 import Servant
34 -- import Servant.Multipart
35
36 import Gargantext.Prelude
37 import Gargantext.Types.Node
38 import Gargantext.Database.Node (getNodesWithParentId
39 , getNode, getNodesWith
40 , deleteNode, deleteNodes)
41 import Gargantext.Database.Facet (FacetDoc, getDocFacet)
42
43 -------------------------------------------------------------------
44 -------------------------------------------------------------------
45
46
47 -- | Node API Types management
48 type Roots = Get '[JSON] [Node HyperdataDocument]
49
50 type NodesAPI = Delete '[JSON] Int
51
52 type NodeAPI = Get '[JSON] (Node HyperdataDocument)
53 :<|> Delete '[JSON] Int
54
55 :<|> "children" :> QueryParam "type" NodeType
56 :> QueryParam "offset" Int
57 :> QueryParam "limit" Int
58 :> Get '[JSON] [Node HyperdataDocument]
59
60
61 :<|> "facet" :> QueryParam "type" NodeType
62 :> QueryParam "offset" Int
63 :> QueryParam "limit" Int
64 :> Get '[JSON] [FacetDoc]
65
66
67 -- Depending on the Type of the Node, we could post
68 -- New documents for a corpus
69 -- New map list terms
70 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
71
72 -- To launch a query and update the corpus
73 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
74
75
76
77 -- | Node API functions
78 roots :: Connection -> Server Roots
79 roots conn = liftIO (getNodesWithParentId conn 0 Nothing)
80
81 nodeAPI :: Connection -> NodeId -> Server NodeAPI
82 nodeAPI conn id = liftIO (getNode conn id)
83 :<|> deleteNode' conn id
84 :<|> getNodesWith' conn id
85 :<|> getDocFacet' conn id
86 -- :<|> upload
87 -- :<|> query
88
89 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
90 nodesAPI conn ids = deleteNodes' conn ids
91
92 deleteNodes' :: Connection -> [NodeId] -> Handler Int
93 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
94
95 deleteNode' :: Connection -> NodeId -> Handler Int
96 deleteNode' conn id = liftIO (deleteNode conn id)
97
98 getNodesWith' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
99 -> Handler [Node HyperdataDocument]
100 getNodesWith' conn id nodeType offset limit = liftIO (getNodesWith conn id nodeType offset limit)
101
102 getDocFacet' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
103 -> Handler [FacetDoc]
104 getDocFacet' conn id nodeType offset limit = liftIO (getDocFacet conn id nodeType offset limit)
105
106 query :: Text -> Handler Text
107 query s = pure s
108
109
110 -- | Upload files
111 -- TODO Is it possible to adapt the function according to iValue input ?
112 --upload :: MultipartData -> Handler Text
113 --upload multipartData = do
114 -- liftIO $ do
115 -- putStrLn "Inputs:"
116 -- forM_ (inputs multipartData) $ \input ->
117 -- putStrLn $ " " <> show (iName input)
118 -- <> " -> " <> show (iValue input)
119 --
120 -- forM_ (files multipartData) $ \file -> do
121 -- content <- readFile (fdFilePath file)
122 -- putStrLn $ "Content of " <> show (fdFileName file)
123 -- <> " at " <> fdFilePath file
124 -- putStrLn content
125 -- pure (pack "Data loaded")
126