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

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

View File

@ -55,6 +55,7 @@ func BlogIndex(c *RequestContext) ResponseData {
ThreadTypes: []models.ThreadType{models.ThreadTypeProjectBlogPost},
Limit: postsPerPage,
Offset: (page - 1) * postsPerPage,
OrderByCreated: true,
})
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch blog posts for index"))