import Data.Swagger
import Data.Text (Text())
import GHC.Generics (Generic)
-import Gargantext.API.Ngrams (TabType(..))
+import Servant
+import Test.QuickCheck (elements)
+import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
+
+import Gargantext.API.HashedResponse
+import Gargantext.API.Ngrams.Types (TabType(..))
+import Gargantext.API.Prelude (GargServer)
import Gargantext.Core.Types (Offset, Limit, TableResult(..))
import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
-import Gargantext.Database.Query.Facet (FacetDoc , runViewDocuments, OrderBy(..), runViewAuthorsDoc)
import Gargantext.Database.Action.Learn (FavOrTrash(..), moreLike)
import Gargantext.Database.Action.Search
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Prelude -- (Cmd, CmdM)
+import Gargantext.Database.Query.Facet (FacetDoc , runViewDocuments, runCountDocuments, OrderBy(..), runViewAuthorsDoc)
import Gargantext.Prelude
-import Servant
-import Test.QuickCheck (elements)
-import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
------------------------------------------------------------------------
-type TableApi = Summary " Table API"
+type TableApi = Summary "Table API"
+ :> QueryParam "tabType" TabType
+ :> QueryParam "list" ListId
+ :> QueryParam "limit" Int
+ :> QueryParam "offset" Int
+ :> QueryParam "orderBy" OrderBy
+ :> QueryParam "query" Text
+ :> Get '[JSON] (HashedResponse FacetTableResult)
+ :<|> Summary "Table API (POST)"
:> ReqBody '[JSON] TableQuery
:> Post '[JSON] FacetTableResult
+ :<|> "hash" :>
+ Summary "Hash Table"
+ :> QueryParam "tabType" TabType
+ :> Get '[JSON] Text
data TableQuery = TableQuery
{ tq_offset :: Int
, tq_limit :: Int
, tq_orderBy :: OrderBy
, tq_view :: TabType
- , tq_query :: Text
+ , tq_query :: Text
} deriving (Generic)
type FacetTableResult = TableResult FacetDoc
arbitrary = elements [TableQuery 0 10 DateAsc Docs "electrodes"]
-tableApi :: NodeId -> TableQuery -> Cmd err FacetTableResult
-tableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order)
-tableApi cId (TableQuery o l order ft q) = case ft of
+tableApi :: NodeId -> GargServer TableApi
+tableApi id' = getTableApi id'
+ :<|> postTableApi id'
+ :<|> getTableHashApi id'
+
+
+getTableApi :: NodeId
+ -> Maybe TabType
+ -> Maybe ListId
+ -> Maybe Int
+ -> Maybe Int
+ -> Maybe OrderBy
+ -> Maybe Text
+ -> Cmd err (HashedResponse FacetTableResult)
+getTableApi cId tabType _mListId mLimit mOffset mOrderBy mQuery = do
+ printDebug "[getTableApi] mQuery" mQuery
+ t <- getTable cId tabType mOffset mLimit mOrderBy mQuery
+ pure $ constructHashedResponse t
+
+postTableApi :: NodeId -> TableQuery -> Cmd err FacetTableResult
+postTableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order) Nothing
+postTableApi cId (TableQuery o l order ft q) = case ft of
Docs -> searchInCorpus' cId False [q] (Just o) (Just l) (Just order)
Trash -> searchInCorpus' cId True [q] (Just o) (Just l) (Just order)
x -> panic $ "not implemented in tableApi " <> (cs $ show x)
+getTableHashApi :: NodeId -> Maybe TabType -> Cmd err Text
+getTableHashApi cId tabType = do
+ HashedResponse { hash = h } <- getTableApi cId tabType Nothing Nothing Nothing Nothing Nothing
+ pure h
+
searchInCorpus' :: CorpusId
-> Bool
-> [Text]
-> Maybe OrderBy
-> Cmd err FacetTableResult
searchInCorpus' cId t q o l order = do
- docs <- searchInCorpus cId t q o l order
+ docs <- searchInCorpus cId t q o l order
countAllDocs <- searchCountInCorpus cId t q
pure $ TableResult { tr_docs = docs, tr_count = countAllDocs }
-getTable :: NodeId -> Maybe TabType
- -> Maybe Offset -> Maybe Limit
- -> Maybe OrderBy -> Cmd err FacetTableResult
-getTable cId ft o l order = do
- docs <- getTable' cId ft o l order
- -- TODO: Rewrite to use runCountOpaQuery and avoid (length allDocs)
- allDocs <- getTable' cId ft Nothing Nothing Nothing
- pure $ TableResult { tr_docs = docs, tr_count = length allDocs }
-
-getTable' :: NodeId -> Maybe TabType
- -> Maybe Offset -> Maybe Limit
- -> Maybe OrderBy -> Cmd err [FacetDoc]
-getTable' cId ft o l order =
+getTable :: NodeId
+ -> Maybe TabType
+ -> Maybe Offset
+ -> Maybe Limit
+ -> Maybe OrderBy
+ -> Maybe Text
+ -> Cmd err FacetTableResult
+getTable cId ft o l order query = do
+ docs <- getTable' cId ft o l order query
+ docsCount <- runCountDocuments cId (ft == Just Trash) query
+ pure $ TableResult { tr_docs = docs, tr_count = docsCount }
+
+getTable' :: NodeId
+ -> Maybe TabType
+ -> Maybe Offset
+ -> Maybe Limit
+ -> Maybe OrderBy
+ -> Maybe Text
+ -> Cmd err [FacetDoc]
+getTable' cId ft o l order query =
case ft of
- (Just Docs) -> runViewDocuments cId False o l order
- (Just Trash) -> runViewDocuments cId True o l order
+ (Just Docs) -> runViewDocuments cId False o l order query
+ (Just Trash) -> runViewDocuments cId True o l order query
(Just MoreFav) -> moreLike cId o l order IsFav
(Just MoreTrash) -> moreLike cId o l order IsTrash
x -> panic $ "not implemented in getTable: " <> (cs $ show x)