Add sample JSON API for listing challenges

This commit is contained in:
Filip Gralinski 2020-10-12 07:27:32 +02:00
parent 5b05c4441c
commit abaaf1c301
3 changed files with 21 additions and 1 deletions

View File

@ -152,6 +152,7 @@ instance Yesod App where
isAuthorized QueryFormR _ = regularAuthorization isAuthorized QueryFormR _ = regularAuthorization
isAuthorized (QueryResultsR _) _ = regularAuthorization isAuthorized (QueryResultsR _) _ = regularAuthorization
isAuthorized ListChallengesR _ = regularAuthorization isAuthorized ListChallengesR _ = regularAuthorization
isAuthorized ListChallengesJsonR _ = regularAuthorization
isAuthorized (ViewVariantR _ ) _ = regularAuthorization isAuthorized (ViewVariantR _ ) _ = regularAuthorization
isAuthorized (ViewVariantTestR _ _) _ = regularAuthorization isAuthorized (ViewVariantTestR _ _) _ = regularAuthorization

View File

@ -2,12 +2,30 @@ module Handler.ListChallenges where
import Import import Import
mainCondition :: [Filter Challenge]
mainCondition = [ChallengeArchived !=. Just True]
getListChallengesR :: Handler Html getListChallengesR :: Handler Html
getListChallengesR = generalListChallenges [ChallengeArchived !=. Just True] getListChallengesR = generalListChallenges mainCondition
getListChallengesJsonR :: Handler Value
getListChallengesJsonR = generalListChallengesJson mainCondition
getListArchivedChallengesR :: Handler Html getListArchivedChallengesR :: Handler Html
getListArchivedChallengesR = generalListChallenges [ChallengeArchived ==. Just True] getListArchivedChallengesR = generalListChallenges [ChallengeArchived ==. Just True]
instance ToJSON (Entity Challenge) where
toJSON (Entity _ ch) = object
[ "link" .= ("/challenge/" <> (challengeName ch))
, "title" .= challengeTitle ch
, "description" .= challengeDescription ch
]
generalListChallengesJson :: [Filter Challenge] -> Handler Value
generalListChallengesJson filterExpr = do
challenges <- getChallenges filterExpr
return $ toJSON challenges
generalListChallenges :: [Filter Challenge] -> Handler Html generalListChallenges :: [Filter Challenge] -> Handler Html
generalListChallenges filterExpr = do generalListChallenges filterExpr = do
challenges <- getChallenges filterExpr challenges <- getChallenges filterExpr

View File

@ -10,6 +10,7 @@
/view-progress/#Int ViewProgressR GET /view-progress/#Int ViewProgressR GET
/open-view-progress/#Int OpenViewProgressR GET /open-view-progress/#Int OpenViewProgressR GET
/list-challenges ListChallengesR GET /list-challenges ListChallengesR GET
/api/list-challenges ListChallengesJsonR GET
/list-archived-challenges ListArchivedChallengesR GET /list-archived-challenges ListArchivedChallengesR GET
/challenge-image/#ChallengeId ChallengeImageR GET /challenge-image/#ChallengeId ChallengeImageR GET