Updated blog design to use ?page=123&size=456.
Use /blog/{user} url to list blog from a specific user.
Fixed bad xhtml rendering (missing closing tag).
This commit is contained in:
@@ -41,11 +41,7 @@ feature {NONE} -- Initialization
|
|||||||
create {CMS_BLOG_STORAGE_NULL} blog_storage.make
|
create {CMS_BLOG_STORAGE_NULL} blog_storage.make
|
||||||
end
|
end
|
||||||
|
|
||||||
--| For test reasons this is 2, so we don't have to create a lot of blog entries.
|
default_entries_count_per_page := 8 -- Default FIXME: find a way to make it customizable.
|
||||||
--| TODO: Set to bigger constant.
|
|
||||||
entries_per_page := 6
|
|
||||||
|
|
||||||
-- initialize_node_types
|
|
||||||
end
|
end
|
||||||
|
|
||||||
feature {CMS_API_ACCESS, CMS_MODULE, CMS_API} -- Restricted access
|
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
|
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
|
-- The numbers of posts that are shown on one page. If there are more post a pagination is generated
|
||||||
|
|
||||||
feature -- Access node
|
feature -- Access node
|
||||||
|
|||||||
@@ -115,7 +115,6 @@ feature -- Access: router
|
|||||||
l_blog_user_handler: BLOG_USER_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]
|
|
||||||
create l_blog_handler.make (a_api, a_blog_api)
|
create l_blog_handler.make (a_api, a_blog_api)
|
||||||
create l_blog_user_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)
|
a_router.handle ("/blogs/page/{page}", l_blog_handler, a_router.methods_get)
|
||||||
|
|
||||||
-- If a user id is given route with blog user handler
|
-- If a user id is given route with blog user handler
|
||||||
--| FIXME: maybe /user/{user}/blogs/ would be better.
|
a_router.handle ("/blog/{user}", l_blog_user_handler, a_router.methods_get)
|
||||||
a_router.handle ("/blogs/user/{user}", l_blog_user_handler, a_router.methods_get)
|
|
||||||
|
|
||||||
-- If a user id is given we also want to allow different pages
|
-- If a user id is given we also want to allow different pages
|
||||||
--| FIXME: what about /user/{user}/blogs/?page={page} ?
|
a_router.handle ("/blog/{user}", l_blog_user_handler, a_router.methods_get)
|
||||||
a_router.handle ("/blogs/user/{user}/page/{page}", l_blog_user_handler, a_router.methods_get)
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
note
|
note
|
||||||
description: "Request handler related to /blogs and /blogs/{page}. Displays all posts in the blog."
|
description: "Request handler related to /blogs and /blogs/?page={page}. Displays all posts in the blog."
|
||||||
author: "Dario B<>sch <daboesch@student.ethz.ch>"
|
contributor: "Dario B<>sch <daboesch@student.ethz.ch>"
|
||||||
date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $"
|
date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $"
|
||||||
revision: "$9661667$"
|
revision: "$9661667$"
|
||||||
|
|
||||||
@@ -69,7 +69,8 @@ feature -- HTTP Methods
|
|||||||
l_page: CMS_RESPONSE
|
l_page: CMS_RESPONSE
|
||||||
do
|
do
|
||||||
-- Read page number from path parameter.
|
-- 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)'.
|
-- Responding with `main_content_html (l_page)'.
|
||||||
create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api)
|
create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api)
|
||||||
@@ -94,6 +95,19 @@ feature -- Query
|
|||||||
Result := entries_per_page < total_entries
|
Result := entries_per_page < total_entries
|
||||||
end
|
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
|
pages_count: NATURAL_32
|
||||||
-- Number of pages needed to display all posts.
|
-- Number of pages needed to display all posts.
|
||||||
require
|
require
|
||||||
@@ -105,14 +119,14 @@ feature -- Query
|
|||||||
Result := tmp.ceiling.to_natural_32
|
Result := tmp.ceiling.to_natural_32
|
||||||
end
|
end
|
||||||
|
|
||||||
page_number_path_parameter (req: WSF_REQUEST): NATURAL_32
|
page_number_parameter (req: WSF_REQUEST): NATURAL_32
|
||||||
-- Page number from path /blogs/{page}.
|
-- Page number from path /blogs/?page={page}.
|
||||||
-- Unsigned integer since negative pages are not allowed.
|
-- Unsigned integer since negative pages are not allowed.
|
||||||
local
|
local
|
||||||
s: STRING
|
s: READABLE_STRING_32
|
||||||
do
|
do
|
||||||
Result := 1 -- default if not get variable is set
|
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
|
s := p_page.value
|
||||||
if s.is_natural_32 then
|
if s.is_natural_32 then
|
||||||
if s.to_natural_32 > 1 then
|
if s.to_natural_32 > 1 then
|
||||||
@@ -206,7 +220,7 @@ feature -- HTML Output
|
|||||||
do
|
do
|
||||||
if attached n.author as l_author then
|
if attached n.author as l_author then
|
||||||
a_output.append ("by ")
|
a_output.append ("by ")
|
||||||
a_output.append ("<a class=%"blog_user_link%" href=%"/blogs/user/" + l_author.id.out + "%">" + html_encoded (l_author.name) + "</a>")
|
a_output.append ("<a class=%"blog_user_link%" href=%"/blog/" + l_author.id.out + "%">" + html_encoded (l_author.name) + "</a>")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -251,7 +265,7 @@ feature -- HTML Output
|
|||||||
-- If exist older posts show link to next page
|
-- If exist older posts show link to next page
|
||||||
if page_number < pages_count then
|
if page_number < pages_count then
|
||||||
tmp := page_number + 1
|
tmp := page_number + 1
|
||||||
a_output.append (" <a class=%"blog_older_posts%" href=%"" + base_path + "/page/" + tmp.out + "%"><< Older Posts</a> ")
|
a_output.append (" <a class=%"blog_older_posts%" href=%"" + base_path + "?page=" + tmp.out + "&size=" + entries_per_page.out + "%"><< Older Posts</a> ")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Delimiter
|
-- Delimiter
|
||||||
@@ -262,7 +276,7 @@ feature -- HTML Output
|
|||||||
-- If exist newer posts show link to previous page
|
-- If exist newer posts show link to previous page
|
||||||
if page_number > 1 then
|
if page_number > 1 then
|
||||||
tmp := page_number -1
|
tmp := page_number -1
|
||||||
a_output.append (" <a class=%"blog_newer_posts%" href=%"" + base_path + "/page/" + tmp.out + "%">Newer Posts >></a> ")
|
a_output.append (" <a class=%"blog_newer_posts%" href=%"" + base_path + "?page=" + tmp.out + "&size=" + entries_per_page.out + "%">Newer Posts >></a> ")
|
||||||
end
|
end
|
||||||
|
|
||||||
a_output.append ("</div>")
|
a_output.append ("</div>")
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
note
|
note
|
||||||
description: "[
|
description: "[
|
||||||
Request handler related to
|
Request handler related to
|
||||||
/blogs/user/{id}/
|
/blog/{user}/
|
||||||
or /blogs/user/{id}/page/{page}.
|
or /blog/{user}?page={page}.
|
||||||
|
|
||||||
Displays all posts of the given user
|
Displays all posts of the given user
|
||||||
|
|
||||||
]"
|
]"
|
||||||
author: "Dario B<>sch <daboesch@student.ethz.ch>"
|
contributor: "Dario B<>sch <daboesch@student.ethz.ch>"
|
||||||
date: "$Date: 2015-05-22 15:13:00 +0100 (lun., 18 mai 2015) $"
|
date: "$Date: 2015-05-22 15:13:00 +0100 (lun., 18 mai 2015) $"
|
||||||
revision: "$Revision 96616$"
|
revision: "$Revision 96616$"
|
||||||
|
fixme: "redesign pagination... see CMS_PAGINATION_GENERATOR."
|
||||||
|
|
||||||
class
|
class
|
||||||
BLOG_USER_HANDLER
|
BLOG_USER_HANDLER
|
||||||
@@ -37,7 +39,7 @@ feature -- HTTP Methods
|
|||||||
local
|
local
|
||||||
l_error: NOT_FOUND_ERROR_CMS_RESPONSE
|
l_error: NOT_FOUND_ERROR_CMS_RESPONSE
|
||||||
do
|
do
|
||||||
user := Void
|
check user_void: user = Void end
|
||||||
if attached user_from_request (req) as l_user then
|
if attached user_from_request (req) as l_user then
|
||||||
user := l_user
|
user := l_user
|
||||||
-- Output the results, similar as in the blog hanlder (but with other queries)
|
-- Output the results, similar as in the blog hanlder (but with other queries)
|
||||||
@@ -45,53 +47,47 @@ feature -- HTTP Methods
|
|||||||
else
|
else
|
||||||
-- Throw a bad request error because the user is not valid
|
-- Throw a bad request error because the user is not valid
|
||||||
create l_error.make (req, res, api)
|
create l_error.make (req, res, api)
|
||||||
l_error.set_main_content ("<h1>Error</h1>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 ("<h1>Error</h1>User with id " + api.html_encoded (l_user_id) + " not found!</h1>")
|
||||||
|
else
|
||||||
|
l_error.set_main_content ("<h1>Error</h1>User not found!</h1>")
|
||||||
|
end
|
||||||
l_error.execute
|
l_error.execute
|
||||||
end
|
end
|
||||||
|
user := Void
|
||||||
end
|
end
|
||||||
|
|
||||||
feature -- Query
|
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
|
user_from_request (req: WSF_REQUEST): detachable CMS_USER
|
||||||
-- Eventual user with given id in the path of request `req'.
|
-- Eventual user with given id in the path of request `req'.
|
||||||
local
|
local
|
||||||
uid: like user_id_path_parameter
|
uid: INTEGER_64
|
||||||
do
|
do
|
||||||
uid := user_id_path_parameter (req)
|
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
|
if uid > 0 then
|
||||||
Result := api.user_api.user_by_id (uid)
|
Result := api.user_api.user_by_id (uid)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
Result := api.user_api.user_by_name (l_user_id)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
-- Missing or invalid user id.
|
-- Missing or invalid user id.
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
user_id_path_parameter (req: WSF_REQUEST): INTEGER_32
|
user_parameter (req: WSF_REQUEST): detachable STRING_32
|
||||||
-- User id from path /blogs/{user}.
|
-- User id from path /blog/{user}.
|
||||||
-- Unsigned integer since negative ids are not allowed.
|
-- Unsigned integer since negative ids are not allowed.
|
||||||
-- If no valid id can be read it returns -1
|
-- If no valid id can be read it returns -1
|
||||||
do
|
do
|
||||||
Result := -1
|
|
||||||
if attached {WSF_STRING} req.path_parameter ("user") as l_user_id then
|
if attached {WSF_STRING} req.path_parameter ("user") as l_user_id then
|
||||||
if l_user_id.is_integer then
|
Result := l_user_id.value
|
||||||
Result := l_user_id.integer_value
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -139,9 +135,9 @@ feature -- HTML Output
|
|||||||
-- If user is logged in, include user id
|
-- If user is logged in, include user id
|
||||||
do
|
do
|
||||||
if attached user as l_user then
|
if attached user as l_user then
|
||||||
Result := "/blogs/user/" + l_user.id.out
|
Result := "/blog/" + l_user.id.out
|
||||||
else
|
else
|
||||||
Result := precursor
|
Result := Precursor
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
note
|
note
|
||||||
description: "Deferred request handler related to /blogs/... Has an own blog api."
|
description: "Deferred request handler related to /blogs/... Has an own blog api."
|
||||||
author: "Dario B<>sch <daboesch@student.ethz.ch>"
|
contributor: "Dario B<>sch <daboesch@student.ethz.ch>"
|
||||||
date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $"
|
date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $"
|
||||||
revision: "$9661667$"
|
revision: "$9661667$"
|
||||||
|
|
||||||
@@ -11,13 +11,19 @@ inherit
|
|||||||
CMS_MODULE_HANDLER [CMS_BLOG_API]
|
CMS_MODULE_HANDLER [CMS_BLOG_API]
|
||||||
rename
|
rename
|
||||||
module_api as blog_api
|
module_api as blog_api
|
||||||
|
redefine
|
||||||
|
make
|
||||||
end
|
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
|
feature -- Access
|
||||||
|
|
||||||
entries_per_page: NATURAL_32
|
entries_per_page: NATURAL_32
|
||||||
do
|
|
||||||
Result := blog_api.entries_per_page
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user