blog handler optimized, blog user handler created

This commit is contained in:
Dario Bösch
2015-05-22 15:34:32 +02:00
parent 2b0e1a2b84
commit 601b88ab36
8 changed files with 121 additions and 85 deletions

View File

@@ -49,7 +49,7 @@ feature -- Access node
Result := add_authors(node_storage.blogs) Result := add_authors(node_storage.blogs)
end 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 -- List of nodes ordered by creation date and limited by limit and offset
local local
tmp: LIST[CMS_NODE] tmp: LIST[CMS_NODE]

View File

@@ -8,9 +8,9 @@ class
CMS_BLOG_CONFIG 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 -- The numbers of posts that are shown on one page. If there are more post a pagination is generated
do do
-- For test reasons this is 2, so we don't have to create a lot of blog entries. -- For test reasons this is 2, so we don't have to create a lot of blog entries.

View File

@@ -1,7 +1,8 @@
note note
description: "Summary description for {CMS_BLOG_MODULE}." description: "Displays all posts (pages with type blog). It's possible to list posts by user."
date: "$Date: 2015-02-13 13:08:13 +0100 (ven., 13 févr. 2015) $" author: "Dario B<>sch <daboesch@student.ethz.ch>"
revision: "$Revision: 96616 $" date: "$Date: 2015-05-22 15:13:00 +0100 (lun., 18 mai 2015) $"
revision: "$Revision 96616$"
class class
CMS_BLOG_MODULE 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) configure_web (a_api: CMS_API; a_node_api: CMS_BLOG_API; a_router: WSF_ROUTER)
local local
l_blog_handler: BLOG_HANDLER l_blog_handler: BLOG_HANDLER
l_blog_user_handler: BLOG_USER_HANDLER
l_uri_mapping: WSF_URI_MAPPING l_uri_mapping: WSF_URI_MAPPING
do do
-- TODO: for now, focused only on web interface, add REST api later. [2015-May-18] -- 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_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" -- Let the class BLOG_HANDLER handle the requests on "/blogs"
create l_uri_mapping.make_trailing_slash_ignored("/blogs", l_blog_handler) 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 -- 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) 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 end
feature -- Hooks feature -- Hooks

View File

