diff --git a/modules/blog/cms_blog_api.e b/modules/blog/cms_blog_api.e index ba9cde0..8c2608b 100644 --- a/modules/blog/cms_blog_api.e +++ b/modules/blog/cms_blog_api.e @@ -41,11 +41,7 @@ feature {NONE} -- Initialization create {CMS_BLOG_STORAGE_NULL} blog_storage.make end - --| For test reasons this is 2, so we don't have to create a lot of blog entries. - --| TODO: Set to bigger constant. - entries_per_page := 6 - --- initialize_node_types + default_entries_count_per_page := 8 -- Default FIXME: find a way to make it customizable. end feature {CMS_API_ACCESS, CMS_MODULE, CMS_API} -- Restricted access @@ -58,7 +54,7 @@ feature {CMS_MODULE} -- Access nodes storage. feature -- Configuration of blog handlers - entries_per_page : NATURAL_32 + default_entries_count_per_page : NATURAL_32 -- The numbers of posts that are shown on one page. If there are more post a pagination is generated feature -- Access node diff --git a/modules/blog/cms_blog_module.e b/modules/blog/cms_blog_module.e index 20ae020..f00f513 100644 --- a/modules/blog/cms_blog_module.e +++ b/modules/blog/cms_blog_module.e @@ -115,7 +115,6 @@ feature -- Access: router 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_blog_api) create l_blog_user_handler.make (a_api, a_blog_api) @@ -127,12 +126,10 @@ feature -- Access: router a_router.handle ("/blogs/page/{page}", l_blog_handler, a_router.methods_get) -- If a user id is given route with blog user handler - --| FIXME: maybe /user/{user}/blogs/ would be better. - a_router.handle ("/blogs/user/{user}", l_blog_user_handler, a_router.methods_get) + a_router.handle ("/blog/{user}", l_blog_user_handler, a_router.methods_get) -- If a user id is given we also want to allow different pages - --| FIXME: what about /user/{user}/blogs/?page={page} ? - a_router.handle ("/blogs/user/{user}/page/{page}", l_blog_user_handler, a_router.methods_get) + a_router.handle ("/blog/{user}", l_blog_user_handler, a_router.methods_get) end diff --git a/modules/blog/handler/blog_handler.e b/modules/blog/handler/blog_handler.e index 98522ea..9f393b2 100644 --- a/modules/blog/handler/blog_handler.e +++ b/modules/blog/handler/blog_handler.e @@ -1,6 +1,6 @@ note - description: "Request handler related to /blogs and /blogs/{page}. Displays all posts in the blog." - author: "Dario Bösch " + description: "Request handler related to /blogs and /blogs/?page={page}. Displays all posts in the blog." + contributor: "Dario Bösch " date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $" revision: "$9661667$" @@ -69,7 +69,8 @@ feature -- HTTP Methods l_page: CMS_RESPONSE do -- Read page number from path parameter. - page_number := page_number_path_parameter (req) + page_number := page_number_parameter (req) + entries_per_page := size_parameter (req) -- Responding with `main_content_html (l_page)'. create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api) @@ -94,6 +95,19 @@ feature -- Query Result := entries_per_page < total_entries end + size_parameter (req: WSF_REQUEST): NATURAL_32 + -- Page size value from req, otherwise use default. + do + if + attached {WSF_STRING} req.query_parameter ("size") as p_size and then + p_size.is_integer + then + Result := p_size.integer_value.to_natural_32 + else + Result := blog_api.default_entries_count_per_page + end + end + pages_count: NATURAL_32 -- Number of pages needed to display all posts. require @@ -105,14 +119,14 @@ feature -- Query Result := tmp.ceiling.to_natural_32 end - page_number_path_parameter (req: WSF_REQUEST): NATURAL_32 - -- Page number from path /blogs/{page}. + page_number_parameter (req: WSF_REQUEST): NATURAL_32 + -- Page number from path /blogs/?page={page}. -- Unsigned integer since negative pages are not allowed. local - s: STRING + s: READABLE_STRING_32 do Result := 1 -- default if not get variable is set - if attached {WSF_STRING} req.path_parameter ("page") as p_page then + if attached {WSF_STRING} req.query_parameter ("page") as p_page then s := p_page.value if s.is_natural_32 then if s.to_natural_32 > 1 then @@ -206,7 +220,7 @@ feature -- HTML Output do if attached n.author as l_author then a_output.append ("by ") - a_output.append ("" + html_encoded (l_author.name) + "") + a_output.append ("" + html_encoded (l_author.name) + "") end end @@ -251,7 +265,7 @@ feature -- HTML Output -- If exist older posts show link to next page if page_number < pages_count then tmp := page_number + 1 - a_output.append (" << Older Posts ") + a_output.append (" << Older Posts ") end -- Delimiter @@ -262,7 +276,7 @@ feature -- HTML Output -- If exist newer posts show link to previous page if page_number > 1 then tmp := page_number -1 - a_output.append (" Newer Posts >> ") + a_output.append (" Newer Posts >> ") end a_output.append ("") diff --git a/modules/blog/handler/blog_user_handler.e b/modules/blog/handler/blog_user_handler.e index 8a50809..e9c979b 100644 --- a/modules/blog/handler/blog_user_handler.e +++ b/modules/blog/handler/blog_user_handler.e @@ -1,14 +1,16 @@ note description: "[ Request handler related to - /blogs/user/{id}/ - or /blogs/user/{id}/page/{page}. + /blog/{user}/ + or /blog/{user}?page={page}. Displays all posts of the given user + ]" - author: "Dario Bösch " + contributor: "Dario Bösch " date: "$Date: 2015-05-22 15:13:00 +0100 (lun., 18 mai 2015) $" revision: "$Revision 96616$" + fixme: "redesign pagination... see CMS_PAGINATION_GENERATOR." class BLOG_USER_HANDLER @@ -28,7 +30,7 @@ create feature -- Global Variables - user : detachable CMS_USER + user: detachable CMS_USER feature -- HTTP Methods @@ -37,7 +39,7 @@ feature -- HTTP Methods local l_error: NOT_FOUND_ERROR_CMS_RESPONSE do - user := Void + check user_void: user = Void end if attached user_from_request (req) as l_user then user := l_user -- Output the results, similar as in the blog hanlder (but with other queries) @@ -45,53 +47,47 @@ feature -- HTTP Methods else -- Throw a bad request error because the user is not valid create l_error.make (req, res, api) - l_error.set_main_content ("

Error

User with id " + user_id_path_parameter (req).out + " doesn't exist!") + if attached user_parameter (req) as l_user_id then + l_error.set_main_content ("

Error

User with id " + api.html_encoded (l_user_id) + " not found!") + else + l_error.set_main_content ("

Error

User not found!") + end l_error.execute end + user := Void end feature -- Query - user_valid (req: WSF_REQUEST) : BOOLEAN - -- Returns true if a valid user id is given and a user with this id exists, - -- otherwise returns false. - local - user_id: INTEGER_32 - do - user_id := user_id_path_parameter (req) - - if user_id <= 0 then - -- Given user id is not valid - Result := False - else - --Check if user with user_id exists - Result := api.user_api.user_by_id (user_id) /= Void - end - end - user_from_request (req: WSF_REQUEST): detachable CMS_USER -- Eventual user with given id in the path of request `req'. local - uid: like user_id_path_parameter + uid: INTEGER_64 do - uid := user_id_path_parameter (req) - if uid > 0 then - Result := api.user_api.user_by_id (uid) + if + attached user_parameter (req) as l_user_id and then + not l_user_id.is_whitespace + then + if l_user_id.is_integer_64 then + uid := l_user_id.to_integer_64 + if uid > 0 then + Result := api.user_api.user_by_id (uid) + end + else + Result := api.user_api.user_by_name (l_user_id) + end else -- Missing or invalid user id. end end - user_id_path_parameter (req: WSF_REQUEST): INTEGER_32 - -- User id from path /blogs/{user}. + user_parameter (req: WSF_REQUEST): detachable STRING_32 + -- User id from path /blog/{user}. -- Unsigned integer since negative ids are not allowed. -- If no valid id can be read it returns -1 do - Result := -1 if attached {WSF_STRING} req.path_parameter ("user") as l_user_id then - if l_user_id.is_integer then - Result := l_user_id.integer_value - end + Result := l_user_id.value end end @@ -139,9 +135,9 @@ feature -- HTML Output -- If user is logged in, include user id do if attached user as l_user then - Result := "/blogs/user/" + l_user.id.out + Result := "/blog/" + l_user.id.out else - Result := precursor + Result := Precursor end end diff --git a/modules/blog/handler/cms_blog_handler.e b/modules/blog/handler/cms_blog_handler.e index 450096f..10f222b 100644 --- a/modules/blog/handler/cms_blog_handler.e +++ b/modules/blog/handler/cms_blog_handler.e @@ -1,6 +1,6 @@ note description: "Deferred request handler related to /blogs/... Has an own blog api." - author: "Dario Bösch " + contributor: "Dario Bösch " date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $" revision: "$9661667$" @@ -11,13 +11,19 @@ inherit CMS_MODULE_HANDLER [CMS_BLOG_API] rename module_api as blog_api + redefine + make end +feature {NONE} -- Initialization + + make (a_api: CMS_API; a_module_api: CMS_BLOG_API) + do + Precursor (a_api, a_module_api) + entries_per_page := a_module_api.default_entries_count_per_page + end feature -- Access entries_per_page: NATURAL_32 - do - Result := blog_api.entries_per_page - end end