From fb196735b64bca277bef8d358f30b67e2fa0e6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20B=C3=B6sch?= Date: Fri, 22 May 2015 11:17:34 +0200 Subject: [PATCH] #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 --- examples/demo/modules/blog/cms_blog_api.e | 6 ++ examples/demo/modules/blog/cms_blog_module.e | 17 +++-- .../demo/modules/blog/handler/blog_handler.e | 68 ++++++++++++++++++- .../blog/persistence/cms_blog_storage_i.e | 5 ++ .../blog/persistence/cms_blog_storage_null.e | 5 ++ .../blog/persistence/cms_blog_storage_sql.e | 29 ++++++++ 6 files changed, 118 insertions(+), 12 deletions(-) diff --git a/examples/demo/modules/blog/cms_blog_api.e b/examples/demo/modules/blog/cms_blog_api.e index d75779b..f0fb9f1 100644 --- a/examples/demo/modules/blog/cms_blog_api.e +++ b/examples/demo/modules/blog/cms_blog_api.e @@ -49,4 +49,10 @@ feature -- Access node Result := node_storage.blogs 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 diff --git a/examples/demo/modules/blog/cms_blog_module.e b/examples/demo/modules/blog/cms_blog_module.e index f69c8a6..d247566 100644 --- a/examples/demo/modules/blog/cms_blog_module.e +++ b/examples/demo/modules/blog/cms_blog_module.e @@ -106,14 +106,6 @@ feature -- Access: router end ---feature -- Access: router - - --setup_router (a_router: WSF_ROUTER; a_api: CMS_API) - -- - -- 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) local 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 -- 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_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) + + -- 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 feature -- Hooks @@ -136,6 +134,7 @@ feature -- Hooks local lnk: CMS_LOCAL_LINK do + -- Add the link to the blog to the main menu create lnk.make ("Blogs", "/blogs/") a_menu_system.primary_menu.extend (lnk) end diff --git a/examples/demo/modules/blog/handler/blog_handler.e b/examples/demo/modules/blog/handler/blog_handler.e index 66efe1a..1b8c359 100644 --- a/examples/demo/modules/blog/handler/blog_handler.e +++ b/examples/demo/modules/blog/handler/blog_handler.e @@ -12,9 +12,18 @@ inherit WSF_URI_HANDLER rename + execute as uri_execute, new_mapping as new_uri_mapping 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 redefine do_get @@ -33,6 +42,18 @@ feature -- execute execute_methods (req, res) 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 entries_per_page : INTEGER -- 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 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 do_get (req: WSF_REQUEST; res: WSF_RESPONSE) -- local l_page: CMS_RESPONSE + l_posts: LIST[CMS_NODE] s: STRING n: CMS_NODE lnk: CMS_LOCAL_LINK hdate: HTTP_DATE + page_number:NATURAL_16 do -- At the moment the template is hardcoded, but we can -- get them from the configuration file and load them into -- 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) l_page.add_variable (node_api.nodes, "nodes") - -- NOTE: for development purposes we have the following hardcode output. - create s.make_from_string ("

Blog

") - if attached node_api.blogs_order_created_desc as lst then + -- Output the title. If more than one page, also output the current page number + create s.make_from_string ("

Blog") + 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 ("

") + + + 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 --if n.content_type.is_equal ("blog") then s.append ("
    %N") @@ -113,5 +161,19 @@ feature -- HTTP Methods l_page.execute 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 diff --git a/examples/demo/modules/blog/persistence/cms_blog_storage_i.e b/examples/demo/modules/blog/persistence/cms_blog_storage_i.e index 21d081a..334f6b3 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_i.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_i.e @@ -21,4 +21,9 @@ feature -- Access deferred 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 diff --git a/examples/demo/modules/blog/persistence/cms_blog_storage_null.e b/examples/demo/modules/blog/persistence/cms_blog_storage_null.e index fff5c0d..328fc69 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_null.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_null.e @@ -28,4 +28,9 @@ feature -- Access create {ARRAYED_LIST [CMS_NODE]} Result.make (0) 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 diff --git a/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e b/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e index e39f103..c7f8407 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e @@ -49,6 +49,32 @@ feature -- Access 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 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 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