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/router/searchHandler.go
akuma06 7209666fb8 Merge branch 'master' of https://github.com/ewhal/nyaa
Conflicts:
	main.go
Resolved conflicts with new merge
2017-05-05 22:30:56 +02:00

50 lignes
Pas d'EOL
1,3 Kio
Go

package router
import(
"github.com/ewhal/nyaa/util/search"
"net/http"
"html/template"
"github.com/gorilla/mux"
"html"
"strconv"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
)
func SearchHandler(w http.ResponseWriter, r *http.Request) {
var templates = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html"))
templates.ParseGlob("templates/_*.html") // common
vars := mux.Vars(r)
page := vars["page"]
// db params url
pagenum, _ := strconv.Atoi(html.EscapeString(page))
if pagenum == 0 {
pagenum = 1
}
b := []model.TorrentsJson{}
search_param, torrents, nbTorrents := search.SearchByQuery( r, pagenum )
for i, _ := range torrents {
res := torrents[i].ToJson()
b = append(b, res)
}
navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"}
searchForm := SearchForm{
search_param.Query,
search_param.Status,
search_param.Category,
search_param.Sort,
search_param.Order,
false,
}
htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)}
err := templates.ExecuteTemplate(w, "index.html", htv)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}