Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/controllers/search_handler.go

64 lignes
1,6 Kio
Go

package controllers
import (
"html"
"net/http"
"strconv"
"math"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/search"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// SearchHandler : Controller for displaying search result page, accepting common search arguments
func SearchHandler(c *gin.Context) {
var err error
// TODO Don't create a new client for each request
// TODO Fallback to postgres search if es is down
page := c.Param("page")
// db params url
pagenum := 1
if page != "" {
pagenum, err = strconv.Atoi(html.EscapeString(page))
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
if pagenum <= 0 {
c.AbortWithError(http.StatusNotFound, errors.New("Can't find a page with negative value"))
return
}
}
searchParam, torrents, nbTorrents, err := search.ByQuery(c, pagenum)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
maxPages := math.Ceil(float64(nbTorrents) / float64(searchParam.Max))
if pagenum > int(maxPages) {
c.AbortWithError(http.StatusNotFound, errors.New("Page superior to the maximum number of pages"))
return
}
// Convert back to strings for now.
category := ""
if len(searchParam.Category) > 0 {
category = searchParam.Category[0].String()
}
nav := navigation{int(nbTorrents), int(searchParam.Max), int(searchParam.Offset), "search"}
searchForm := newSearchForm(c)
searchForm.TorrentParam, searchForm.Category = searchParam, category
if c.Query("refine") == "1" {
searchForm.ShowRefine = true
}
modelList(c, "site/torrents/listing.jet.html", models.TorrentsToJSON(torrents), nav, searchForm)
}