2 Module : Gargantext.API.Count
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Count API part of Gargantext.
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE DeriveAnyClass #-}
19 module Gargantext.API.Search
22 import Data.Aeson.TH (deriveJSON)
24 import Data.Text (Text)
25 import Data.Time (UTCTime)
26 import GHC.Generics (Generic)
27 import Gargantext.API.Prelude (GargServer)
28 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
29 import Gargantext.Database.Query.Facet
30 import Gargantext.Database.Action.Search
31 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataContact)
32 import Gargantext.Database.Admin.Types.Node
33 import Gargantext.Prelude
35 import Test.QuickCheck (elements)
36 import Test.QuickCheck.Arbitrary
38 -----------------------------------------------------------------------
39 data SearchType = SearchDoc | SearchContact
42 $(deriveJSON (unPrefix "") ''SearchType)
43 instance ToSchema SearchType
44 instance Arbitrary SearchType where
45 arbitrary = elements [SearchDoc, SearchContact]
47 -----------------------------------------------------------------------
48 data SearchQuery = SearchQuery
50 , sq_type :: SearchType
53 $(deriveJSON (unPrefix "sq_") ''SearchQuery)
55 instance ToSchema SearchQuery where
56 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "sq_")
58 instance Arbitrary SearchQuery where
59 arbitrary = elements [SearchQuery ["electrodes"] SearchDoc]
60 -----------------------------------------------------------------------
62 data SearchResult = SearchResultDoc { sr_result :: [FacetDoc]}
63 | SearchResultContact { sr_results :: [FacetPaired Int UTCTime HyperdataContact Int] } | SearchNoResult { sr_message :: Text }
66 $(deriveJSON (unPrefix "sr_") ''SearchResult)
68 instance Arbitrary SearchResult where
70 srd <- SearchResultDoc <$> arbitrary
71 src <- SearchResultContact <$> arbitrary
72 srn <- pure $ SearchNoResult "No result because.."
73 elements [srd, src, srn]
75 instance ToSchema SearchResult where
76 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "sr_")
78 -----------------------------------------------------------------------
79 -- TODO-ACCESS: CanSearch? or is it part of CanGetNode
80 -- TODO-EVENTS: No event, this is a read-only query.
81 type API results = Summary "Search endpoint"
82 :> ReqBody '[JSON] SearchQuery
83 :> QueryParam "offset" Int
84 :> QueryParam "limit" Int
85 :> QueryParam "order" OrderBy
86 :> Post '[JSON] results
87 -----------------------------------------------------------------------
88 api :: NodeId -> GargServer (API SearchResult)
89 api nId (SearchQuery q SearchDoc) o l order =
90 SearchResultDoc <$> searchInCorpus nId False q o l order
91 api nId (SearchQuery q SearchContact) o l order =
92 -- SearchPairedResults <$> searchInCorpusWithContacts pId aId q o l order
93 pure $ SearchNoResult "Need Implementation"
94 -----------------------------------------------------------------------