getBy404 与 get404

getBy404 vs get404

我正在尝试使用 Yesod 网络框架构建休息服务。我可以通过 Id 获取条目,但我不能通过唯一键获取它们。这是因为 getBy404 的类型签名与 get404 不同。 getBy404 returns 包装在实体中的值和 get404 returns 纯值。

module Handler.MusicaUser where
import Import

getMusicaUserR :: MusicaUserId -> Handler Value
getMusicaUserR pid = do
post <- runDB $ get404 pid

    return $ object ["user" .= (Entity pid post)]

putMusicaUserR :: MusicaUserId -> Handler Value
putMusicaUserR pid = do
    post <- requireJsonBody :: Handler MusicaUser

    runDB $ replace pid post

    sendResponseStatus status200 ("UPDATED" :: Text)

deleteMusicaUserR :: MusicaUserId -> Handler Value
deleteMusicaUserR pid = do
    runDB $ delete pid

    sendResponseStatus status200 ("DELETED" :: Text)

我已尝试使用此示例修改我的代码

Why does this code work with Yesod.Persist's get404 but not getBy404?

谢谢。感谢您的帮助。

编辑

我想我越来越近了

getMusicaUserR :: Int64 -> Handler Value
getMusicaUserR facebookId = do
   user <- runDB $ getBy404 (UniqueFacebookId facebookId)
   return $ object ["user" .= (user)]

我现在得到的错误是

Application.hs:41:1:
    Couldn't match type ‘Int64’ with ‘Key MusicaUser’
    Expected type: MusicaUserId
      Actual type: Int64
    In the first argument of ‘MusicaUserR’, namely ‘dyn_al6K’
    In the first argument of ‘Just’, namely ‘MusicaUserR dyn_al6K’

Int64 是我函数的正确类型吗?我使用它的原因是因为这就是我的模型中定义 facebookId 的方式。我也尝试过使用 UniqueFacebookId。

为了修复上述错误,我需要将路由定义更改为

/users/#Int64 而不是 /users/MusicaUserId

当您查看 the definition of Entity 时,您会发现它只是一个实体的容器并且是键。您可以使用 entityKeyentityVal 这两个函数从 getBy404 的 return 值中提取键和值。