1 {-# LANGUAGE OverloadedStrings, TupleSections #-} 2 module Controller.Search 3 ( postSearchHandler 4 ) where 5 6 import Control.Monad (when) 7 import Data.Maybe (fromMaybe) 8 9 import Has 10 import Model.Id.Types 11 import Model.Metric 12 import Solr.Search 13 import Action.Response 14 import Action 15 import HTTP.Form (FormKey(..)) 16 import HTTP.Form.Deform 17 import Controller.Form 18 import Controller.Angular 19 20 searchForm :: DeformHandler f SearchQuery 21 searchForm = SearchQuery 22 <$> ("q" .:> deformNonEmpty deform) 23 <*> ("f" .:> withSubDeforms (\k -> (view k, ) <$> deform)) 24 <*> ("m" .:> withSubDeforms (\k -> (,) 25 <$> either deformError' return (maybe (Left "Metric ID not found") Right . getMetric . Id =<< case k of 26 FormField t -> textInteger t 27 FormIndex i -> Right (fromIntegral i)) 28 <*> deform)) 29 <*> ("volume" .:> fromMaybe SearchVolumes <$> deformOptional (sv <$> deform)) 30 <*> paginateForm 31 where 32 sv False = SearchParties 33 sv True = SearchVolumes 34 35 postSearchHandler :: API -> Action -- TODO: GET only 36 postSearchHandler = \api -> withAuth $ do 37 when (api == HTML) angular 38 q <- runForm Nothing searchForm 39 proxyResponse <$> search q