Search code examples
haskellyesodpersist

Yesod Persist create String key from JSON


I have two pretty simple model definitions:

Page json
  uuid Text
  title Text
  UniquePageUuid uuid
  Primary uuid

Link
  uuid Text
  href Text
  pageId PageId Maybe
  Primary uuid

I would like to build a JSON endpoint to update the pageId in the Link model:

data ApiLink { linkPageId :: PageId } deriving (Generic)

instance FromJSON ApiLink

My handler would look something like this:

patchApiLinkR :: LinkId -> Handler Value
patchApiLinkR linkId = do
  jsonData <- requireJsonBody :: Handler ApiLink
  link <- runDB $ update linkId [LinkPageId =. (linkPageId jsonData)]
  returnJson link

Unfortunately I have no idea how to make this work, since the linkPageId in ApiLink has the type of PageId. I have seen some code to create DB Keys for Int64, but I don't know how to create them from Text values.

Thanks a lot!


Solution

  • You have to slightly change your db query to like this:

    link <- runDB $ update linkId [LinkPageId =. (Just $ linkPageId jsonData)]