2017-05-06 21:21:39 +02:00
package router
2017-05-07 01:20:13 +02:00
import (
2017-05-06 21:21:39 +02:00
"net/http"
2017-05-07 00:10:40 +02:00
2017-05-07 14:53:01 +02:00
"github.com/ewhal/nyaa/service/captcha"
2017-05-07 05:04:55 +02:00
"github.com/ewhal/nyaa/service/user"
2017-05-07 00:10:40 +02:00
"github.com/ewhal/nyaa/service/user/form"
2017-05-07 02:32:32 +02:00
"github.com/ewhal/nyaa/util/languages"
2017-05-07 00:10:40 +02:00
"github.com/ewhal/nyaa/util/modelHelper"
"github.com/gorilla/mux"
2017-05-06 21:21:39 +02:00
)
2017-05-07 00:10:40 +02:00
//var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html"))
//var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html"))
2017-05-06 21:21:39 +02:00
// Getting View User Registration
func UserRegisterFormHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-07 22:00:45 +02:00
_ , errorUser := userService . CurrentUser ( r )
if ( errorUser != nil ) {
b := form . RegistrationForm { }
modelHelper . BindValueForm ( & b , r )
b . CaptchaID = captcha . GetID ( )
2017-05-07 23:05:41 +02:00
languages . SetTranslationFromRequest ( viewRegisterTemplate , r , "en-us" )
2017-05-08 01:46:30 +02:00
htv := UserRegisterTemplateVariables { b , form . NewErrors ( ) , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-07 22:00:45 +02:00
err := viewRegisterTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
} else {
HomeHandler ( w , r )
2017-05-07 00:10:40 +02:00
}
2017-05-06 21:21:39 +02:00
}
// Getting View User Login
func UserLoginFormHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-07 04:02:57 +02:00
b := form . LoginForm { }
modelHelper . BindValueForm ( & b , r )
2017-05-08 00:22:57 +02:00
2017-05-07 23:05:41 +02:00
languages . SetTranslationFromRequest ( viewLoginTemplate , r , "en-us" )
2017-05-08 01:46:30 +02:00
htv := UserLoginFormVariables { b , form . NewErrors ( ) , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-08 00:22:57 +02:00
2017-05-07 04:02:57 +02:00
err := viewLoginTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2017-05-06 21:21:39 +02:00
}
// Getting User Profile
func UserProfileHandler ( w http . ResponseWriter , r * http . Request ) {
}
2017-05-07 01:20:13 +02:00
// Getting View User Profile Update
2017-05-06 21:21:39 +02:00
func UserProfileFormHandler ( w http . ResponseWriter , r * http . Request ) {
}
2017-05-07 14:53:01 +02:00
// Post Registration controller, we do some check on the form here, the rest on user service
2017-05-06 21:21:39 +02:00
func UserRegisterPostHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-07 05:04:55 +02:00
// Check same Password
2017-05-07 19:59:38 +02:00
b := form . RegistrationForm { }
err := form . NewErrors ( )
2017-05-07 14:53:01 +02:00
if ! captcha . Authenticate ( captcha . Extract ( r ) ) {
2017-05-07 19:59:38 +02:00
err [ "errors" ] = append ( err [ "errors" ] , "Wrong captcha!" )
}
if ( len ( err ) == 0 ) {
_ , err = form . EmailValidation ( r . PostFormValue ( "email" ) , err )
_ , err = form . ValidateUsername ( r . PostFormValue ( "username" ) , err )
if ( len ( err ) == 0 ) {
modelHelper . BindValueForm ( & b , r )
err = modelHelper . ValidateForm ( & b , err )
if ( len ( err ) == 0 ) {
_ , errorUser := userService . CreateUser ( w , r )
2017-05-07 20:47:29 +02:00
if ( errorUser != nil ) {
err [ "errors" ] = append ( err [ "errors" ] , errorUser . Error ( ) )
}
2017-05-07 19:59:38 +02:00
if ( len ( err ) == 0 ) {
b := form . RegistrationForm { }
2017-05-07 23:05:41 +02:00
languages . SetTranslationFromRequest ( viewRegisterSuccessTemplate , r , "en-us" )
2017-05-08 01:46:30 +02:00
htv := UserRegisterTemplateVariables { b , err , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-07 19:59:38 +02:00
errorTmpl := viewRegisterSuccessTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
}
}
}
2017-05-07 14:53:01 +02:00
}
2017-05-07 19:59:38 +02:00
if ( len ( err ) > 0 ) {
b . CaptchaID = captcha . GetID ( )
2017-05-07 23:05:41 +02:00
languages . SetTranslationFromRequest ( viewRegisterTemplate , r , "en-us" )
2017-05-08 01:46:30 +02:00
htv := UserRegisterTemplateVariables { b , err , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-07 19:59:38 +02:00
errorTmpl := viewRegisterTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
2017-05-07 05:04:55 +02:00
}
}
2017-05-06 21:21:39 +02:00
}
2017-05-07 22:00:45 +02:00
func UserVerifyEmailHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
token := vars [ "token" ]
err := form . NewErrors ( )
_ , errEmail := userService . EmailVerification ( token , w )
if ( errEmail != nil ) {
err [ "errors" ] = append ( err [ "errors" ] , errEmail . Error ( ) )
}
2017-05-07 23:05:41 +02:00
languages . SetTranslationFromRequest ( viewVerifySuccessTemplate , r , "en-us" )
2017-05-08 01:46:30 +02:00
htv := UserVerifyTemplateVariables { err , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-07 22:00:45 +02:00
errorTmpl := viewVerifySuccessTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
}
2017-05-06 21:21:39 +02:00
// Post Login controller
func UserLoginPostHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-08 00:21:31 +02:00
b := form . LoginForm { }
modelHelper . BindValueForm ( & b , r )
err := form . NewErrors ( )
err = modelHelper . ValidateForm ( & b , err )
if ( len ( err ) == 0 ) {
_ , errorUser := userService . CreateUserAuthentication ( w , r )
if ( errorUser != nil ) {
err [ "errors" ] = append ( err [ "errors" ] , errorUser . Error ( ) )
2017-05-08 00:32:41 +02:00
languages . SetTranslationFromRequest ( viewLoginTemplate , r , "en-us" )
2017-05-08 01:46:30 +02:00
htv := UserLoginFormVariables { b , err , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-08 00:21:31 +02:00
errorTmpl := viewLoginTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
} else {
url , _ := Router . Get ( "home" ) . URL ( )
http . Redirect ( w , r , url . String ( ) , http . StatusSeeOther )
}
}
2017-05-06 21:21:39 +02:00
}
// Post Profule Update controller
func UserProfilePostHandler ( w http . ResponseWriter , r * http . Request ) {
}