haskell - Return JSON based on Persistent Query from yesod handler -
this additional question @konstantinerybnikov return json yesod handler.
what if want have persistent query selectlist
, result data must return json format? code it?
here code (adopt code of @konstantinerybnikov):
-- json import data.aeson (object, (.=)) import qualified data.aeson j import yesod.core.json (returnjson) getimagesr :: handler value getimagesr = images <- rundb $ selectlist [imagesfilename !=. ""] [desc imagesdate] return $ object (if null images [] else [] -- want result of perstent in json format)
``
hope me thank you.
i guess main problem selectlist
returns list of entity record
(where record
type querying) instead of record
, we'll have list of records using map entityval
(entityval
defined in entity
). handler :
getimagesr = images <- rundb $ selectlist [imagesfilename !=. ""] [desc imagesdate] returnjson (map entityval images)
now it's matter of transforming [record]
json. luckily, can done automatically adding word json
persistent model definition (as shown here).