diff --git a/Foundation.hs b/Foundation.hs index 9a83e70..10acaef 100644 --- a/Foundation.hs +++ b/Foundation.hs @@ -217,7 +217,7 @@ instance Yesod App where isAuthorized (ResetPasswordR _) _ = return Authorized isAuthorized (ToggleSubmissionTagR _) _ = regularAuthorization - isAuthorized (ChallengeImageR _) _ = regularAuthorization + isAuthorized (ChallengeImgR _) _ = regularAuthorization isAuthorized (ApiTxtScoreR _) _ = return Authorized diff --git a/Handler/ListChallenges.hs b/Handler/ListChallenges.hs index 36446f8..512fbd8 100644 --- a/Handler/ListChallenges.hs +++ b/Handler/ListChallenges.hs @@ -59,9 +59,9 @@ getListArchivedChallengesR :: Handler Html getListArchivedChallengesR = generalListChallenges [ChallengeArchived ==. Just True] imageUrl :: Entity Challenge -> Maybe (Route App) -imageUrl (Entity challengeId challenge) = +imageUrl (Entity _ challenge) = case challengeImage challenge of - Just _ -> Just $ ChallengeImageR challengeId + Just _ -> Just $ ChallengeImgR $ challengeName challenge Nothing -> Nothing instance ToJSON (Entity Challenge) where @@ -134,8 +134,7 @@ instance ToJSON (Entity Version) where instance ToSchema (Entity Version) where declareNamedSchema _ = do stringSchema <- declareSchemaRef (Proxy :: Proxy String) - booleanSchema <- declareSchemaRef (Proxy :: Proxy Bool) - return $ NamedSchema (Just "Challenge") $ mempty + return $ NamedSchema (Just "Version") $ mempty & type_ .~ SwaggerObject & properties .~ fromList [ ("deadline", stringSchema) @@ -176,11 +175,36 @@ getVersionInfoJsonR versionHash = do return $ toJSON theVersion -getChallengeImageR :: ChallengeId -> Handler Html -getChallengeImageR challengeId = do - challenge <- runDB $ get404 challengeId +getChallengeImgR :: Text -> Handler Html +getChallengeImgR chName = do + (Entity _ challenge) <- runDB $ getBy404 $ UniqueName chName case challengeImage challenge of Just image -> do addHeader "Content-Disposition" "attachment; filename=\"image.png\"" sendResponse (typePng, toContent image) Nothing -> sendResponseStatus status202 () + + +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 diff --git a/Handler/Swagger.hs b/Handler/Swagger.hs index 23299ab..96fecd4 100644 --- a/Handler/Swagger.hs +++ b/Handler/Swagger.hs @@ -29,6 +29,7 @@ apiDescription = generalApi <> versionInfoApi <> listTagsApi <> myTeamsApi + <> challengeImgApi generalApi :: Swagger generalApi = (mempty :: Swagger) diff --git a/config/routes b/config/routes index db256e4..f22a2fa 100644 --- a/config/routes +++ b/config/routes @@ -21,7 +21,7 @@ /api/add-user AddUserR GET /api/challenge-submission/#Text ChallengeSubmissionJsonR POST /api/challenge-readme/#Text/markdown ChallengeReadmeInMarkdownR GET -/api/challenge-image/#ChallengeId ChallengeImageR GET +/api/challenge-img/#Text ChallengeImgR GET /api/query/#Text QueryJsonR GET /api/challenge-info/#Text ChallengeInfoJsonR GET /api/version-info/#Text VersionInfoJsonR GET diff --git a/templates/list-challenges-core.hamlet b/templates/list-challenges-core.hamlet index 04d322c..953119b 100644 --- a/templates/list-challenges-core.hamlet +++ b/templates/list-challenges-core.hamlet @@ -1,9 +1,9 @@ -$forall (Entity challengeId challenge) <- challenges +$forall (Entity _ challenge) <- challenges
#{challengeTitle challenge} $if challengeStarred challenge \ $if isJust (challengeImage challenge) -
#{challengeDescription challenge} +
#{challengeDescription challenge} $else
#{challengeDescription challenge}