Commits

Luke Plant committed b2b12e1

Added basic paging to main page (no UI yet)

Comments (0)

Files changed (2)

src/Blog/Model.hs

 
 import Database.HDBC
 import Blog.DBUtils (makeSlugGeneric)
+import Blog.Utils (regexReplace)
 import qualified Blog.DB as DB
 import qualified Blog.Post as P
 import qualified Blog.Category as Ct
 import qualified Blog.Comment as Cm
 import qualified Data.List as List
+import qualified Data.ByteString.Lazy.Char8 as BL
 
 ------ Create -------
 getDbId cn =
 -- (we can then use the same 'makePost' function)
 getPostByIdQuery        = "SELECT id, title, slug, post_raw, post_formatted, summary_raw, summary_formatted, format_id, timestamp, comments_open FROM posts WHERE id = ?;"
 getPostBySlugQuery      = "SELECT id, title, slug, '',       post_formatted, '',          '',                '',        timestamp, comments_open FROM posts WHERE slug = ?;"
-getRecentPostsQuery     = "SELECT id, title, slug, '',       '',             '',          summary_formatted, '',        timestamp, ''            FROM posts ORDER BY timestamp DESC LIMIT 20;"
+getRecentPostsQuery     = "SELECT id, title, slug, '',       '',             '',          summary_formatted, '',        timestamp, ''            FROM posts ORDER BY timestamp DESC $LIMITOFFSET;"
 
 
 -- Used to get post related to a post, ordered to favour posts with
 sqlInIds :: [Int] -> String
 sqlInIds ids = "(" ++ (concat $ List.intersperse "," $ map show ids) ++ ")"
 
+addLimitOffset sql limitOffset =
+    BL.unpack $ regexReplace (" \\$LIMITOFFSET") (BL.pack $ " " ++ limitOffset) (BL.pack sql)
+
+-- return 'LIMIT/OFFSET' for a page (1 indexed)
+makePagingLimitOffset page size =
+    let limit = size
+        offset = (page - 1) * size
+    in "LIMIT " ++ (show limit) ++ " OFFSET " ++ (show offset)
+
 ---- Constructors ----
 
 makePost row =
     [] -> return Nothing
     (postdata:_) -> return $ Just $ makePost postdata
 
-getRecentPosts cn = do
-  res <- quickQuery' cn getRecentPostsQuery []
+getRecentPosts cn p = do
+  let q = addLimitOffset getRecentPostsQuery (makePagingLimitOffset p 20)
+  res <- quickQuery' cn q []
   return $ map makePost res
 
 getCategoriesForPost cn post = do

src/Blog/Views.hs

 import Ella.Request
 import Ella.Response
 import Ella.Utils (addHtml)
-import Ella.GenUtils (utf8, with)
+import Ella.GenUtils (utf8, with, exactParse)
 import Blog.Templates
 import Blog.Links
 import Blog.DB (connect)
 import Blog.Model (getPostBySlug, getCategoriesForPost, getRecentPosts, getCommentsForPost, getRelatedPosts)
-
+import Maybe (fromMaybe)
 
 ---- Utilities
 
 
 ---- Views
 
+-- | parse a value (packed in a Just) or return a default
+--
+-- This is useful in dealing with 'Maybe' vals returned from
+-- getGET, getPOST etc.
+parseOrDefault v d = fromMaybe d (v >>= exactParse)
+
 -- View for the main page
 mainIndex :: Request -> IO (Maybe Response)
 mainIndex req = do
+  let page = (getGET "p" req) `parseOrDefault` 1 :: Int
   cn <- connect
-  posts <- getRecentPosts cn
+  posts <- getRecentPosts cn page
   return $ Just $ standardResponse $ mainIndexPage posts
 
 -- View to help with debugging