2021-01-25 06:53:37 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
|
2015-09-04 23:23:32 +02:00
|
|
|
module Handler.ListChallenges where
|
|
|
|
|
2021-01-25 06:53:37 +01:00
|
|
|
import Import hiding (get, fromList, Proxy)
|
|
|
|
|
2021-02-24 14:11:30 +01:00
|
|
|
import Handler.Shared
|
|
|
|
import PersistSHA1
|
|
|
|
|
2021-01-25 06:53:37 +01:00
|
|
|
import Data.HashMap.Strict.InsOrd (fromList)
|
|
|
|
|
|
|
|
import Data.Proxy
|
|
|
|
import Control.Lens hiding ((.=))
|
|
|
|
import Data.Swagger
|
|
|
|
import Data.Swagger.Declare
|
2015-09-04 23:23:32 +02:00
|
|
|
|
2020-10-12 07:27:32 +02:00
|
|
|
mainCondition :: [Filter Challenge]
|
|
|
|
mainCondition = [ChallengeArchived !=. Just True]
|
|
|
|
|
2015-09-04 23:23:32 +02:00
|
|
|
getListChallengesR :: Handler Html
|
2020-10-12 07:27:32 +02:00
|
|
|
getListChallengesR = generalListChallenges mainCondition
|
|
|
|
|
2021-01-25 06:53:37 +01:00
|
|
|
declareListChallengesSwagger :: Declare (Definitions Schema) Swagger
|
|
|
|
declareListChallengesSwagger = do
|
|
|
|
-- param schemas
|
|
|
|
listChallengesResponse <- declareResponse (Proxy :: Proxy [Entity Challenge])
|
2021-02-15 18:27:10 +01:00
|
|
|
challengeInfoResponse <- declareResponse (Proxy :: Proxy (Entity Challenge))
|
|
|
|
let challengeNameSchema = toParamSchema (Proxy :: Proxy String)
|
2021-01-25 06:53:37 +01:00
|
|
|
|
|
|
|
return $ mempty
|
|
|
|
& paths .~
|
|
|
|
[ ("/api/list-challenges", mempty & get ?~ (mempty
|
|
|
|
& produces ?~ MimeList ["application/json"]
|
|
|
|
& description ?~ "Returns the list of all challenges"
|
2021-02-15 18:27:10 +01:00
|
|
|
& at 200 ?~ Inline listChallengesResponse)),
|
|
|
|
("/api/challenge-info/{challengeName}",
|
|
|
|
mempty & get ?~ (mempty
|
|
|
|
& parameters .~ [ Inline $ mempty
|
|
|
|
& name .~ "challengeName"
|
|
|
|
& required ?~ True
|
|
|
|
& schema .~ ParamOther (mempty
|
|
|
|
& in_ .~ ParamPath
|
|
|
|
& paramSchema .~ challengeNameSchema) ]
|
|
|
|
& produces ?~ MimeList ["application/json"]
|
|
|
|
& description ?~ "Returns metadata for a specific challenge"
|
|
|
|
& at 200 ?~ Inline challengeInfoResponse))
|
2021-01-25 06:53:37 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
listChallengesApi :: Swagger
|
|
|
|
listChallengesApi = spec & definitions .~ defs
|
|
|
|
where
|
|
|
|
(defs, spec) = runDeclare declareListChallengesSwagger mempty
|
|
|
|
|
2020-10-12 07:27:32 +02:00
|
|
|
getListChallengesJsonR :: Handler Value
|
|
|
|
getListChallengesJsonR = generalListChallengesJson mainCondition
|
2019-03-20 16:31:08 +01:00
|
|
|
|
|
|
|
getListArchivedChallengesR :: Handler Html
|
|
|
|
getListArchivedChallengesR = generalListChallenges [ChallengeArchived ==. Just True]
|
|
|
|
|
2021-02-08 20:52:12 +01:00
|
|
|
imageUrl :: Entity Challenge -> Maybe (Route App)
|
2021-05-28 07:17:45 +02:00
|
|
|
imageUrl (Entity _ challenge) =
|
2021-02-08 20:52:12 +01:00
|
|
|
case challengeImage challenge of
|
2021-05-28 07:17:45 +02:00
|
|
|
Just _ -> Just $ ChallengeImgR $ challengeName challenge
|
2021-02-08 20:52:12 +01:00
|
|
|
Nothing -> Nothing
|
|
|
|
|
2020-10-12 07:27:32 +02:00
|
|
|
instance ToJSON (Entity Challenge) where
|
2021-02-08 20:52:12 +01:00
|
|
|
toJSON chEnt@(Entity _ ch) = object
|
2020-10-15 22:27:16 +02:00
|
|
|
[ "name" .= challengeName ch
|
2020-10-12 07:27:32 +02:00
|
|
|
, "title" .= challengeTitle ch
|
|
|
|
, "description" .= challengeDescription ch
|
2020-10-15 22:27:16 +02:00
|
|
|
, "starred" .= challengeStarred ch
|
|
|
|
, "archived" .= challengeArchived ch
|
2021-02-08 20:52:12 +01:00
|
|
|
, "imageUrl" .= (("/" <>) <$> intercalate "/" <$> fst <$> renderRoute <$> imageUrl chEnt)
|
2021-02-24 14:11:30 +01:00
|
|
|
, "version" .= (fromSHA1ToText $ challengeVersion ch)
|
2020-10-12 07:27:32 +02:00
|
|
|
]
|
|
|
|
|
2021-01-25 06:53:37 +01:00
|
|
|
instance ToSchema (Entity Challenge) where
|
|
|
|
declareNamedSchema _ = do
|
|
|
|
stringSchema <- declareSchemaRef (Proxy :: Proxy String)
|
|
|
|
booleanSchema <- declareSchemaRef (Proxy :: Proxy Bool)
|
|
|
|
return $ NamedSchema (Just "Challenge") $ mempty
|
|
|
|
& type_ .~ SwaggerObject
|
|
|
|
& properties .~
|
2021-06-16 08:49:50 +02:00
|
|
|
fromList [ ("name", stringSchema)
|
2021-01-25 06:53:37 +01:00
|
|
|
, ("title", stringSchema)
|
|
|
|
, ("description", stringSchema)
|
|
|
|
, ("starred", booleanSchema)
|
|
|
|
, ("archived", booleanSchema)
|
2021-02-08 20:52:12 +01:00
|
|
|
, ("imageUrl", stringSchema)
|
2021-02-24 14:11:30 +01:00
|
|
|
, ("version", stringSchema)
|
|
|
|
]
|
|
|
|
& required .~ [ "name", "title", "description", "starred", "archived", "version" ]
|
|
|
|
|
|
|
|
|
|
|
|
declareVersionInfoSwagger :: Declare (Definitions Schema) Swagger
|
|
|
|
declareVersionInfoSwagger = do
|
|
|
|
-- param schemas
|
|
|
|
versionInfoResponse <- declareResponse (Proxy :: Proxy (Entity Version))
|
|
|
|
let versionHashSchema = toParamSchema (Proxy :: Proxy String)
|
|
|
|
|
|
|
|
return $ mempty
|
|
|
|
& paths .~
|
2021-09-02 21:13:21 +02:00
|
|
|
[ ("/api/version-info/{versionHash}",
|
2021-02-24 14:11:30 +01:00
|
|
|
mempty & get ?~ (mempty
|
|
|
|
& parameters .~ [ Inline $ mempty
|
|
|
|
& name .~ "versionHash"
|
|
|
|
& required ?~ True
|
|
|
|
& schema .~ ParamOther (mempty
|
|
|
|
& in_ .~ ParamPath
|
|
|
|
& paramSchema .~ versionHashSchema) ]
|
|
|
|
& produces ?~ MimeList ["application/json"]
|
|
|
|
& description ?~ "Returns information about a challenge version"
|
|
|
|
& at 200 ?~ Inline versionInfoResponse))
|
|
|
|
]
|
|
|
|
|
|
|
|
versionInfoApi :: Swagger
|
|
|
|
versionInfoApi = spec & definitions .~ defs
|
|
|
|
where
|
|
|
|
(defs, spec) = runDeclare declareVersionInfoSwagger mempty
|
|
|
|
|
|
|
|
|
|
|
|
instance ToJSON (Entity Version) where
|
|
|
|
toJSON chEnt@(Entity _ ver) = object
|
|
|
|
[ "deadline" .= versionDeadline ver
|
|
|
|
, "version" .= (formatVersion (versionMajor ver,
|
|
|
|
versionMinor ver,
|
|
|
|
versionPatch ver))
|
|
|
|
, "description" .= versionDescription ver
|
|
|
|
, "when" .= versionStamp ver
|
|
|
|
, "commit" .= (fromSHA1ToText $ versionCommit ver)
|
|
|
|
]
|
|
|
|
|
|
|
|
instance ToSchema (Entity Version) where
|
|
|
|
declareNamedSchema _ = do
|
|
|
|
stringSchema <- declareSchemaRef (Proxy :: Proxy String)
|
2021-05-28 07:17:45 +02:00
|
|
|
return $ NamedSchema (Just "Version") $ mempty
|
2021-02-24 14:11:30 +01:00
|
|
|
& type_ .~ SwaggerObject
|
|
|
|
& properties .~
|
|
|
|
fromList [ ("deadline", stringSchema)
|
|
|
|
, ("version", stringSchema)
|
|
|
|
, ("description", stringSchema)
|
|
|
|
, ("when", stringSchema)
|
|
|
|
, ("commit", stringSchema)
|
2021-01-25 06:53:37 +01:00
|
|
|
]
|
2021-02-24 14:11:30 +01:00
|
|
|
& required .~ [ "version", "description", "when", "commit" ]
|
2021-01-25 06:53:37 +01:00
|
|
|
|
|
|
|
|
2020-10-12 07:27:32 +02:00
|
|
|
generalListChallengesJson :: [Filter Challenge] -> Handler Value
|
|
|
|
generalListChallengesJson filterExpr = do
|
|
|
|
challenges <- getChallenges filterExpr
|
|
|
|
return $ toJSON challenges
|
|
|
|
|
2020-10-12 07:17:48 +02:00
|
|
|
generalListChallenges :: [Filter Challenge] -> Handler Html
|
2019-03-20 16:31:08 +01:00
|
|
|
generalListChallenges filterExpr = do
|
2020-10-12 07:17:48 +02:00
|
|
|
challenges <- getChallenges filterExpr
|
2015-09-04 23:23:32 +02:00
|
|
|
defaultLayout $ do
|
|
|
|
setTitle "List challenges"
|
|
|
|
$(widgetFile "list-challenges")
|
|
|
|
|
2020-10-12 07:17:48 +02:00
|
|
|
getChallenges :: [Filter Challenge] -> Handler [Entity Challenge]
|
|
|
|
getChallenges filterExpr = runDB $ selectList filterExpr [Desc ChallengeStarred, Desc ChallengeStamp]
|
|
|
|
|
|
|
|
listChallengesCore :: [Entity Challenge] -> Widget
|
2015-09-04 23:23:32 +02:00
|
|
|
listChallengesCore challenges = $(widgetFile "list-challenges-core")
|
2018-01-18 08:21:06 +01:00
|
|
|
|
2021-02-15 18:27:10 +01:00
|
|
|
getChallengeInfoJsonR :: Text -> Handler Value
|
|
|
|
getChallengeInfoJsonR challengeName = do
|
|
|
|
entCh <- runDB $ getBy404 $ UniqueName challengeName
|
|
|
|
return $ toJSON entCh
|
|
|
|
|
2021-02-24 14:11:30 +01:00
|
|
|
getVersionInfoJsonR :: Text -> Handler Value
|
|
|
|
getVersionInfoJsonR versionHash = do
|
|
|
|
theVersion <- runDB $ getBy404 $ UniqueVersionByCommit $ fromTextToSHA1 versionHash
|
|
|
|
return $ toJSON theVersion
|
|
|
|
|
|
|
|
|
2021-05-28 07:17:45 +02:00
|
|
|
getChallengeImgR :: Text -> Handler Html
|
|
|
|
getChallengeImgR chName = do
|
|
|
|
(Entity _ challenge) <- runDB $ getBy404 $ UniqueName chName
|
2018-01-18 08:21:06 +01:00
|
|
|
case challengeImage challenge of
|
|
|
|
Just image -> do
|
|
|
|
addHeader "Content-Disposition" "attachment; filename=\"image.png\""
|
|
|
|
sendResponse (typePng, toContent image)
|
|
|
|
Nothing -> sendResponseStatus status202 ()
|
2021-05-28 07:17:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
declareChallengeImgSwagger :: Declare (Definitions Schema) Swagger
|
|
|
|
declareChallengeImgSwagger = do
|
|
|
|
-- param schemas
|
|
|
|
let challengeNameSchema = toParamSchema (Proxy :: Proxy String)
|
|
|
|
|
|
|
|
return $ mempty
|
|
|
|
& paths .~
|
|
|
|
[ ("/api/challenge-img/{challengeName}",
|
|
|
|
mempty & get ?~ (mempty
|
|
|
|
& parameters .~ [ Inline $ mempty
|
|
|
|
& name .~ "challengeName"
|
|
|
|
& required ?~ True
|
|
|
|
& schema .~ ParamOther (mempty
|
|
|
|
& in_ .~ ParamPath
|
|
|
|
& paramSchema .~ challengeNameSchema) ]
|
|
|
|
& produces ?~ MimeList ["image/png"]
|
|
|
|
& description ?~ "Return the main image for a challenge"))
|
|
|
|
]
|
|
|
|
|
|
|
|
challengeImgApi :: Swagger
|
|
|
|
challengeImgApi = spec & definitions .~ defs
|
|
|
|
where
|
|
|
|
(defs, spec) = runDeclare declareChallengeImgSwagger mempty
|