From 601b88ab367247e764b19c59e10e3da8ec980d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20B=C3=B6sch?= Date: Fri, 22 May 2015 15:34:32 +0200 Subject: [PATCH] blog handler optimized, blog user handler created --- examples/demo/modules/blog/cms_blog_api.e | 2 +- examples/demo/modules/blog/cms_blog_config.e | 4 +- examples/demo/modules/blog/cms_blog_module.e | 12 +- .../demo/modules/blog/handler/blog_handler.e | 166 ++++++++++-------- .../modules/blog/handler/blog_user_handler.e | 16 ++ .../blog/persistence/cms_blog_storage_i.e | 2 +- .../blog/persistence/cms_blog_storage_null.e | 2 +- .../blog/persistence/cms_blog_storage_sql.e | 2 +- 8 files changed, 121 insertions(+), 85 deletions(-) create mode 100644 examples/demo/modules/blog/handler/blog_user_handler.e diff --git a/examples/demo/modules/blog/cms_blog_api.e b/examples/demo/modules/blog/cms_blog_api.e index 4442bb7..4c279c3 100644 --- a/examples/demo/modules/blog/cms_blog_api.e +++ b/examples/demo/modules/blog/cms_blog_api.e @@ -49,7 +49,7 @@ feature -- Access node Result := add_authors(node_storage.blogs) end - blogs_order_created_desc_limited (a_limit:INTEGER_32; a_offset:INTEGER_32) : LIST[CMS_NODE] + blogs_order_created_desc_limited (a_limit:NATURAL_32; a_offset:NATURAL_32) : LIST[CMS_NODE] -- List of nodes ordered by creation date and limited by limit and offset local tmp: LIST[CMS_NODE] diff --git a/examples/demo/modules/blog/cms_blog_config.e b/examples/demo/modules/blog/cms_blog_config.e index 61a3d87..6be55d8 100644 --- a/examples/demo/modules/blog/cms_blog_config.e +++ b/examples/demo/modules/blog/cms_blog_config.e @@ -8,9 +8,9 @@ class CMS_BLOG_CONFIG -feature {BLOG_HANDLER}-- Configuration of blog handler +feature -- Configuration of blog handlers - entries_per_page : INTEGER + entries_per_page : NATURAL_32 -- The numbers of posts that are shown on one page. If there are more post a pagination is generated do -- For test reasons this is 2, so we don't have to create a lot of blog entries. diff --git a/examples/demo/modules/blog/cms_blog_module.e b/examples/demo/modules/blog/cms_blog_module.e index f2e795d..8e1f48e 100644 --- a/examples/demo/modules/blog/cms_blog_module.e +++ b/examples/demo/modules/blog/cms_blog_module.e @@ -1,7 +1,8 @@ note - description: "Summary description for {CMS_BLOG_MODULE}." - date: "$Date: 2015-02-13 13:08:13 +0100 (ven., 13 févr. 2015) $" - revision: "$Revision: 96616 $" + description: "Displays all posts (pages with type blog). It's possible to list posts by user." + author: "Dario Bösch " + date: "$Date: 2015-05-22 15:13:00 +0100 (lun., 18 mai 2015) $" + revision: "$Revision 96616$" class CMS_BLOG_MODULE @@ -109,10 +110,12 @@ feature -- Access: router configure_web (a_api: CMS_API; a_node_api: CMS_BLOG_API; a_router: WSF_ROUTER) local l_blog_handler: BLOG_HANDLER + l_blog_user_handler: BLOG_USER_HANDLER l_uri_mapping: WSF_URI_MAPPING 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_blog_user_handler.make (a_api, a_node_api) -- Let the class BLOG_HANDLER handle the requests on "/blogs" create l_uri_mapping.make_trailing_slash_ignored("/blogs", l_blog_handler) @@ -121,6 +124,9 @@ configure_web (a_api: CMS_API; a_node_api: CMS_BLOG_API; a_router: WSF_ROUTER) -- We can add a page number after /blogs/ to get older posts a_router.handle_with_request_methods ("/blogs/page/{page}", l_blog_handler, a_router.methods_get) + -- If a user id is given route with blog user handler + a_router.handle_with_request_methods ("/blogs/user/{user}", l_blog_user_handler, a_router.methods_get) + end feature -- Hooks diff --git a/examples/demo/modules/blog/handler/blog_handler.e b/examples/demo/modules/blog/handler/blog_handler.e index 72ed0d7..354496c 100644 --- a/examples/demo/modules/blog/handler/blog_handler.e +++ b/examples/demo/modules/blog/handler/blog_handler.e @@ -1,8 +1,8 @@ note description: "Request handler related to /blogs." author: "Dario Bösch " - date: "$Date: 2015-05-18 13:49:99 +0100 (lun., 18 mai 2015) $" - revision: "$966167$" + date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $" + revision: "$9661667$" class BLOG_HANDLER @@ -64,114 +64,128 @@ feature -- HTTP Methods -- local l_page: CMS_RESPONSE - s: STRING - n: CMS_NODE - lnk: CMS_LOCAL_LINK - page_number:NATURAL_16 + page_number:NATURAL_32 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 and then "" interpreted as 0) - if page_number = 0 then page_number := 1 end + -- Read the page number from get variable + page_number := page_number_path_parameter (req) create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api) l_page.add_variable (node_api.nodes, "nodes") - - - -- 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 + " of " + pages.out + ")") - -- Get the posts from the current page (limited by entries per page) - end - s.append ("

") - - -- Get the posts from the current page (given by page number and entries per page) - if attached node_api.blogs_order_created_desc_limited (entries_per_page, (page_number-1) * entries_per_page) as lst then - - -- List all posts of the blog - s.append ("
    %N") - across - lst as ic - loop - n := ic.item - lnk := node_api.node_link (n) - s.append ("
  • ") - - -- Output the creation date - s.append (creation_date_html(n)) - - -- Output the author of the post - s.append (author_html(n)) - - -- Output the title of the post as a link (to the detail page) - s.append (title_html(n, l_page)) - - -- Output the summary of the post and a more link to the detail page - s.append (summary_html(n, l_page)) - - s.append ("
  • %N") - end - - -- End of post list - s.append ("
%N") - - -- Pagination - s.append (pagination_html(page_number)) - - --end - end - - l_page.set_main_content (s) + l_page.set_main_content (main_content_html(page_number, l_page)) l_page.add_block (create {CMS_CONTENT_BLOCK}.make ("nodes_warning", Void, "/blogs/ is not yet fully implemented
", Void), "highlighted") l_page.execute end -feature {NONE} -- Query +feature -- Query + + posts (page_number: NATURAL_32) : LIST[CMS_NODE] + -- The posts to list on the given page + do + Result := node_api.blogs_order_created_desc_limited (entries_per_page, (page_number-1) * entries_per_page) + 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 + Result := entries_per_page < total_entries end - pages : NATURAL_16 + pages : NATURAL_32 -- Returns the number of pages needed to display all posts require entries_per_page > 0 local tmp: REAL_32 do - tmp := node_api.blogs_count.to_real / entries_per_page.to_real; - Result := tmp.ceiling.to_natural_16 + tmp := total_entries.to_real_32 / entries_per_page.to_real_32; + Result := tmp.ceiling.to_natural_32 end - 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 + page_number_path_parameter (req: WSF_REQUEST): NATURAL_32 + -- Returns the page number from the path /blogs/{page}. It's an unsigned integer since negative pages are not allowed local s: STRING do + Result := 1 -- default if not get variable is set 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 + if s.is_natural_32 then + if s.to_natural_32 > 1 then + Result := s.to_natural_32 + end end end end -feature {NONE} -- HTML Output + total_entries : NATURAL_32 + -- Returns the number of total entries/posts + do + Result := node_api.blogs_count.to_natural_32 + end + +feature -- HTML Output + + main_content_html (page_number: NATURAL_32; page: CMS_RESPONSE) : STRING + -- Return the content of the page as a html string + local + n: CMS_NODE + lnk: CMS_LOCAL_LINK + do + -- Output the title. If more than one page, also output the current page number + create Result.make_from_string (page_title_html(page_number)) + + -- Get the posts from the current page (given by page number and entries per page) + if attached posts(page_number) as lst then + + -- List all posts of the blog + Result.append ("
    %N") + across + lst as ic + loop + n := ic.item + lnk := node_api.node_link (n) + Result.append ("
  • ") + + -- Output the creation date + Result.append (creation_date_html(n)) + + -- Output the author of the post + Result.append (author_html(n)) + + -- Output the title of the post as a link (to the detail page) + Result.append (title_html(n, page)) + + -- Output the summary of the post and a more link to the detail page + Result.append (summary_html(n, page)) + + Result.append ("
  • %N") + end + + -- End of post list + Result.append ("
%N") + + -- Pagination + Result.append (pagination_html(page_number)) + + --end + end + end + + page_title_html (page_number: NATURAL_32) : STRING + -- Returns the title of the page as a html string. It shows the current page number + do + create Result.make_from_string ("

Blog") + if more_than_one_page then + Result.append (" (Page " + page_number.out + " of " + pages.out + ")") + -- Get the posts from the current page (limited by entries per page) + end + Result.append ("

") + end creation_date_html (n: CMS_NODE) : STRING -- returns the creation date. At the moment hard coded as html @@ -227,10 +241,10 @@ feature {NONE} -- HTML Output end end - pagination_html (page_number: NATURAL_16) : STRING + pagination_html (page_number: NATURAL_32) : STRING -- returns a html string with the pagination links (if necessary) local - tmp: NATURAL_16 + tmp: NATURAL_32 do Result := "" if more_than_one_page then diff --git a/examples/demo/modules/blog/handler/blog_user_handler.e b/examples/demo/modules/blog/handler/blog_user_handler.e new file mode 100644 index 0000000..ce065a9 --- /dev/null +++ b/examples/demo/modules/blog/handler/blog_user_handler.e @@ -0,0 +1,16 @@ +note + description: "Request handler related to /blogs/user/{id}/. Displays all posts of the given user" + author: "Dario Bösch " + date: "$Date: 2015-05-22 15:13:00 +0100 (lun., 18 mai 2015) $" + revision: "$Revision 96616$" + +class + BLOG_USER_HANDLER + +inherit + BLOG_HANDLER + +create + make + +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 334f6b3..258052a 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_i.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_i.e @@ -21,7 +21,7 @@ feature -- Access deferred end - blogs_limited (limit:INTEGER_32; offset:INTEGER_32) : LIST[CMS_NODE] + blogs_limited (limit:NATURAL_32; offset:NATURAL_32) : LIST[CMS_NODE] -- List of nodes ordered by creation date from limit to limit + offset deferred 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 328fc69..4ab0f0c 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_null.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_null.e @@ -28,7 +28,7 @@ feature -- Access create {ARRAYED_LIST [CMS_NODE]} Result.make (0) end - blogs_limited (limit:INTEGER_32; offset:INTEGER_32) : LIST[CMS_NODE] + blogs_limited (limit:NATURAL_32; offset:NATURAL_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) 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 5dcbf6a..758e6ba 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e @@ -49,7 +49,7 @@ feature -- Access end end - blogs_limited (a_limit:INTEGER_32; a_offset:INTEGER_32) : LIST[CMS_NODE] + blogs_limited (a_limit:NATURAL_32; a_offset:NATURAL_32) : LIST[CMS_NODE] -- List of nodes ordered by creation date from limit to limit + offset local l_parameters: STRING_TABLE [detachable ANY]