2017-05-05 16:39:15 +02:00
package router
2017-05-06 19:01:15 +02:00
import (
2017-05-10 12:06:32 +02:00
"html"
"net/http"
"strconv"
"github.com/ewhal/nyaa/cache"
"github.com/ewhal/nyaa/common"
2017-05-05 16:39:15 +02:00
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
2017-05-09 13:31:58 +02:00
"github.com/ewhal/nyaa/util"
2017-05-08 21:16:26 +02:00
"github.com/ewhal/nyaa/util/languages"
2017-05-07 13:51:33 +02:00
"github.com/ewhal/nyaa/util/log"
2017-05-06 19:01:15 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
func HomeHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-06 19:01:15 +02:00
vars := mux . Vars ( r )
2017-05-05 16:39:15 +02:00
page := vars [ "page" ]
// db params url
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
var err error
maxPerPage := 50
maxString := r . URL . Query ( ) . Get ( "max" )
if maxString != "" {
maxPerPage , err = strconv . Atoi ( maxString )
if ! log . CheckError ( err ) {
maxPerPage = 50 // default Value maxPerPage
}
2017-05-05 16:39:15 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
pagenum := 1
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-05 16:39:15 +02:00
}
2017-05-10 12:06:32 +02:00
search := common . SearchParam {
Max : uint ( maxPerPage ) ,
Page : pagenum ,
2017-05-09 13:31:58 +02:00
}
2017-05-05 16:39:15 +02:00
2017-05-11 15:01:53 +02:00
torrents , nbTorrents , err := cache . Impl . Get ( search , func ( ) ( [ ] model . Torrent , int , error ) {
2017-05-10 12:06:32 +02:00
torrents , nbTorrents , err := torrentService . GetAllTorrents ( maxPerPage , maxPerPage * ( pagenum - 1 ) )
if ! log . CheckError ( err ) {
util . SendError ( w , err , 400 )
}
return torrents , nbTorrents , err
} )
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-07 01:20:13 +02:00
navigationTorrents := Navigation { nbTorrents , maxPerPage , pagenum , "search_page" }
2017-05-08 21:16:26 +02:00
languages . SetTranslationFromRequest ( homeTemplate , r , "en-us" )
2017-05-08 01:46:30 +02:00
htv := HomeTemplateVariables { b , NewSearchForm ( ) , navigationTorrents , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-05 16:39:15 +02:00
2017-05-09 13:31:58 +02:00
err = homeTemplate . ExecuteTemplate ( w , "index.html" , htv )
2017-05-05 16:39:15 +02:00
if err != nil {
2017-05-07 13:51:33 +02:00
log . Errorf ( "HomeHandler(): %s" , err )
2017-05-05 16:39:15 +02:00
}
}