2016-02-12 13:00:33 +01:00
module Handler.Query where
import Import
2016-02-15 12:42:05 +01:00
import Handler.Shared
2017-02-25 19:13:55 +01:00
import Handler.SubmissionView
2016-02-12 13:00:33 +01:00
import PersistSHA1
import Database.Persist.Sql
import Data.Text as T ( pack )
import Yesod.Form.Bootstrap3 ( BootstrapFormLayout ( .. ) , renderBootstrap3 ,
withSmallInput )
findSubmissions :: Text -> Handler [ FullSubmissionInfo ]
findSubmissions sha1Prefix = do
2016-02-16 21:10:10 +01:00
mauthId <- maybeAuth
submissions <- runDB $ case mauthId of
Just ( Entity authId _ ) -> rawSql " SELECT ?? FROM submission WHERE (is_public OR submitter = ?) AND cast(commit as text) like ? " [ toPersistValue authId , PersistText $ " \ \ \ \ x " ++ sha1Prefix ++ " % " ]
Nothing -> rawSql " SELECT ?? FROM submission WHERE is_public AND cast(commit as text) like ? " [ PersistText $ " \ \ \ \ x " ++ sha1Prefix ++ " % " ]
2016-02-12 13:00:33 +01:00
mapM getFullInfo submissions
getQueryFormR :: Handler Html
getQueryFormR = do
( formWidget , formEnctype ) <- generateFormPost queryForm
let submission = Nothing :: Maybe Text
handlerName = " getQueryFormR " :: Text
defaultLayout $ do
aDomId <- newIdent
setTitle " Searching for submissions "
$ ( widgetFile " query-form " )
postQueryFormR :: Handler Html
postQueryFormR = do
( ( result , formWidget ) , formEnctype ) <- runFormPost queryForm
let handlerName = " postQueryFormR " :: Text
case result of
FormSuccess query -> processQuery query
_ -> defaultLayout $ do
aDomId <- newIdent
setTitle " Searching for submissions "
$ ( widgetFile " query-form " )
getQueryResultsR :: Text -> Handler Html
getQueryResultsR = processQuery
processQuery :: Text -> Handler Html
processQuery query = do
submissions <- findSubmissions query
defaultLayout $ do
setTitle " query results "
$ ( widgetFile " query-results " )
queryForm :: Form Text
queryForm = renderBootstrap3 BootstrapBasicForm $ areq textField ( fieldSettingsLabel MsgGitCommitSha1 ) Nothing