From bb28d2c590815213cee1020b72ef285872084a6a Mon Sep 17 00:00:00 2001 From: Filip Gralinski Date: Mon, 15 Feb 2021 18:27:10 +0100 Subject: [PATCH] Add challenge-info endpoint --- Foundation.hs | 1 + Handler/ListChallenges.hs | 20 +++++++++++++++++++- config/routes | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Foundation.hs b/Foundation.hs index 3d69250..eb96f1d 100644 --- a/Foundation.hs +++ b/Foundation.hs @@ -154,6 +154,7 @@ instance Yesod App where isAuthorized (QueryResultsR _) _ = regularAuthorization isAuthorized ListChallengesR _ = regularAuthorization isAuthorized ListChallengesJsonR _ = regularAuthorization + isAuthorized (ChallengeInfoJsonR _) _ = regularAuthorization isAuthorized (LeaderboardJsonR _) _ = regularAuthorization isAuthorized (ViewVariantR _ ) _ = regularAuthorization isAuthorized (ViewVariantTestR _ _) _ = regularAuthorization diff --git a/Handler/ListChallenges.hs b/Handler/ListChallenges.hs index 72d0c78..a795eb0 100644 --- a/Handler/ListChallenges.hs +++ b/Handler/ListChallenges.hs @@ -22,13 +22,26 @@ declareListChallengesSwagger :: Declare (Definitions Schema) Swagger declareListChallengesSwagger = do -- param schemas listChallengesResponse <- declareResponse (Proxy :: Proxy [Entity Challenge]) + challengeInfoResponse <- declareResponse (Proxy :: Proxy (Entity Challenge)) + let challengeNameSchema = toParamSchema (Proxy :: Proxy String) return $ mempty & paths .~ [ ("/api/list-challenges", mempty & get ?~ (mempty & produces ?~ MimeList ["application/json"] & description ?~ "Returns the list of all challenges" - & at 200 ?~ Inline listChallengesResponse)) + & 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)) ] listChallengesApi :: Swagger @@ -93,6 +106,11 @@ getChallenges filterExpr = runDB $ selectList filterExpr [Desc ChallengeStarred, listChallengesCore :: [Entity Challenge] -> Widget listChallengesCore challenges = $(widgetFile "list-challenges-core") +getChallengeInfoJsonR :: Text -> Handler Value +getChallengeInfoJsonR challengeName = do + entCh <- runDB $ getBy404 $ UniqueName challengeName + return $ toJSON entCh + getChallengeImageR :: ChallengeId -> Handler Html getChallengeImageR challengeId = do challenge <- runDB $ get404 challengeId diff --git a/config/routes b/config/routes index 05d7fca..3753dd0 100644 --- a/config/routes +++ b/config/routes @@ -21,6 +21,7 @@ /api/challenge-readme/#Text/markdown ChallengeReadmeInMarkdownR GET /api/challenge-image/#ChallengeId ChallengeImageR GET /api/query/#Text QueryJsonR GET +/api/challenge-info/#Text ChallengeInfoJsonR GET /list-archived-challenges ListArchivedChallengesR GET /challenge/#Text ShowChallengeR GET