4

我正在尝试将日期部分添加到博客文章的 URL 中,可以说

/blog/2016/11/23/my-blog-post-slug

我不仅要检查蛞蝓,还要检查日期部分。这是我到目前为止得到的:

getBlogPostR :: Int -> Int -> Int -> Slug -> Handler Html
getBlogPostR pathYear pathMonth pathDay pathSlug = do
  Entity _ BlogPost {..} <- runDB $ getBy404 $ UniqueBlogPostSlug pathSlug
  let (year, month, day) = toGregorian $ utctDay blogPostCreatedAt
  if (fromIntegral year /= pathYear && month /= pathMonth && day /= pathDay)
    then notFound
    else
      defaultLayout $ do
        setTitleI blogPostTitle
        $(widgetFile "blog/post")

似乎有点笨拙。有没有办法将今年、月、日部分添加为查询过滤参数?(我知道我可以执行原始查询,但这不是我想要的)

[附加信息]

我的模型定义如下:

BlogPost
  uuid Text
  title Text
  slug Text
  markdownContent Text
  createdAt UTCTime
  UniqueBlogPostUuid uuid
  UniqueBlogPostSlug slug
  Primary uuid
4

1 回答 1

0
getBlogPostR :: Slug -> Handler Html
getBlogPostR pathSlug = do
  Entity _ BlogPost {..} <- runDB $ getBy404 $ UniqueBlogPostSlug pathSlug
  mYear <- lookupGetParam "year"
  mMonth <- lookupGetParam "month"
  mDay <- lookupGetParam "day"
  case (,,) <$> mYear <*> mMonth <*> mDay of
    Just (queryYear, queryMonth, queryDay) -> do 
      let (year, month, day) = toGregorian $ utctDay blogPostCreatedAt
      if (fromIntegral year /= queryYear && month /= queryMonth && day /= queryDay)
        then notFound
        else
          defaultLayout $ do
            setTitleI blogPostTitle
            $(widgetFile "blog/post")
    Nothing -> notFound
于 2018-05-10T17:42:49.203 回答