From 0bd75e7c59515857025077ad2eb5929d8c5bfb40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20B=C3=B6sch?= Date: Fri, 22 May 2015 17:31:30 +0200 Subject: [PATCH] Added list of posts of a specific user Similar to the blog handler the blog user handler routes /blogs/users/{id}. Pagination is implemented as well --- examples/demo/modules/blog/cms_blog_api.e | 16 +++ examples/demo/modules/blog/cms_blog_module.e | 3 + .../demo/modules/blog/handler/blog_handler.e | 13 +- .../modules/blog/handler/blog_user_handler.e | 123 ++++++++++++++++++ .../blog/persistence/cms_blog_storage_i.e | 12 +- .../blog/persistence/cms_blog_storage_null.e | 13 +- .../blog/persistence/cms_blog_storage_sql.e | 52 +++++++- src/persistence/sql/cms_storage_sql_builder.e | 5 + 8 files changed, 230 insertions(+), 7 deletions(-) diff --git a/examples/demo/modules/blog/cms_blog_api.e b/examples/demo/modules/blog/cms_blog_api.e index 4c279c3..95a9509 100644 --- a/examples/demo/modules/blog/cms_blog_api.e +++ b/examples/demo/modules/blog/cms_blog_api.e @@ -43,6 +43,12 @@ feature -- Access node Result := node_storage.blogs_count end + blogs_count_from_user (user_id: INTEGER_64) : INTEGER_64 + -- Number of nodes of type blog from user with user_id + do + Result := node_storage.blogs_count_from_user(user_id) + end + blogs_order_created_desc: LIST[CMS_NODE] -- List of nodes ordered by creation date (descending) do @@ -59,6 +65,16 @@ feature -- Access node end + blogs_from_user_order_created_desc_limited (a_user_id: INTEGER_32; 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] + do + -- load all posts and add the authors to each post + Result := add_authors(node_storage.blogs_from_user_limited (a_user_id, a_limit, a_offset)) + + end + feature {NONE} -- Helpers add_authors(posts: LIST[CMS_NODE]) : LIST[CMS_NODE] diff --git a/examples/demo/modules/blog/cms_blog_module.e b/examples/demo/modules/blog/cms_blog_module.e index 8e1f48e..65c47a1 100644 --- a/examples/demo/modules/blog/cms_blog_module.e +++ b/examples/demo/modules/blog/cms_blog_module.e @@ -127,6 +127,9 @@ configure_web (a_api: CMS_API; a_node_api: CMS_BLOG_API; a_router: WSF_ROUTER) -- 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) + -- If a user id is given we also want to allow different pages + a_router.handle_with_request_methods ("/blogs/user/{user}/page/{page}", 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 e818a23..c0a04ab 100644 --- a/examples/demo/modules/blog/handler/blog_handler.e +++ b/examples/demo/modules/blog/handler/blog_handler.e @@ -75,7 +75,6 @@ feature -- HTTP Methods 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") l_page.set_main_content (main_content_html(l_page)) l_page.execute end @@ -88,7 +87,7 @@ feature -- Query 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 @@ -255,7 +254,7 @@ feature -- HTML Output -- If exist older posts show link to next page if page_number < pages then tmp := page_number + 1 - Result.append (" << Older Posts ") + Result.append (" << Older Posts ") end -- Delmiter @@ -266,7 +265,7 @@ feature -- HTML Output -- If exist newer posts show link to previous page if page_number > 1 then tmp := page_number -1 - Result.append (" Newer Posts >> ") + Result.append (" Newer Posts >> ") end Result.append ("") @@ -275,4 +274,10 @@ feature -- HTML Output end + base_path : STRING + -- the path to the page that lists all blogs + do + Result := "/blogs" + end + end diff --git a/examples/demo/modules/blog/handler/blog_user_handler.e b/examples/demo/modules/blog/handler/blog_user_handler.e index ce065a9..d20667a 100644 --- a/examples/demo/modules/blog/handler/blog_user_handler.e +++ b/examples/demo/modules/blog/handler/blog_user_handler.e @@ -9,8 +9,131 @@ class inherit BLOG_HANDLER + redefine + do_get, + posts, + total_entries, + page_title_html, + base_path + end create make + +feature -- Global Variables + + user : detachable CMS_USER + +feature -- HTTP Methods + + do_get (req: WSF_REQUEST; res: WSF_RESPONSE) + -- + local + l_error: GENERIC_VIEW_CMS_RESPONSE + do + -- Check if userID valid + if user_valid (req) then + user := load_user(req) + precursor(req, res) + else + -- Throw a bad request error if the user is not valid + create l_error.make (req, res, api) + l_error.set_main_content ("

Error

User with id " + user_path_parameter(req).out + " doesn't exist!") + l_error.execute + end + + 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_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 + + load_user (req: WSF_REQUEST) : detachable CMS_USER + -- Returnes the user with the given id in the request req + require + user_valid(req) + do + Result := api.user_api.user_by_id (user_path_parameter(req)) + end + + user_path_parameter (req: WSF_REQUEST): INTEGER_32 + -- Returns the user id from the path /blogs/{user}. It's an unsigned integer since negative ids are not allowed. If no valid id can be read it returns -1 + local + s: STRING + do + if attached {WSF_STRING} req.path_parameter ("user") as user_id then + s := user_id.value + if s.is_integer_32 then + if s.to_integer_32 > 0 then + Result := s.to_integer_32 + end + end + end + end + + posts : LIST[CMS_NODE] + -- The posts to list on the given page + do + if attached user as l_user then + Result := node_api.blogs_from_user_order_created_desc_limited (l_user.id.to_integer_32, entries_per_page, (page_number-1) * entries_per_page) + else + create {ARRAYED_LIST [CMS_NODE]} Result.make (0) + end + end + + total_entries : NATURAL_32 + -- Returns the number of total entries/posts + do + if attached user as l_user then + Result := node_api.blogs_count_from_user(l_user.id).to_natural_32 + else + Result := precursor + end + + end + +feature -- HTML Output + + page_title_html : STRING + -- Returns the title of the page as a html string. It shows the current page number + do + create Result.make_from_string ("

Posts from ") + if attached user as l_user then + Result.append(l_user.name) + else + Result.append ("unknown user") + end + 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 + + base_path : STRING + -- the path to the page that lists all blogs + do + if attached user as l_user then + Result := "/blogs/user/" + l_user.id.out + else + Result := precursor + 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 258052a..8fd8ca7 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_i.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_i.e @@ -16,13 +16,23 @@ feature -- Access deferred end + blogs_count_from_user (user_id: INTEGER_64) : INTEGER_64 + -- Number of nodes of type blog from user with user_id + deferred + end + blogs: LIST [CMS_NODE] -- List of nodes ordered by creation date (descending). deferred end blogs_limited (limit:NATURAL_32; offset:NATURAL_32) : LIST[CMS_NODE] - -- List of nodes ordered by creation date from limit to limit + offset + -- List of posts ordered by creation date from offset to offset + limit + deferred + end + + blogs_from_user_limited (user_id: INTEGER_32; limit:NATURAL_32; offset:NATURAL_32) : LIST[CMS_NODE] + -- List of posts from user_id ordered by creation date from offset to offset + limit 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 4ab0f0c..d208190 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_null.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_null.e @@ -22,6 +22,11 @@ feature -- Access do end + blogs_count_from_user (user_id: INTEGER_64) : INTEGER_64 + -- Number of nodes of type blog from user with user_id + do + end + blogs: LIST[CMS_NODE] -- List of nodes. do @@ -29,7 +34,13 @@ feature -- Access end blogs_limited (limit:NATURAL_32; offset:NATURAL_32) : LIST[CMS_NODE] - -- List of nodes ordered by creation date from limit to limit + offset + -- List of posts ordered by creation date from offset to offset + limit + do + create {ARRAYED_LIST [CMS_NODE]} Result.make (0) + end + + blogs_from_user_limited (user_id: INTEGER_32; limit:NATURAL_32; offset:NATURAL_32) : LIST[CMS_NODE] + -- List of posts from user_id ordered by creation date from offset to offset + limit do create {ARRAYED_LIST [CMS_NODE]} Result.make (0) 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 758e6ba..915ad20 100644 --- a/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e +++ b/examples/demo/modules/blog/persistence/cms_blog_storage_sql.e @@ -28,6 +28,21 @@ feature -- Access end end + blogs_count_from_user (user_id: INTEGER_64) : INTEGER_64 + -- Number of nodes of type blog from user with user_id + local + l_parameters: STRING_TABLE [detachable ANY] + do + error_handler.reset + write_information_log (generator + ".nodes_count") + create l_parameters.make (2) + l_parameters.put (user_id, "user") + sql_query (sql_select_blog_count_from_user, l_parameters) + if sql_rows_count = 1 then + Result := sql_read_integer_64 (1) + end + end + blogs: LIST [CMS_NODE] -- List of nodes ordered by creation date (descending). do @@ -75,16 +90,51 @@ feature -- Access end end + blogs_from_user_limited (a_user_id:INTEGER_32; a_limit:NATURAL_32; a_offset:NATURAL_32) : LIST[CMS_NODE] + -- List of posts of the author with a_user_id ordered by creation date starting at offset and limited by limit + 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") + l_parameters.put (a_user_id, "user") + sql_query (sql_blogs_from_user_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%";" -- Nodes count (Published and not Published) --| note: {CMS_NODE_API}.trashed = -1 + sql_select_blog_count_from_user: STRING = "SELECT count(*) FROM Nodes WHERE status != -1 AND type = %"blog%" AND author = :user ;" + -- Nodes count (Published and not Published) + --| note: {CMS_NODE_API}.trashed = -1 + 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 + --- SQL Query to retrieve all node of type "blog" limited by limit and starting at offset + + sql_blogs_from_user_limited: STRING = "SELECT * FROM Nodes WHERE status != -1 AND type = %"blog%" AND author = :user ORDER BY created DESC LIMIT :limit OFFSET :offset ;" + --- SQL Query to retrieve all node of type "blog" from author with id limited by limit + offset + end diff --git a/src/persistence/sql/cms_storage_sql_builder.e b/src/persistence/sql/cms_storage_sql_builder.e index 6000207..8c9fe69 100644 --- a/src/persistence/sql/cms_storage_sql_builder.e +++ b/src/persistence/sql/cms_storage_sql_builder.e @@ -36,6 +36,11 @@ feature -- Initialization create u.make ("admin") u.set_password ("istrator#") u.set_email (a_setup.site_email) + a_storage.new_user (u) + + create u.make ("daboesch") + u.set_password ("eiffel") + u.set_email ("daboesch@student.ethz.ch") a_storage.new_user (u) --| Node