2017-05-05 16:39:15 +02:00
package router
2017-05-06 10:36:37 +02:00
import (
"net/http"
2017-05-08 05:34:12 +02:00
"strconv"
2017-05-06 19:01:15 +02:00
2017-05-08 05:34:12 +02:00
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/captcha"
2017-05-06 19:01:15 +02:00
"github.com/ewhal/nyaa/service/torrent"
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 ViewHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
torrent , err := torrentService . GetTorrentById ( id )
2017-05-07 13:51:33 +02:00
if err != nil {
NotFoundHandler ( w , r )
return
}
2017-05-05 16:39:15 +02:00
b := torrent . ToJson ( )
2017-05-08 05:34:12 +02:00
htv := ViewTemplateVariables { b , captcha . Captcha { CaptchaID : captcha . GetID ( ) } , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-05 16:39:15 +02:00
2017-05-06 19:01:15 +02:00
err = viewTemplate . 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 ( "ViewHandler(): %s" , err )
2017-05-05 16:39:15 +02:00
}
2017-05-06 10:36:37 +02:00
}
2017-05-08 05:34:12 +02:00
func PostCommentHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
userCaptcha := captcha . Extract ( r )
if ! captcha . Authenticate ( userCaptcha ) {
http . Error ( w , "bad captcha" , 403 )
}
content := r . FormValue ( "comment" )
idNum , err := strconv . Atoi ( id )
comment := model . Comment { Username : "れんちょん" , Content : content , TorrentId : idNum }
db . ORM . Create ( & comment )
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , id )
if err == nil {
http . Redirect ( w , r , url . String ( ) , 302 )
} else {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
}