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:
2017-01-27 19:12:37 +01:00
parent 456770ca3e
commit 38b4713276
5 changed files with 69 additions and 60 deletions

View File

@@ -1,6 +1,6 @@
note
description: "Request handler related to /blogs and /blogs/{page}. Displays all posts in the blog."
author: "Dario B<>sch <daboesch@student.ethz.ch>"
description: "Request handler related to /blogs and /blogs/?page={page}. Displays all posts in the blog."
contributor: "Dario B<>sch <daboesch@student.ethz.ch>"
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 ("<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
@@ -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 (" <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
-- 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 (" <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
a_output.append ("</div>")

View File

@@ -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 <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) $"
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 ("<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
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

View File

@@ -1,6 +1,6 @@
note
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) $"
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