如何在Yesod中获取参考模型信息?
How to get referenced model information in Yesod?
我有 3 个模型
Address json
number Text
street Text
pincode Text
deriving Show
Person json
email Text
name Text
telephone Text
deriving Show
House json
rent Int
ownerId PersonId
addressId AddressId
deriving Show
我想访问所有房屋。我可以通过
getHouseR :: Handler Value
getHouseR = do
houses <- runDB $ selectList [] [Asc HouseRent]
return $ toJSON houses
但我只得到对其他实体的引用,例如 person & address
。
[{"rent":8000,"addressId":4,"ownerId":1,"id":3},{"rent":10000,"addressId":2,"ownerId":1,"id":1}]
我想获得房子的完整信息,这意味着,解析 ownerId & addressId
并发送数据,可能看起来像,
[{"rent":8000,"address":{"number": "23", "street": "12/B", "pincode": "111111"},"owner":{"email": "hey@email.com", "name": "Moto", "telephone": "xxxxxxxxxx"},"id":3}]
有什么方法可以将 getForiegnData
之类的信息添加到查询中,这样我就可以获取所有这些信息吗?或者任何其他解决方案?
我找不到任何直接的方式(类似于注释)来获取引用的模型信息。但是我们有两种方法可以完成这项工作。
- 以防万一,如果您的数据库是 SQL。您可以使用 esqueleto https://github.com/bitemyapp/esqueleto/blob/master/README.md.
- 编写一些样板代码并获取信息。
创建 HouseResp
类型,它将代表您的响应正文,包括人员和地址信息。
data HouseResp = HouseResp
{ rent :: Int
, owner :: Person
, address :: Address
}
然后,修改 getHouseR
以使用可用的参考 ID 获取每个房屋的完整信息。
getHouseR :: Handler Value
getHouseR = do
housesWithReference <- runDB $ selectList [] [Asc HouseRent]
houses <- sequence (Import.map getCompleteHouse housesWithReference)
return $ toJSON houses
getCompleteHouse :: Entity House -> Handler HouseResp
getCompleteHouse house = runDB $ do
let rent' = houseRent (entityVal house)
person <- getJust (houseOwnerId (entityVal house))
address' <- getJust (houseAddressId (entityVal house))
return (HouseResp rent' person address')
我有 3 个模型
Address json
number Text
street Text
pincode Text
deriving Show
Person json
email Text
name Text
telephone Text
deriving Show
House json
rent Int
ownerId PersonId
addressId AddressId
deriving Show
我想访问所有房屋。我可以通过
getHouseR :: Handler Value
getHouseR = do
houses <- runDB $ selectList [] [Asc HouseRent]
return $ toJSON houses
但我只得到对其他实体的引用,例如 person & address
。
[{"rent":8000,"addressId":4,"ownerId":1,"id":3},{"rent":10000,"addressId":2,"ownerId":1,"id":1}]
我想获得房子的完整信息,这意味着,解析 ownerId & addressId
并发送数据,可能看起来像,
[{"rent":8000,"address":{"number": "23", "street": "12/B", "pincode": "111111"},"owner":{"email": "hey@email.com", "name": "Moto", "telephone": "xxxxxxxxxx"},"id":3}]
有什么方法可以将 getForiegnData
之类的信息添加到查询中,这样我就可以获取所有这些信息吗?或者任何其他解决方案?
我找不到任何直接的方式(类似于注释)来获取引用的模型信息。但是我们有两种方法可以完成这项工作。
- 以防万一,如果您的数据库是 SQL。您可以使用 esqueleto https://github.com/bitemyapp/esqueleto/blob/master/README.md.
- 编写一些样板代码并获取信息。
创建 HouseResp
类型,它将代表您的响应正文,包括人员和地址信息。
data HouseResp = HouseResp
{ rent :: Int
, owner :: Person
, address :: Address
}
然后,修改 getHouseR
以使用可用的参考 ID 获取每个房屋的完整信息。
getHouseR :: Handler Value
getHouseR = do
housesWithReference <- runDB $ selectList [] [Asc HouseRent]
houses <- sequence (Import.map getCompleteHouse housesWithReference)
return $ toJSON houses
getCompleteHouse :: Entity House -> Handler HouseResp
getCompleteHouse house = runDB $ do
let rent' = houseRent (entityVal house)
person <- getJust (houseOwnerId (entityVal house))
address' <- getJust (houseAddressId (entityVal house))
return (HouseResp rent' person address')