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 NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE DeriveGeneric #-}
20 {-# LANGUAGE DeriveAnyClass #-}
21 {-# LANGUAGE OverloadedStrings #-}
23 module Gargantext.API.Search
26 import GHC.Generics (Generic)
27 import Control.Monad.IO.Class (liftIO)
28 import Data.Aeson hiding (Error, fieldLabelModifier)
29 import Data.Aeson.TH (deriveJSON)
31 import Data.Text (Text)
32 import Database.PostgreSQL.Simple (Connection)
34 import Test.QuickCheck.Arbitrary
35 import Test.QuickCheck (elements)
36 -- import Control.Applicative ((<*>))
37 import Gargantext.Prelude
38 import Gargantext.Core.Utils.Prefix (unPrefix)
39 import Gargantext.Database.TextSearch
41 -----------------------------------------------------------------------
42 data SearchQuery = SearchQuery { sq_query :: [Text]
45 $(deriveJSON (unPrefix "sq_") ''SearchQuery)
46 instance ToSchema SearchQuery where
48 genericDeclareNamedSchema
49 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
51 instance Arbitrary SearchQuery where
52 arbitrary = elements [SearchQuery ["electrodes"] 472764]
54 -----------------------------------------------------------------------
55 data Author = Author { _a_name :: Text
59 $(deriveJSON (unPrefix "_a_") ''Author)
61 instance ToSchema Author where
63 genericDeclareNamedSchema
64 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
66 arbitraryAuthor :: Author
67 arbitraryAuthor = Author "Jezequel" 1011669
68 instance Arbitrary Author where
69 arbitrary = elements [arbitraryAuthor]
71 -----------------------------------------------------------------------
72 data SearchResult = SearchResult { sr_id :: Int
74 , sr_authors :: [Author]
76 $(deriveJSON (unPrefix "sr_") ''SearchResult)
77 instance Arbitrary SearchResult where
78 arbitrary = elements [SearchResult 1 "Title" [arbitraryAuthor]]
80 instance ToSchema SearchResult where
82 genericDeclareNamedSchema
83 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
85 -----------------------------------------------------------------------
87 data SearchResults = SearchResults { srs_results :: [SearchResult]}
89 $(deriveJSON (unPrefix "srs_") ''SearchResults)
91 instance Arbitrary SearchResults where
92 arbitrary = SearchResults <$> arbitrary
94 instance ToSchema SearchResults where
96 genericDeclareNamedSchema
97 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
101 -----------------------------------------------------------------------
102 type SearchAPI = Post '[JSON] SearchResults
103 -----------------------------------------------------------------------
105 search :: Connection -> SearchQuery -> Handler SearchResults
106 search c (SearchQuery q pId) =
107 liftIO $ SearchResults <$> map (\(i, _, t, _, a, _) -> SearchResult i (cs $ encode t) [arbitraryAuthor])
108 <$> textSearch c (toTSQuery q) pId 5 0 Desc