4

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
Alebon
  • 1,189
  • 2
  • 11
  • 24

1 Answers1

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
MCH
  • 2,124
  • 1
  • 19
  • 34
  • Actually it is not what I meant. I know how to access the URL parts for year, month and day. I want to have it in the Persistent query, so I don't need to check this myself :) – Alebon May 22 '18 at 08:26
  • @Alebon Understood. I can help you with that. You should think of `UTCTime` as a time stamp or a number. The best way to do that would be to check for a `UTCTime` stamp that exists in a range like beginning of `2016/11/23` to the end of `2016/11/23`. The only problem is that you might have to adjust it for a particular time zone, or does that matter for you? – MCH May 22 '18 at 10:14
  • No, time zone is not of an interest here. Incoming params are aligned to the createdAt date in db. I don't use specific time zone changes. Thanks a lot for the quick response. – Alebon May 24 '18 at 08:53