2014-09-22 46 views
0

我的問題是關於如何從查詢結果列表中提取每個實體Post的年份。如何從我的模型中提取yesod中處理程序的信息?

我的模型是這樣的:

Post 
    name Text 
    content Text 
    date UTCTime default=CURRENT_TIME 
    deriving Show 

而且我的處理程序是這樣的:

getYearR :: Integer -> Handler Html 
getYearR year = do 
    posts <- runDB $ selectList [] [Desc PostDate] 
    years <- --some function with posts for extract the list of years 
    defaultLayout $ do 
     aDomId <- newIdent 
     setTitle "Blog" 
     $(widgetFile "homepage") 

也許溶液與約單子一些基本知識有關,但我不知道如何工作的對於這種情況。

感謝您的時間,我真的很感謝你的幫助

回答

2

首先我創建了一個方便的功能,提取一年。我引用these docs獲取DayUTCTimethese docsDay獲得年份。

import Data.Time 

getYear :: UTCTime -> Integer 
getYear time = 
    let (year,_,_) = toGregorian $ utctDay time 
    in year 

使用此功能,我可以通過提取使用entityValEntity Haskell的記錄得到的年份列表,提取UTCTime使用postDate,然後用我的getYear功能提取一年

let years = map (getYear . postDate . entityVal) posts 

如果您使用的是哈姆雷特中的年份列表,則可以跳過創建中間值years的值,並重復執行posts

<ul> 
    $forall Entity id post <- posts 
     <li> 
      <h4>The year is #{getYear $ postDate post}</a>