0

Hi I am a beginner in Haskell and Yesod and I need help with this problem.

These are my entities:

Location
    name Text sqltype=varchar(255)
    address AddressId Maybe sqltype=varchar(255)
    UniqueLocationName name
    deriving Show Typeable

Manifestation
    name Text sqltype=varchar(255)
    description Text Maybe sqltype=varchar(255)
    category Category Maybe
    startDateTime UTCTime sqltype=DateTime
    location LocationId Maybe sqltype=varchar(255)
    UniqueManName name
    deriving Show Typeable

This is my handler that calls the man-details template:

getManDetailsR :: ManifestationId -> Handler Html
getManDetailsR mid = do
    (_, user) <- requireAuthPair
    md <- runDB $ get404 mid -- type is Manifestation
    defaultLayout $ do
        setTitle "Manifestation details"
        $(widgetFile "man-details")

And part of the man-details hamlet file where I want to display information about the event:

<div class="row">
        <div class="col-md-5">
            <div class="project-info-box mt-0">
                <h2>#{manifestationName md}
                <p class="mb-0">#{fromJust(manifestationDescription md)}

            <div class="project-info-box">
                <p><b>Start :</b>{show $ manifestationStartDateTime md}
                <p><b>Location :</b>#{locationName (manifestationLocation md)}

In this case error is : Couldn't match expected type ‘Location’ with actual type ‘Maybe (Key Location)’

Then I try like this :

$maybe l <- manifestationLocation md
                    <p><b>Location :</b>{locationName l}

And error is : Couldn't match expected type ‘Location’ with actual type ‘Key Location’

I apologize for the huge question, but I don't know how to get out of this, ie how to get only the value out of this pair (Key Location)?
Any advice and help is welcome, Thanks.

jovanKg
  • 35
  • 7
  • 2
    `l` contains the `Key` of a `Location`, not the `Location` itself, for that you will need another query. – Willem Van Onsem Feb 26 '21 at 21:02
  • Do you want to tell me that it is impossible to get to the location like this? Do I have to run the runDB once again, now with mid (id of manifestation) in the handler to get the location of that manifestation and then use it in the template? – jovanKg Feb 27 '21 at 14:32
  • If so, then my relation between the Location and the Manifestation is not what I need. I need to make a many to many relation so I can get Location from Manifestation? – jovanKg Feb 27 '21 at 15:05

1 Answers1

0

I'll post my solution, it's a beginner's problem, but it might be useful to someone.
As @Willem Van Onsem wrote in the comment, I again made a query to the database
now for a location.
Now handler looks like this, with the fromJust function because of the Maybe wrapper:

getManDetailsR :: ManifestationId -> Handler Html
getManDetailsR mid = do
    (_, user) <- requireAuthPair
    md <- runDB $ get404 mid
    loc <- runDB $ get404 $ fromJust(manifestationLocation md) -- and now use loc in template
    liftIO $ print (loc)
    defaultLayout $ do
        setTitle "Manifestation details"
        $(widgetFile "man-details")
jovanKg
  • 35
  • 7