gonito/Handler/SubmissionView.hs

89 lines
3.2 KiB
Haskell
Raw Normal View History

2017-02-25 19:13:55 +01:00
module Handler.SubmissionView where
2021-02-15 12:51:24 +01:00
import Import hiding (fromList)
2017-02-25 19:13:55 +01:00
2018-11-17 13:49:44 +01:00
import qualified Database.Esqueleto as E
import Database.Esqueleto ((^.))
2018-11-12 14:12:51 +01:00
2021-02-15 12:51:24 +01:00
import Handler.Shared
import PersistSHA1
import Data.Swagger hiding (get)
import Control.Lens hiding ((.=), (^.))
import Data.Proxy as DPR
import Data.HashMap.Strict.InsOrd (fromList)
2017-02-25 19:13:55 +01:00
data FullSubmissionInfo = FullSubmissionInfo {
fsiSubmissionId :: SubmissionId,
fsiSubmission :: Submission,
fsiUser :: User,
fsiRepo :: Repo,
fsiChallenge :: Challenge,
fsiChallengeRepo :: Repo,
2018-11-10 11:20:17 +01:00
fsiScheme :: RepoScheme,
2021-02-15 12:51:24 +01:00
fsiTags :: [(Entity Import.Tag, Entity SubmissionTag)],
2018-11-17 13:49:44 +01:00
fsiExternalLinks :: [Entity ExternalLink],
fsiSuperSubmissions :: [FullSubmissionInfo] }
2017-02-25 19:13:55 +01:00
2021-02-15 12:51:24 +01:00
instance ToJSON FullSubmissionInfo where
toJSON entry = object
[ "hash" .= (fromSHA1ToText $ submissionCommit $ fsiSubmission entry),
"submitter" .= (formatSubmitter $ fsiUser entry),
"challenge" .= (challengeName $ fsiChallenge entry)
]
instance ToSchema FullSubmissionInfo where
declareNamedSchema _ = do
stringSchema <- declareSchemaRef (DPR.Proxy :: DPR.Proxy String)
return $ NamedSchema (Just "SubmissionInfo") $ mempty
& type_ .~ SwaggerObject
& properties .~
fromList [ ("hash", stringSchema)
, ("submitter", stringSchema)
, ("challenge", stringSchema)
]
& required .~ [ "hash", "submitter", "challenge" ]
2017-02-25 19:13:55 +01:00
getFullInfo :: Entity Submission -> Handler FullSubmissionInfo
getFullInfo (Entity submissionId submission) = do
repo <- runDB $ get404 $ submissionRepo submission
user <- runDB $ get404 $ submissionSubmitter submission
challenge <- runDB $ get404 $ submissionChallenge submission
challengeRepo <- runDB $ get404 $ challengePublicRepo challenge
2018-11-10 11:20:17 +01:00
tags <- runDB $ getTags submissionId
2018-11-12 20:41:46 +01:00
links <- runDB $ selectList [ExternalLinkSubmission ==. submissionId] [Asc ExternalLinkTitle]
app <- getYesod
let scheme = appRepoScheme $ appSettings app
2018-11-17 13:49:44 +01:00
superSubmissions <- runDB $ E.select $ E.from $ \(submission', dependency) -> do
E.where_ (submission' ^. SubmissionCommit E.==. dependency ^. DependencySuperRepoCommit
E.&&. dependency ^. DependencySubRepoCommit E.==. (E.val (submissionCommit submission)))
return submission'
superSubmissionFsis <- mapM getFullInfo superSubmissions
2017-02-25 19:13:55 +01:00
return $ FullSubmissionInfo {
fsiSubmissionId = submissionId,
fsiSubmission = submission,
fsiUser = user,
fsiRepo = repo,
fsiChallenge = challenge,
fsiChallengeRepo = challengeRepo,
2018-11-10 11:20:17 +01:00
fsiScheme = scheme,
2018-11-12 20:41:46 +01:00
fsiTags = tags,
2018-11-17 13:49:44 +01:00
fsiExternalLinks = links,
fsiSuperSubmissions = superSubmissionFsis }
2021-02-15 12:51:24 +01:00
getTags :: (BaseBackend backend ~ SqlBackend, MonadIO m, PersistQueryRead backend) => Key Submission -> ReaderT backend m [(Entity Import.Tag, Entity SubmissionTag)]
2017-02-25 19:13:55 +01:00
getTags submissionId = do
sts <- selectList [SubmissionTagSubmission ==. submissionId] []
2017-02-25 22:53:17 +01:00
let tagIds = Import.map (submissionTagTag . entityVal) sts
tags <- mapM get404 $ tagIds
2017-05-27 22:31:53 +02:00
let tagEnts = Import.map (\(k, v) -> Entity k v) $ Import.zip tagIds tags
return $ zip tagEnts sts