]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node.hs
[MOCK] all routes completed, builds but need to be adapted to fite the practices.
[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 module Gargantext.API.Node
20 where
21
22 import Control.Monad.IO.Class (liftIO)
23 import Data.Aeson (Value())
24 import Servant
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
32
33 import Gargantext.Database.Node (getNodesWithParentId
34 , getNode, getNodesWith
35 , deleteNode, deleteNodes)
36 import Gargantext.Database.Facet (FacetDoc, getDocFacet)
37
38
39
40 -- | Node API Types management
41 type Roots = Get '[JSON] [Node Value]
42
43 type NodesAPI = Delete '[JSON] Int
44
45 type NodeAPI = Get '[JSON] (Node Value)
46 :<|> Delete '[JSON] Int
47
48 :<|> "children" :> QueryParam "type" NodeType
49 :> QueryParam "offset" Int
50 :> QueryParam "limit" Int
51 :> Get '[JSON] [Node Value]
52
53
54 :<|> "facet" :> QueryParam "type" NodeType
55 :> QueryParam "offset" Int
56 :> QueryParam "limit" Int
57 :> Get '[JSON] [FacetDoc]
58
59
60 -- Depending on the Type of the Node, we could post
61 -- New documents for a corpus
62 -- New map list terms
63 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
64
65 -- To launch a query and update the corpus
66 -- :<|> "query" :> Capture "string" Text :> Get '[JSON] Text
67
68
69
70 -- | Node API functions
71 roots :: Connection -> Server Roots
72 roots conn = liftIO (getNodesWithParentId conn 0 Nothing)
73
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
79 -- :<|> upload
80 -- :<|> query
81
82 nodesAPI :: Connection -> [NodeId] -> Server NodesAPI
83 nodesAPI conn ids = deleteNodes' conn ids
84
85 deleteNodes' :: Connection -> [NodeId] -> Handler Int
86 deleteNodes' conn ids = liftIO (deleteNodes conn ids)
87
88 deleteNode' :: Connection -> NodeId -> Handler Int
89 deleteNode' conn id = liftIO (deleteNode conn id)
90
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)
94
95 getDocFacet' :: Connection -> NodeId -> Maybe NodeType -> Maybe Int -> Maybe Int
96 -> Handler [FacetDoc]
97 getDocFacet' conn id nodeType offset limit = liftIO (getDocFacet conn id nodeType offset limit)
98
99 query :: Text -> Handler Text
100 query s = pure s
101
102
103 -- | Upload files
104 -- TODO Is it possible to adapt the function according to iValue input ?
105 --upload :: MultipartData -> Handler Text
106 --upload multipartData = do
107 -- liftIO $ do
108 -- putStrLn "Inputs:"
109 -- forM_ (inputs multipartData) $ \input ->
110 -- putStrLn $ " " <> show (iName input)
111 -- <> " -> " <> show (iValue input)
112 --
113 -- forM_ (files multipartData) $ \file -> do
114 -- content <- readFile (fdFilePath file)
115 -- putStrLn $ "Content of " <> show (fdFileName file)
116 -- <> " at " <> fdFilePath file
117 -- putStrLn content
118 -- pure (pack "Data loaded")
119