gonito/Handler/SubmissionView.hs

60 lines
2.2 KiB
Haskell
Raw Normal View History

2017-02-25 19:13:55 +01:00
module Handler.SubmissionView where
import Import
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
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,
2018-11-12 20:41:46 +01:00
fsiTags :: [(Entity Tag, Entity SubmissionTag)],
2018-11-17 13:49:44 +01:00
fsiExternalLinks :: [Entity ExternalLink],
fsiSuperSubmissions :: [FullSubmissionInfo] }
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 }
2018-11-17 13:49:44 +01:00
getTags :: (BaseBackend backend ~ SqlBackend, MonadIO m, PersistQueryRead backend) => Key Submission -> ReaderT backend m [(Entity 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