Tenho as seguintes entidades definidas com a biblioteca Persistent:
ClapEntity
Id Text
count Int
userId UserEntityId
deriving Show
UserEntity
Id Text
name Text
email Text
deriving Show
Quero armazenar novos dados ClapEntity
do ClapCreate
modelo:
newtype UserId = UserId {unUserId :: UUID}
deriving (Eq, Show, ToJSON, FromJSON)
data ClapCreate = ClapCreate
{ userId :: UserId,
count :: Int
}
deriving (Eq, Show)
-- Insert a new clap record into the database
storeClap :: ConnectionPool -> ClapCreate -> IO ClapId
storeClap pool clap = do
let clapEntity = clapCreateToEntity clap
key <- generateClapEntityKey
cid <- runSql (insertKey key clapEntity) pool
case toUUID (unClapEntityKey key) of
Left err -> error $ show err
Right uuid -> return $ ClapId uuid
clapCreateToEntity :: ClapCreate -> ClapEntity
clapCreateToEntity (ClapCreate (UserId uid) cnt) =
ClapEntity
cnt
(UserEntityId (UUID.toText uid)) -- ISSUE HERE
Não sei como criar um novo UserEntityId
a partir do dado uid
:(
Além disso, não quero buscar a UserEntity
primeira e usar a chave retornada, só quero criar uma nova chave estrangeira. Estou esquecendo de algo...
Pergunta bônus
A propósito, como posso usar cid
in case toUUID (unClapEntityKey key) of
line?
Você se refere a um objeto
α
comαKey
, então neste casoUserEntityKey
:O
UserEntityId
também existe, mas este é um construtor de dados sem nenhum parâmetro: ele é usado para se referir a uma coluna em uma expressão, comoselectList [UserEntityId ==. "c86aed00-023a-4368-89ce-56e5cbcd810e"]