Sort blog index correctly

This commit is contained in:
Ben Visness 2021-12-11 16:18:58 -06:00
parent 415ce8db43
commit 6d609f1fae
2 changed files with 12 additions and 6 deletions

View File

@ -29,7 +29,8 @@ type ThreadsQuery struct {
ThreadIDs []int ThreadIDs []int
// Ignored when using FetchThread or CountThreads. // Ignored when using FetchThread or CountThreads.
Limit, Offset int // if empty, no pagination Limit, Offset int // if empty, no pagination
OrderByCreated bool // defaults to order by last updated
} }
type ThreadAndStuff struct { type ThreadAndStuff struct {
@ -127,7 +128,11 @@ func FetchThreads(
currentUserID, currentUserID,
) )
} }
qb.Add(`ORDER BY last_post.postdate DESC`) if q.OrderByCreated {
qb.Add(`ORDER BY first_post.postdate DESC`)
} else {
qb.Add(`ORDER BY last_post.postdate DESC`)
}
if q.Limit > 0 { if q.Limit > 0 {
qb.Add(`LIMIT $? OFFSET $?`, q.Limit, q.Offset) qb.Add(`LIMIT $? OFFSET $?`, q.Limit, q.Offset)
} }

View File

@ -51,10 +51,11 @@ func BlogIndex(c *RequestContext) ResponseData {
} }
threads, err := hmndata.FetchThreads(c.Context(), c.Conn, c.CurrentUser, hmndata.ThreadsQuery{ threads, err := hmndata.FetchThreads(c.Context(), c.Conn, c.CurrentUser, hmndata.ThreadsQuery{
ProjectIDs: []int{c.CurrentProject.ID}, ProjectIDs: []int{c.CurrentProject.ID},
ThreadTypes: []models.ThreadType{models.ThreadTypeProjectBlogPost}, ThreadTypes: []models.ThreadType{models.ThreadTypeProjectBlogPost},
Limit: postsPerPage, Limit: postsPerPage,
Offset: (page - 1) * postsPerPage, Offset: (page - 1) * postsPerPage,
OrderByCreated: true,
}) })
if err != nil { if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch blog posts for index")) return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch blog posts for index"))