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 ("