@@ -1,8 +1,8 @@
note note
description: "Request handler related to /blogs." description: "Request handler related to /blogs."
author: "Dario B<>sch <daboesch@student.ethz.ch>" author: "Dario B<>sch <daboesch@student.ethz.ch>"
date: "$Date: 2015-05-18 13:49:99 +0100 (lun., 18 mai 2015) $" date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $"
revision: "$966167$" revision: "$9661667$"
class class
BLOG_HANDLER BLOG_HANDLER
@@ -64,114 +64,128 @@ feature -- HTTP Methods
-- <Precursor> -- <Precursor>
local local
l_page: CMS_RESPONSE l_page: CMS_RESPONSE
s: STRING page_number:NATURAL_32
n: CMS_NODE
lnk: CMS_LOCAL_LINK
page_number:NATURAL_16
do do
-- At the moment the template is hardcoded, but we can -- At the moment the template is hardcoded, but we can
-- get them from the configuration file and load them into -- get them from the configuration file and load them into
-- the setup class. -- the setup class.
--Check if a page is given, if not start with page 1 -- Read the page number from get variable
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) 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
create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api) create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api)
l_page.add_variable (node_api.nodes, "nodes") l_page.add_variable (node_api.nodes, "nodes")
l_page.set_main_content (main_content_html(page_number, l_page))
-- Output the title. If more than one page, also output the current page number
create s.make_from_string ("<h2>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 ("</h2>")
-- 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 ("<ul class=%"cms_blog_nodes%">%N")
across
lst as ic
loop
n := ic.item
lnk := node_api.node_link (n)
s.append ("<li class=%"cms_type_"+ n.content_type +"%">")
-- 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 ("</li>%N")
end
-- End of post list
s.append ("</ul>%N")
-- Pagination
s.append (pagination_html(page_number))
--end
end
l_page.set_main_content (s)
l_page.add_block (create {CMS_CONTENT_BLOCK}.make ("nodes_warning", Void, "/blogs/ is not yet fully implemented<br/>", Void), "highlighted") l_page.add_block (create {CMS_CONTENT_BLOCK}.make ("nodes_warning", Void, "/blogs/ is not yet fully implemented<br/>", Void), "highlighted")
l_page.execute l_page.execute
end 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 more_than_one_page : BOOLEAN
-- Checks if all posts fit on one page (FALSE) or if more than one page is needed (TRUE) -- Checks if all posts fit on one page (FALSE) or if more than one page is needed (TRUE)
do do
Result := entries_per_page < node_api.blogs_count Result := entries_per_page < total_entries
end end
pages : NATURAL_16 pages : NATURAL_32
-- Returns the number of pages needed to display all posts -- Returns the number of pages needed to display all posts
require require
entries_per_page > 0 entries_per_page > 0
local local
tmp: REAL_32 tmp: REAL_32
do do
tmp := node_api.blogs_count.to_real / entries_per_page.to_real; tmp := total_entries.to_real_32 / entries_per_page.to_real_32;
Result := tmp.ceiling.to_natural_16 Result := tmp.ceiling.to_natural_32
end end
page_number_path_parameter (req: WSF_REQUEST): NATURAL_16 page_number_path_parameter (req: WSF_REQUEST): NATURAL_32
-- Returns the page number from the path /blogs/{page}. It's an unsigned integere since negative pages are not allowed -- Returns the page number from the path /blogs/{page}. It's an unsigned integer since negative pages are not allowed
local local
s: STRING s: STRING
do 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.path_parameter ("page") as p_page then
s := p_page.value s := p_page.value
if s.is_natural_16 then if s.is_natural_32 then
Result := s.to_natural_16 if s.to_natural_32 > 1 then
Result := s.to_natural_32
end
end end
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 ("<ul class=%"cms_blog_nodes%">%N")
across
lst as ic
loop
n := ic.item
lnk := node_api.node_link (n)
Result.append ("<li class=%"cms_type_"+ n.content_type +"%">")
-- 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 ("</li>%N")
end
-- End of post list
Result.append ("</ul>%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 ("<h2>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 ("</h2>")
end
creation_date_html (n: CMS_NODE) : STRING creation_date_html (n: CMS_NODE) : STRING
-- returns the creation date. At the moment hard coded as html -- returns the creation date. At the moment hard coded as html
@@ -227,10 +241,10 @@ feature {NONE} -- HTML Output
end end
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) -- returns a html string with the pagination links (if necessary)
local local
tmp: NATURAL_16 tmp: NATURAL_32
do do
Result := "" Result := ""
if more_than_one_page then if more_than_one_page then

View File

@@ -0,0 +1,16 @@
note
description: "Request handler related to /blogs/user/{id}/. Displays all posts of the given user"
author: "Dario B<>sch <daboesch@student.ethz.ch>"
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

View File

@@ -21,7 +21,7 @@ feature -- Access
deferred deferred
end 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 -- List of nodes ordered by creation date from limit to limit + offset
deferred deferred
end end

View File

@@ -28,7 +28,7 @@ feature -- Access
create {ARRAYED_LIST [CMS_NODE]} Result.make (0) create {ARRAYED_LIST [CMS_NODE]} Result.make (0)
end 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 -- List of nodes ordered by creation date from limit to limit + offset
do do
create {ARRAYED_LIST [CMS_NODE]} Result.make (0) create {ARRAYED_LIST [CMS_NODE]} Result.make (0)

View File

@@ -49,7 +49,7 @@ feature -- Access
end end
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 -- List of nodes ordered by creation date from limit to limit + offset
local local
l_parameters: STRING_TABLE [detachable ANY] l_parameters: STRING_TABLE [detachable ANY]