Haskell 从数据库检索消化函子的选项列表(快照/抢劫)
我有一个地址表,可以处理添加和编辑(如果添加,则无需任何内容,如果编辑,则只需地址)。到目前为止,我已经把州和国家的选择硬编码为几个项目Haskell 从数据库检索消化函子的选项列表(快照/抢劫),haskell,haskell-snap-framework,digestive-functors,Haskell,Haskell Snap Framework,Digestive Functors,我有一个地址表,可以处理添加和编辑(如果添加,则无需任何内容,如果编辑,则只需地址)。到目前为止,我已经把州和国家的选择硬编码为几个项目 addressForm :: Monad m => Maybe Address -> [Address] -> Form Text m Address addressForm a addrs = Address <$> "id" .: choiceWith (addrToChoice addrs) (fm
addressForm :: Monad m => Maybe Address -> [Address] -> Form Text m Address
addressForm a addrs =
Address
<$> "id" .: choiceWith (addrToChoice addrs) (fmap id a)
<*> "name" .: string (fmap name a)
<*> "street" .: string (fmap street a)
<*> "city" .: string (fmap city a)
<*> "state" .: choiceWith stateChoices (fmap state a)
<*> "country" .: choiceWith countryChoicesRequired (fmap country a)
<*> "zipcode" .: string (fmap zipcode a)
stateChoices :: [(Text, (Maybe String, Text))] -- (htmlValue, (realValue, labelValue))
stateChoices = [("ON", (Just "ON", "Ontario")), ("NE", (Just "NE", "Nebraska"))]
countryChoicesRequired :: [(Text, (String, Text))]
countryChoicesRequired = [("CA", ("CA", "Canada")), ("US", ("US", "United States of America"))]
addressForm::Monad m=>可能的地址->[Address]->Form Text m地址
地址格式a地址=
地址
“id”。:choiceWith(地址选择地址)(fmap id a)
“名称”。:字符串(fmap名称a)
“street.”:字符串(fmap street a)
“城市”。:字符串(fmap城市a)
“状态”。:选择状态选项(fmap状态a)
“国家”。:选择需要国家选项(fmap国家a)
“zipcode”:字符串(fmap zipcode a)
状态选项::[(文本,(可能是字符串,文本))]-(htmlValue,(realValue,labelValue))
州选项=[(“ON”,“ON”,“安大略省”),(“NE”,“NE”,“内布拉斯加州”)]
countryChoicesRequired::[(文本,(字符串,文本))]
countryChoicesRequired=[((“CA”,“加拿大”),(“美国”,“美国”)]
现在我想从数据库中检索州和国家的列表。我可以像处理地址列表一样将州/国家列表传递给表单,但此表单也是3或4个其他表单(新广告商表单、新客户表单等)的子表单,我不需要表单之外的州/国家列表,就像处理地址列表一样
这是我刚刚添加的获取国家信息的新表单:
addressForm' :: (HasPostgres p, Monad m) => Maybe Address -> [Address] -> p (Form Text m Address)
addressForm' a addrs = do
countries <- countryChoices'
return $ Address
<$> "id" .: choiceWith (addrToChoice addrs) (fmap id a)
<*> "name" .: string (fmap name a)
<*> "street" .: string (fmap street a)
<*> "city" .: string (fmap city a)
<*> "state" .: choiceWith stateChoices (fmap state a)
<*> "country" .: choiceWith countries (fmap country a)
<*> "zipcode" .: string (fmap zipcode a)
data Region = Region
{ code :: String
, fullName :: String
}
countryChoices' :: HasPostgres m => m [(Text, (String, Text))]
countryChoices' = do
countries <- getCountries
return $ abbrToChoice countries
abbrToChoice :: [Region] -> [(Text, (String, Text))]
abbrToChoice regions =
map (\ a -> ((pack $ code a), (code a, (pack $ fullName a)))) regions
addressForm'::(HasPostgres p,Monad m)=>可能地址->[Address]->p(Form Text m Address)
addressForm'a addrs=do
国家m[(文本,(字符串,文本))]
countryChoices'=do
国家[(文本,(字符串,文本))]
阿巴托选择区=
映射(\a->((pack$code a),(code a,(pack$fullName a)))区域
下面是调用它的处理程序:
editAddressH :: ([(T.Text, Splice AppHandler)] -> AppHandler ()) -> Addr.Address -> Maybe Int64 -> AppHandler ()
editAddressH renderer a userId = do
addresses <- Addr.list userId
(view, result) <- runForm "form" $ Addr.addressForm' (Just a) addresses -- line 233
case result of
Just x -> do
r <- Addr.edit x -- line 236
case r of
_ -> renderer [("success", showContents), ("dfForm", hideContents)]
Nothing -> renderer $ ("success", hideContents) : ("addressList", addressScriptSplice addresses) : phoneSplices ++ digestiveSplices view
editAddressH::([(T.Text,拼接AppHandler)]->AppHandler())->Addr.Address->Maybe Int64->AppHandler()
editAddressH呈现程序a userId=do
地址渲染器
$(“成功”,隐藏内容)
:(“地址列表”,地址脚本拼接地址)
:phoneSplices++digestiveSplices视图}
src/Site.hs:236:40:
无法匹配预期的类型“地址”
实际类型为'suscellate-funcors-0.5.0.1:Text.suscellate.Form.Internal.FormTree
m0文本m0地址'
应为类型:地址地址
实际类型:表单文本m0地址
在'Addr.edit'的第一个参数中,即'x'
在“do”表达式的stmt中:r看起来需要该函数。它可能看起来像这样:
addressForm :: Monad m => Maybe Address -> [Address] -> Form Text m Address
addressForm a addrs = monadic $ do
stateChoices <- getStatesFromDB
countryChoices <- getCountriesFromDB
return $
Address
<$> "id" .: choiceWith (addrToChoice addrs) (fmap id a)
<*> "name" .: string (fmap name a)
<*> "street" .: string (fmap street a)
<*> "city" .: string (fmap city a)
<*> "state" .: choiceWith stateChoices (fmap state a)
<*> "country" .: choiceWith countryChoices (fmap country a)
<*> "zipcode" .: string (fmap zipcode a)
addressForm::Monad m=>可能的地址->[Address]->Form Text m地址
地址形式a地址=一元$do
州选择
addressForm :: Monad m => Maybe Address -> [Address] -> Form Text m Address
addressForm a addrs = monadic $ do
stateChoices <- getStatesFromDB
countryChoices <- getCountriesFromDB
return $
Address
<$> "id" .: choiceWith (addrToChoice addrs) (fmap id a)
<*> "name" .: string (fmap name a)
<*> "street" .: string (fmap street a)
<*> "city" .: string (fmap city a)
<*> "state" .: choiceWith stateChoices (fmap state a)
<*> "country" .: choiceWith countryChoices (fmap country a)
<*> "zipcode" .: string (fmap zipcode a)