<commit_before>{-# LANGUAGE TemplateHaskell
           , NamedFieldPuns
   #-}
module Handler.Search
       ( getSearchR
       ) where

import Import
import Data.List as L
import Data.Text as T
import Database.Esqueleto as S
import Widget


getSearchString :: Handler (Maybe Text)
getSearchString = do
  YesodRequest {reqGetParams} <- getRequest
  case L.lookup searchQueryParamName reqGetParams of
    Nothing  -> return Nothing
    Just str
      | T.null str -> return Nothing
      |  otherwise -> return (Just str)

searchReleasesT :: Text -> YesodDB App [Entity Release]
searchReleasesT qstr =
  S.select $
  S.from $ \ release -> do
    let pattern = "%" <> qstr <> "%"
    S.where_  (release ^. ReleaseName `like` val pattern)
    return release

getSearchJson :: [Release] -> Handler Json
getSearchJson releases = return $ toJSON releases

getSearchHtml :: Maybe Text -> [Release] -> Handler Html
getSearchHtml msearchString releases = defaultLayout (searchPage msearchString releases)

-- TODO feed for search string

getSearchR :: Handler TypedContent
getSearchR = do
  msearchString <- getSearchString
  releases      <- fmap entityVal <$> do
    maybe (pure []) (runDB . searchReleasesT) msearchString

  selectRep $ do
    provideRep $ getSearchHtml msearchString releases
    provideRep $ getSearchJson               releases<commit_msg>Use lookupGetParam instead of list lookup<commit_after>{-# LANGUAGE TemplateHaskell
           , NamedFieldPuns
   #-}
module Handler.Search
       ( getSearchR
       ) where

import Import
import Data.List as L
import Data.Text as T
import Database.Esqueleto as S
import Widget


getSearchString :: Handler (Maybe Text)
getSearchString = do
  mq <- lookupGetParam searchQueryParamName
  case mq of
    Nothing -> return Nothing
    Just q
      | T.null q  -> return Nothing
      | otherwise -> return (Just q)

searchReleasesT :: Text -> YesodDB App [Entity Release]
searchReleasesT qstr =
  S.select $
  S.from $ \ release -> do
    let pattern = "%" <> qstr <> "%"
    S.where_  (release ^. ReleaseName `like` val pattern)
    return release

getSearchJson :: [Release] -> Handler Json
getSearchJson releases = return $ toJSON releases

getSearchHtml :: Maybe Text -> [Release] -> Handler Html
getSearchHtml msearchString releases = defaultLayout (searchPage msearchString releases)

-- TODO feed for search string

getSearchR :: Handler TypedContent
getSearchR = do
  msearchString <- getSearchString
  releases      <- fmap entityVal <$> do
    maybe (pure []) (runDB . searchReleasesT) msearchString

  selectRep $ do
    provideRep $ getSearchHtml msearchString releases
    provideRep $ getSearchJson               releases
{'fraction_non_alphanumeric': 0.08075921101600297, 'fraction_numerical': 0.0, 'mean_word_length': 3.2802547770700636, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '460836', 'n_tokens_mistral': 786, 'n_tokens_neox': 758, 'n_words': 254}