Reputation: 1187
I am trying to add date parts into an URL for a blog post, lets say
/blog/2016/11/23/my-blog-post-slug
I want to check not only for the slug but the date parts as well. This is what I got so far:
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")
Seems to be a bit clunky. Is there a way to add this year, month, day parts as query filtering params? (I know I could execute a raw query, but this is not what I am looking for)
[Additional Info]
My models are defined as follows:
BlogPost
uuid Text
title Text
slug Text
markdownContent Text
createdAt UTCTime
UniqueBlogPostUuid uuid
UniqueBlogPostSlug slug
Primary uuid
Upvotes: 4
Views: 367
Reputation: 2214
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
Upvotes: 0