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 00:10:40 +02:00
b := form . RegistrationForm { }
2017-05-07 02:32:32 +02:00
modelHelper . BindValueForm ( & b , r )
2017-05-07 16:08:45 +02:00
b . CaptchaID = captcha . GetID ( )
2017-05-07 02:32:32 +02:00
languages . SetTranslation ( "en-us" , viewRegisterTemplate )
2017-05-07 01:20:13 +02:00
htv := UserRegisterTemplateVariables { b , NewSearchForm ( ) , Navigation { } , r . URL , mux . CurrentRoute ( r ) }
2017-05-07 03:00:05 +02:00
err := viewRegisterTemplate . ExecuteTemplate ( w , "index.html" , htv )
2017-05-07 00:10:40 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
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 )
languages . SetTranslation ( "en-us" , viewLoginTemplate )
htv := UserLoginFormVariables { b , NewSearchForm ( ) , Navigation { } , r . URL , mux . CurrentRoute ( r ) }
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 14:53:01 +02:00
if ! captcha . Authenticate ( captcha . Extract ( r ) ) {
2017-05-07 16:08:45 +02:00
// TODO: Prettier passing of mistyoed captcha errors
http . Error ( w , captcha . ErrInvalidCaptcha . Error ( ) , 403 )
return
2017-05-07 14:53:01 +02:00
}
2017-05-07 11:08:44 +02:00
if ( r . PostFormValue ( "password" ) == r . PostFormValue ( "password_confirm" ) ) && ( r . PostFormValue ( "password" ) != "" ) {
2017-05-07 16:08:45 +02:00
if ( form . EmailValidation ( r . PostFormValue ( "email" ) ) ) &&
( form . ValidateUsername ( r . PostFormValue ( "username" ) ) ) &&
( form . IsAgreed ( r . PostFormValue ( "t_and_c" ) ) ) {
2017-05-07 11:08:44 +02:00
_ , err := userService . CreateUser ( w , r )
if err == nil {
2017-05-07 05:04:55 +02:00
b := form . RegistrationForm { }
htv := UserRegisterTemplateVariables { b , NewSearchForm ( ) , Navigation { } , r . URL , mux . CurrentRoute ( r ) }
err = viewRegisterSuccessTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
2017-05-07 11:08:44 +02:00
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
2017-05-07 05:04:55 +02:00
}
} else {
UserRegisterFormHandler ( w , r )
}
} else {
UserRegisterFormHandler ( w , r )
}
} else {
UserRegisterFormHandler ( w , r )
}
2017-05-06 21:21:39 +02:00
}
// Post Login controller
func UserLoginPostHandler ( w http . ResponseWriter , r * http . Request ) {
}
// Post Profule Update controller
func UserProfilePostHandler ( w http . ResponseWriter , r * http . Request ) {
}