#3: Routet page and limited entries
The blog module routes /blog/{page} and the blog handler limits the entries per page (given as a feature) and sets the offsets according to the given page number
This commit is contained in:
@@ -49,4 +49,10 @@ feature -- Access node
|
|||||||
Result := node_storage.blogs
|
Result := node_storage.blogs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
blogs_order_created_desc_limited (a_limit:INTEGER_32; a_offset:INTEGER_32) : LIST[CMS_NODE]
|
||||||
|
-- List of nodes ordered by creation date and limited by limit and offset
|
||||||
|
do
|
||||||
|
Result := node_storage.blogs_limited (a_limit, a_offset)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -106,14 +106,6 @@ feature -- Access: router
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--feature -- Access: router
|
|
||||||
|
|
||||||
--setup_router (a_router: WSF_ROUTER; a_api: CMS_API)
|
|
||||||
-- <Precursor>
|
|
||||||
-- do
|
|
||||||
-- a_router.handle_with_request_methods ("/blogs/", create {WSF_URI_AGENT_HANDLER}.make (agent handle_blogs (?,?, a_api)), a_router.methods_get)
|
|
||||||
-- end
|
|
||||||
|
|
||||||
configure_web (a_api: CMS_API; a_node_api: CMS_BLOG_API; a_router: WSF_ROUTER)
|
configure_web (a_api: CMS_API; a_node_api: CMS_BLOG_API; a_router: WSF_ROUTER)
|
||||||
local
|
local
|
||||||
l_blog_handler: BLOG_HANDLER
|
l_blog_handler: BLOG_HANDLER
|
||||||
@@ -121,8 +113,14 @@ configure_web (a_api: CMS_API; a_node_api: CMS_BLOG_API; a_router: WSF_ROUTER)
|
|||||||
do
|
do
|
||||||
-- TODO: for now, focused only on web interface, add REST api later. [2015-May-18]
|
-- TODO: for now, focused only on web interface, add REST api later. [2015-May-18]
|
||||||
create l_blog_handler.make (a_api, a_node_api)
|
create l_blog_handler.make (a_api, a_node_api)
|
||||||
create l_uri_mapping.make_trailing_slash_ignored ("/blogs", l_blog_handler)
|
|
||||||
|
-- Let the class BLOG_HANDLER handle the requests on "/blogs"
|
||||||
|
create l_uri_mapping.make_trailing_slash_ignored("/blogs", l_blog_handler)
|
||||||
a_router.map_with_request_methods (l_uri_mapping, a_router.methods_get)
|
a_router.map_with_request_methods (l_uri_mapping, a_router.methods_get)
|
||||||
|
|
||||||
|
-- We can add a page number after /blogs/ to get older posts
|
||||||
|
a_router.handle_with_request_methods ("/blogs/{page}", l_blog_handler, a_router.methods_get)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
feature -- Hooks
|
feature -- Hooks
|
||||||
@@ -136,6 +134,7 @@ feature -- Hooks
|
|||||||
local
|
local
|
||||||
lnk: CMS_LOCAL_LINK
|
lnk: CMS_LOCAL_LINK
|
||||||
do
|
do
|
||||||
|
-- Add the link to the blog to the main menu
|
||||||
create lnk.make ("Blogs", "/blogs/")
|
create lnk.make ("Blogs", "/blogs/")
|
||||||
a_menu_system.primary_menu.extend (lnk)
|
a_menu_system.primary_menu.extend (lnk)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,9 +12,18 @@ inherit
|
|||||||
|
|
||||||
WSF_URI_HANDLER
|
WSF_URI_HANDLER
|
||||||
rename
|
rename
|
||||||
|
execute as uri_execute,
|
||||||
new_mapping as new_uri_mapping
|
new_mapping as new_uri_mapping
|
||||||
end
|
end
|
||||||
|
|
||||||
|
WSF_URI_TEMPLATE_HANDLER
|
||||||
|
rename
|
||||||
|
execute as uri_template_execute,
|
||||||
|
new_mapping as new_uri_template_mapping
|
||||||
|
select
|
||||||
|
new_uri_template_mapping
|
||||||
|
end
|
||||||
|
|
||||||
WSF_RESOURCE_HANDLER_HELPER
|
WSF_RESOURCE_HANDLER_HELPER
|
||||||
redefine
|
redefine
|
||||||
do_get
|
do_get
|
||||||
@@ -33,6 +42,18 @@ feature -- execute
|
|||||||
execute_methods (req, res)
|
execute_methods (req, res)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
uri_execute (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||||
|
-- Execute request handler
|
||||||
|
do
|
||||||
|
execute (req, res)
|
||||||
|
end
|
||||||
|
|
||||||
|
uri_template_execute (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||||
|
-- Execute request handler
|
||||||
|
do
|
||||||
|
execute (req, res)
|
||||||
|
end
|
||||||
|
|
||||||
feature -- Settings
|
feature -- Settings
|
||||||
entries_per_page : INTEGER
|
entries_per_page : INTEGER
|
||||||
-- The numbers of posts that are shown on one page. If there are more post a pagination is generated
|
-- The numbers of posts that are shown on one page. If there are more post a pagination is generated
|
||||||
@@ -42,27 +63,54 @@ feature -- Settings
|
|||||||
Result := 2
|
Result := 2
|
||||||
end
|
end
|
||||||
|
|
||||||
|
more_than_one_page : BOOLEAN
|
||||||
|
-- Checks if all posts fit on one page (FALSE) or if more than one page is needed (TRUE)
|
||||||
|
do
|
||||||
|
Result := entries_per_page < node_api.blogs_count
|
||||||
|
end
|
||||||
|
|
||||||
feature -- HTTP Methods
|
feature -- HTTP Methods
|
||||||
do_get (req: WSF_REQUEST; res: WSF_RESPONSE)
|
do_get (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||||
-- <Precursor>
|
-- <Precursor>
|
||||||
local
|
local
|
||||||
l_page: CMS_RESPONSE
|
l_page: CMS_RESPONSE
|
||||||
|
l_posts: LIST[CMS_NODE]
|
||||||
s: STRING
|
s: STRING
|
||||||
n: CMS_NODE
|
n: CMS_NODE
|
||||||
lnk: CMS_LOCAL_LINK
|
lnk: CMS_LOCAL_LINK
|
||||||
hdate: HTTP_DATE
|
hdate: HTTP_DATE
|
||||||
|
page_number:NATURAL_16
|
||||||
do
|
do
|
||||||
-- At the moment the template is hardcoded, but we can
|
-- At the moment the template is hardcoded, but we can
|
||||||
-- get them from the configuration file and load them into
|
-- get them from the configuration file and load them into
|
||||||
-- the setup class.
|
-- the setup class.
|
||||||
|
|
||||||
|
--Check if a page is given, if not start with page 1
|
||||||
|
if req.path_info.ends_with_general ("/blogs") then
|
||||||
|
-- No page number given, set to 0
|
||||||
|
page_number := 1
|
||||||
|
else
|
||||||
|
-- Read page number from get variable
|
||||||
|
page_number := page_number_path_parameter (req)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Ensure that page is never 0 (happens if /blogs/ is routed)
|
||||||
|
if page_number = 0 then page_number := 1 end
|
||||||
|
|
||||||
create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api)
|
create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api)
|
||||||
l_page.add_variable (node_api.nodes, "nodes")
|
l_page.add_variable (node_api.nodes, "nodes")
|
||||||
|
|
||||||
|
|
||||||
-- NOTE: for development purposes we have the following hardcode output.
|
-- Output the title. If more than one page, also output the current page number
|
||||||
create s.make_from_string ("<h2>Blog</h2>")
|
create s.make_from_string ("<h2>Blog")
|
||||||
if attached node_api.blogs_order_created_desc as lst then
|
if more_than_one_page then
|
||||||
|
s.append (" (Page " + page_number.out + ")")
|
||||||
|
-- Get the posts from the current page (limited by entries per page)
|
||||||
|
end
|
||||||
|
s.append ("</h2>")
|
||||||
|
|
||||||
|
|
||||||
|
if attached node_api.blogs_order_created_desc_limited (entries_per_page, (page_number-1) * entries_per_page) as lst then
|
||||||
-- Filter out blog entries from all nodes
|
-- Filter out blog entries from all nodes
|
||||||
--if n.content_type.is_equal ("blog") then
|
--if n.content_type.is_equal ("blog") then
|
||||||
s.append ("<ul class=%"cms-blog-nodes%">%N")
|
s.append ("<ul class=%"cms-blog-nodes%">%N")
|
||||||
@@ -113,5 +161,19 @@ feature -- HTTP Methods
|
|||||||
l_page.execute
|
l_page.execute
|
||||||
end
|
end
|
||||||
|
|
||||||
|
feature -- Query
|
||||||
|
|
||||||
|
page_number_path_parameter (req: WSF_REQUEST): NATURAL_16
|
||||||
|
-- Returns the page number from the path /blogs/{page}. It's an unsigned integere since negative pages are not allowed
|
||||||
|
local
|
||||||
|
s: STRING
|
||||||
|
do
|
||||||
|
if attached {WSF_STRING} req.path_parameter ("page") as p_page then
|
||||||
|
s := p_page.value
|
||||||
|
if s.is_natural_16 then
|
||||||
|
Result := s.to_natural_16
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -21,4 +21,9 @@ feature -- Access
|
|||||||
deferred
|
deferred
|
||||||
end
|
end
|
||||||
|
|
||||||
|
blogs_limited (limit:INTEGER_32; offset:INTEGER_32) : LIST[CMS_NODE]
|
||||||
|
-- List of nodes ordered by creation date from limit to limit + offset
|
||||||
|
deferred
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -28,4 +28,9 @@ feature -- Access
|
|||||||
create {ARRAYED_LIST [CMS_NODE]} Result.make (0)
|
create {ARRAYED_LIST [CMS_NODE]} Result.make (0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
blogs_limited (limit:INTEGER_32; offset:INTEGER_32) : LIST[CMS_NODE]
|
||||||
|
-- List of nodes ordered by creation date from limit to limit + offset
|
||||||
|
do
|
||||||
|
create {ARRAYED_LIST [CMS_NODE]} Result.make (0)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -49,6 +49,32 @@ feature -- Access
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
blogs_limited (a_limit:INTEGER_32; a_offset:INTEGER_32) : LIST[CMS_NODE]
|
||||||
|
-- List of nodes ordered by creation date from limit to limit + offset
|
||||||
|
local
|
||||||
|
l_parameters: STRING_TABLE [detachable ANY]
|
||||||
|
do
|
||||||
|
create {ARRAYED_LIST [CMS_NODE]} Result.make (0)
|
||||||
|
|
||||||
|
error_handler.reset
|
||||||
|
write_information_log (generator + ".nodes")
|
||||||
|
|
||||||
|
from
|
||||||
|
create l_parameters.make (2)
|
||||||
|
l_parameters.put (a_limit, "limit")
|
||||||
|
l_parameters.put (a_offset, "offset")
|
||||||
|
sql_query (sql_blogs_limited, l_parameters)
|
||||||
|
sql_start
|
||||||
|
until
|
||||||
|
sql_after
|
||||||
|
loop
|
||||||
|
if attached fetch_node as l_node then
|
||||||
|
Result.force (l_node)
|
||||||
|
end
|
||||||
|
sql_forth
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
feature {NONE} -- Queries
|
feature {NONE} -- Queries
|
||||||
|
|
||||||
sql_select_blog_count: STRING = "SELECT count(*) FROM Nodes WHERE status != -1 AND type = %"blog%";"
|
sql_select_blog_count: STRING = "SELECT count(*) FROM Nodes WHERE status != -1 AND type = %"blog%";"
|
||||||
@@ -58,4 +84,7 @@ feature {NONE} -- Queries
|
|||||||
sql_select_blogs_order_created_desc: STRING = "SELECT * FROM Nodes WHERE status != -1 AND type = %"blog%" ORDER BY created DESC;"
|
sql_select_blogs_order_created_desc: STRING = "SELECT * FROM Nodes WHERE status != -1 AND type = %"blog%" ORDER BY created DESC;"
|
||||||
-- SQL Query to retrieve all nodes that are from the type "blog" ordered by descending creation date.
|
-- SQL Query to retrieve all nodes that are from the type "blog" ordered by descending creation date.
|
||||||
|
|
||||||
|
sql_blogs_limited: STRING = "SELECT * FROM Nodes WHERE status != -1 AND type = %"blog%" ORDER BY created DESC LIMIT :limit OFFSET :offset ;"
|
||||||
|
--- SQL Query to retrieve all node of type "blog" from limit to limit + offset
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user