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"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/cache"
"github.com/NyaaPantsu/nyaa/common"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service/torrent"
"github.com/NyaaPantsu/nyaa/util"
"github.com/NyaaPantsu/nyaa/util/log"
2017-05-23 04:05:33 +02:00
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-05-06 19:01:15 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
2017-05-25 21:54:58 +02:00
// HomeHandler : Controller for Home page, can have some query arguments
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" ]
2017-05-23 04:05:33 +02:00
messages := msg . GetMessages ( r )
deleteVar := r . URL . Query ( ) [ "deleted" ]
2017-05-05 16:39:15 +02:00
// 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
}
2017-05-24 09:11:13 +02:00
if deleteVar != nil {
2017-05-23 04:05:33 +02:00
messages . AddInfoTf ( "infos" , "torrent_deleted" , "" )
}
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-20 01:10:16 +02:00
if pagenum <= 0 {
NotFoundHandler ( w , r )
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-25 21:54:58 +02:00
navigationTorrents := navigation {
2017-05-19 04:55:59 +02:00
TotalItem : nbTorrents ,
MaxItemPerPage : maxPerPage ,
CurrentPage : pagenum ,
Route : "search_page" ,
}
2017-05-08 21:16:26 +02:00
2017-05-25 21:54:58 +02:00
torrentsJSON := model . TorrentsToJSON ( torrents )
common := newCommonVariables ( r )
2017-05-21 09:10:19 +02:00
common . Navigation = navigationTorrents
2017-05-25 21:54:58 +02:00
htv := modelListVbs {
commonTemplateVariables : common ,
Models : torrentsJSON ,
2017-05-24 09:11:13 +02:00
Infos : messages . GetAllInfos ( ) ,
2017-05-19 04:55:59 +02:00
}
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
}
}