blog handler optimized, blog user handler created
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 <daboesch@student.ethz.ch>"
|
||||
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
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
note
|
||||
description: "Request handler related to /blogs."
|
||||
author: "Dario B<>sch <daboesch@student.ethz.ch>"
|
||||
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
|
||||
-- <Precursor>
|
||||
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 ("<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.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<br/>", 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 ("<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
|
||||
-- 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
|
||||
|
||||
16
examples/demo/modules/blog/handler/blog_user_handler.e
Normal file
16
examples/demo/modules/blog/handler/blog_user_handler.e
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user