Get items with selectList and filters. when field is Maybe

7 views
Skip to first unread message

Conee

unread,
Mar 5, 2021, 1:34:29 PM3/5/21
to Yesod Web Framework

Hi,
I have data :

Address
   state Text sqltype=varchar(255)
    city Text sqltype=varchar(255)
    street Text sqltype=varchar(255)
    number Text sqltype=varchar(255)
    deriving Show Typeable Eq

Location
   name Text sqltype=varchar(255)
   address AddressId 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 sqltype=varchar(255)
  UniqueManName name
   deriving Show Typeable

data Category = Sport | Concert | Theater
   deriving (Show, Eq, Read, Enum, Bounded)
   derivePersistField "Category"

What I want to do is to filter Manifestations, with the data I receive from users. Data for filtering is : [Category], city :: Text and search :: Text.
I imagined doing this with a selectList and its ability to filter items from the database but here I come up with one problem, so I would ask for help if anyone has an idea.

1. The first mistake is that my ManifestationCategory field is in the Maybe wrapper and I don't know way to handle this:
filterManifestations :: [Category] -> Maybe T.Text -> Maybe T.Text -> Handler [Entity Manifestation]
filterManifestations cat city src = do
    let city' = fromMaybe city
       src' = fromMaybe src
    runDB $ selectList [ManifestationCategory <-. cat] []

  -- Couldn't match type  ‘Category’     with ‘Maybe Category’
    arising from a use of ‘ManifestationCategory’
    • In the first argument of ‘(<-.)’, namely ‘ManifestationCategory’

Of course any advice is welcome,
thanks,
Nemanja

jsch...@gmail.com

unread,
Mar 6, 2021, 4:35:29 AM3/6/21
to Yesod Web Framework
Hi,

I don't know the (<-.) operator. But if I want to selectList with a filter, I use the (==.) operator for equality check.

- Jakob
Reply all
Reply to author
Forward
0 new messages