Sort Gallery photos by Likes and Comments
This commit is contained in:
parent
9575041d1e
commit
944b2e28e9
|
@ -18,6 +18,10 @@ func SiteGallery() http.HandlerFunc {
|
|||
var sortWhitelist = []string{
|
||||
"created_at desc",
|
||||
"created_at asc",
|
||||
|
||||
// Custom (advanced) sort options.
|
||||
"by_likes",
|
||||
"by_comments",
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -19,6 +19,10 @@ func UserPhotos() http.HandlerFunc {
|
|||
"pinned desc nulls last, updated_at desc",
|
||||
"created_at desc",
|
||||
"created_at asc",
|
||||
|
||||
// Custom (advanced) sort options.
|
||||
"by_likes",
|
||||
"by_comments",
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -135,6 +135,24 @@ func PaginateUserPhotos(userID uint64, conf UserGallery, pager *Pagination) ([]*
|
|||
placeholders = append(placeholders, explicit[0])
|
||||
}
|
||||
|
||||
// Custom SORT parameters.
|
||||
switch pager.Sort {
|
||||
case "by_likes":
|
||||
pager.Sort = `(
|
||||
SELECT count(likes.id)
|
||||
FROM likes
|
||||
WHERE likes.table_name = 'photos'
|
||||
AND likes.table_id = photos.id
|
||||
) DESC`
|
||||
case "by_comments":
|
||||
pager.Sort = `(
|
||||
SELECT count(comments.id)
|
||||
FROM comments
|
||||
WHERE comments.table_name = 'photos'
|
||||
AND comments.table_id = photos.id
|
||||
) DESC NULLS LAST`
|
||||
}
|
||||
|
||||
query := DB.Where(
|
||||
strings.Join(wheres, " AND "),
|
||||
placeholders...,
|
||||
|
@ -672,6 +690,24 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
|
|||
)
|
||||
}
|
||||
|
||||
// Custom SORT parameters.
|
||||
switch pager.Sort {
|
||||
case "by_likes":
|
||||
pager.Sort = `(
|
||||
SELECT count(likes.id)
|
||||
FROM likes
|
||||
WHERE likes.table_name = 'photos'
|
||||
AND likes.table_id = photos.id
|
||||
) DESC`
|
||||
case "by_comments":
|
||||
pager.Sort = `(
|
||||
SELECT count(comments.id)
|
||||
FROM comments
|
||||
WHERE comments.table_name = 'photos'
|
||||
AND comments.table_id = photos.id
|
||||
) DESC NULLS LAST`
|
||||
}
|
||||
|
||||
query = query.Order(pager.Sort)
|
||||
|
||||
query.Model(&Photo{}).Count(&pager.Total)
|
||||
|
|
|
@ -414,6 +414,8 @@
|
|||
{{end}}
|
||||
<option value="created_at desc"{{if eq .Sort "created_at desc"}} selected{{end}}>Most recent</option>
|
||||
<option value="created_at asc"{{if eq .Sort "created_at asc"}} selected{{end}}>Oldest first</option>
|
||||
<option value="by_likes"{{if eq .Sort "by_likes"}} selected{{end}}>Most likes</option>
|
||||
<option value="by_comments"{{if eq .Sort "by_comments"}} selected{{end}}>Most comments</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue
Block a user