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
This commit is contained in:
@@ -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 (" <a class=%"blog_older_posts%" href=%"/blogs/page/" + tmp.out + "%"><< Older Posts</a> ")
|
||||
Result.append (" <a class=%"blog_older_posts%" href=%"" + base_path + "/page/" + tmp.out + "%"><< Older Posts</a> ")
|
||||
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 (" <a class=%"blog_newer_posts%" href=%"/blogs/page/" + tmp.out + "%">Newer Posts >></a> ")
|
||||
Result.append (" <a class=%"blog_newer_posts%" href=%"" + base_path + "/page/" + tmp.out + "%">Newer Posts >></a> ")
|
||||
end
|
||||
|
||||
Result.append ("</div>")
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
-- <Precursor>
|
||||
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 ("<h1>Error</h1>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 ("<h2>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 ("</h2>")
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user