2017-05-05 16:39:15 +02:00
package router
2017-05-06 19:01:15 +02:00
import (
2017-05-07 13:51:33 +02:00
"html"
"net/http"
"strconv"
2017-05-09 01:56:57 +02:00
"github.com/ewhal/nyaa/model"
2017-05-09 13:31:58 +02:00
"github.com/ewhal/nyaa/util"
2017-05-09 01:56:57 +02:00
"github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/search"
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
func SearchHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
page := vars [ "page" ]
// db params url
pagenum , _ := strconv . Atoi ( html . EscapeString ( page ) )
if pagenum == 0 {
pagenum = 1
}
2017-05-06 19:01:15 +02:00
2017-05-09 13:31:58 +02:00
search_param , torrents , nbTorrents , err := search . SearchByQuery ( r , pagenum )
if err != nil {
util . SendError ( w , err , 400 )
return
}
2017-05-05 16:39:15 +02:00
2017-05-09 01:56:57 +02:00
b := model . TorrentsToJSON ( torrents )
2017-05-05 16:39:15 +02:00
2017-05-09 13:31:58 +02:00
navigationTorrents := Navigation { nbTorrents , int ( search_param . Max ) , pagenum , "search_page" }
// Convert back to strings for now.
2017-05-07 01:20:13 +02:00
searchForm := SearchForm {
2017-05-09 13:31:58 +02:00
SearchParam : search_param ,
Category : search_param . Category . String ( ) ,
HideAdvancedSearch : false ,
2017-05-05 16:39:15 +02:00
}
2017-05-08 01:46:30 +02:00
htv := HomeTemplateVariables { b , searchForm , navigationTorrents , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-05 16:39:15 +02:00
2017-05-08 22:43:33 +02:00
languages . SetTranslationFromRequest ( searchTemplate , r , "en-us" )
2017-05-09 13:31:58 +02:00
err = searchTemplate . ExecuteTemplate ( w , "index.html" , htv )
2017-05-05 16:39:15 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2017-05-06 19:01:15 +02:00
}