From 052c502b07b14bb36d152930ed2a0d78acbfb815 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 7 May 2017 00:10:40 +0200 Subject: [PATCH] Added User Registration View Moved public files in public folder --- {css => public/css}/style-night.css | 0 {css => public/css}/style.css | 0 {img => public/img}/favicon.png | Bin {img => public/img}/megumin.png | Bin {img => public/img}/renchon.png | Bin {img => public/img}/torrents/1.png | Bin {img => public/img}/torrents/10.png | Bin {img => public/img}/torrents/11.png | Bin {img => public/img}/torrents/12.png | Bin {img => public/img}/torrents/13.png | Bin {img => public/img}/torrents/14.png | Bin {img => public/img}/torrents/15.png | Bin {img => public/img}/torrents/16.png | Bin {img => public/img}/torrents/18.png | Bin {img => public/img}/torrents/2.png | Bin {img => public/img}/torrents/3.png | Bin {img => public/img}/torrents/4.png | Bin {img => public/img}/torrents/5.png | Bin {img => public/img}/torrents/6.png | Bin {img => public/img}/torrents/7.png | Bin {img => public/img}/torrents/8.png | Bin {img => public/img}/torrents/9.png | Bin {js => public/js}/main.js | 0 router/router.go | 13 ++--- router/templateVariables.go | 9 ++++ router/userHandler.go | 22 ++++++-- service/user/cookieHelper.go | 4 +- service/user/{ => form}/formValidator.go | 2 +- service/user/user.go | 15 +++--- service/user/verification.go | 2 +- templates/user/register.html | 66 +++++++++++++++++++++++ 31 files changed, 111 insertions(+), 22 deletions(-) rename {css => public/css}/style-night.css (100%) rename {css => public/css}/style.css (100%) rename {img => public/img}/favicon.png (100%) rename {img => public/img}/megumin.png (100%) rename {img => public/img}/renchon.png (100%) rename {img => public/img}/torrents/1.png (100%) rename {img => public/img}/torrents/10.png (100%) rename {img => public/img}/torrents/11.png (100%) rename {img => public/img}/torrents/12.png (100%) rename {img => public/img}/torrents/13.png (100%) rename {img => public/img}/torrents/14.png (100%) rename {img => public/img}/torrents/15.png (100%) rename {img => public/img}/torrents/16.png (100%) rename {img => public/img}/torrents/18.png (100%) rename {img => public/img}/torrents/2.png (100%) rename {img => public/img}/torrents/3.png (100%) rename {img => public/img}/torrents/4.png (100%) rename {img => public/img}/torrents/5.png (100%) rename {img => public/img}/torrents/6.png (100%) rename {img => public/img}/torrents/7.png (100%) rename {img => public/img}/torrents/8.png (100%) rename {img => public/img}/torrents/9.png (100%) rename {js => public/js}/main.js (100%) rename service/user/{ => form}/formValidator.go (99%) create mode 100644 templates/user/register.html diff --git a/css/style-night.css b/public/css/style-night.css similarity index 100% rename from css/style-night.css rename to public/css/style-night.css diff --git a/css/style.css b/public/css/style.css similarity index 100% rename from css/style.css rename to public/css/style.css diff --git a/img/favicon.png b/public/img/favicon.png similarity index 100% rename from img/favicon.png rename to public/img/favicon.png diff --git a/img/megumin.png b/public/img/megumin.png similarity index 100% rename from img/megumin.png rename to public/img/megumin.png diff --git a/img/renchon.png b/public/img/renchon.png similarity index 100% rename from img/renchon.png rename to public/img/renchon.png diff --git a/img/torrents/1.png b/public/img/torrents/1.png similarity index 100% rename from img/torrents/1.png rename to public/img/torrents/1.png diff --git a/img/torrents/10.png b/public/img/torrents/10.png similarity index 100% rename from img/torrents/10.png rename to public/img/torrents/10.png diff --git a/img/torrents/11.png b/public/img/torrents/11.png similarity index 100% rename from img/torrents/11.png rename to public/img/torrents/11.png diff --git a/img/torrents/12.png b/public/img/torrents/12.png similarity index 100% rename from img/torrents/12.png rename to public/img/torrents/12.png diff --git a/img/torrents/13.png b/public/img/torrents/13.png similarity index 100% rename from img/torrents/13.png rename to public/img/torrents/13.png diff --git a/img/torrents/14.png b/public/img/torrents/14.png similarity index 100% rename from img/torrents/14.png rename to public/img/torrents/14.png diff --git a/img/torrents/15.png b/public/img/torrents/15.png similarity index 100% rename from img/torrents/15.png rename to public/img/torrents/15.png diff --git a/img/torrents/16.png b/public/img/torrents/16.png similarity index 100% rename from img/torrents/16.png rename to public/img/torrents/16.png diff --git a/img/torrents/18.png b/public/img/torrents/18.png similarity index 100% rename from img/torrents/18.png rename to public/img/torrents/18.png diff --git a/img/torrents/2.png b/public/img/torrents/2.png similarity index 100% rename from img/torrents/2.png rename to public/img/torrents/2.png diff --git a/img/torrents/3.png b/public/img/torrents/3.png similarity index 100% rename from img/torrents/3.png rename to public/img/torrents/3.png diff --git a/img/torrents/4.png b/public/img/torrents/4.png similarity index 100% rename from img/torrents/4.png rename to public/img/torrents/4.png diff --git a/img/torrents/5.png b/public/img/torrents/5.png similarity index 100% rename from img/torrents/5.png rename to public/img/torrents/5.png diff --git a/img/torrents/6.png b/public/img/torrents/6.png similarity index 100% rename from img/torrents/6.png rename to public/img/torrents/6.png diff --git a/img/torrents/7.png b/public/img/torrents/7.png similarity index 100% rename from img/torrents/7.png rename to public/img/torrents/7.png diff --git a/img/torrents/8.png b/public/img/torrents/8.png similarity index 100% rename from img/torrents/8.png rename to public/img/torrents/8.png diff --git a/img/torrents/9.png b/public/img/torrents/9.png similarity index 100% rename from img/torrents/9.png rename to public/img/torrents/9.png diff --git a/js/main.js b/public/js/main.js similarity index 100% rename from js/main.js rename to public/js/main.js diff --git a/router/router.go b/router/router.go index 4b073854..ec37579a 100644 --- a/router/router.go +++ b/router/router.go @@ -11,12 +11,12 @@ var Router *mux.Router func init() { Router = mux.NewRouter() - cssHandler := http.FileServer(http.Dir("./css/")) - jsHandler := http.FileServer(http.Dir("./js/")) - imgHandler := http.FileServer(http.Dir("./img/")) - http.Handle("/css/", http.StripPrefix("/css/", cssHandler)) - http.Handle("/js/", http.StripPrefix("/js/", jsHandler)) - http.Handle("/img/", http.StripPrefix("/img/", imgHandler)) + cssHandler := http.FileServer(http.Dir("./static/css/")) + jsHandler := http.FileServer(http.Dir("./static/js/")) + imgHandler := http.FileServer(http.Dir("./static/img/")) + http.Handle("/css/", http.StripPrefix("/static/css/", cssHandler)) + http.Handle("/js/", http.StripPrefix("/static/js/", jsHandler)) + http.Handle("/img/", http.StripPrefix("/static/img/", imgHandler)) // Routes, Router.HandleFunc("/", HomeHandler).Name("home") @@ -29,4 +29,5 @@ func init() { Router.HandleFunc("/feed", RssHandler).Name("feed") Router.HandleFunc("/view/{id}", ViewHandler).Name("view_torrent") Router.HandleFunc("/upload", UploadHandler).Name("upload") + Router.HandleFunc("/user/register", UserRegisterFormHandler).Name("user_register") } diff --git a/router/templateVariables.go b/router/templateVariables.go index cf33cf45..c44a92cd 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -5,6 +5,7 @@ import ( "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/templates" + userForms "github.com/ewhal/nyaa/service/user/form" "github.com/gorilla/mux" ) @@ -29,6 +30,14 @@ type ViewTemplateVariables struct { Route *mux.Route // For getting current route in templates } +type UserRegisterTemplateVariables struct { + RegistrationForm userForms.RegistrationForm + Search templates.SearchForm + Navigation templates.Navigation + URL *url.URL // For parsing Url in templates + Route *mux.Route // For getting current route in templates +} + type HomeTemplateVariables struct { ListTorrents []model.TorrentsJson ListCategories []model.Categories diff --git a/router/userHandler.go b/router/userHandler.go index fcd54933..4b8ec24b 100644 --- a/router/userHandler.go +++ b/router/userHandler.go @@ -1,18 +1,30 @@ package router import( - // "github.com/gorilla/mux" + "html/template" "net/http" - // "html" - // "strconv" - // "github.com/ewhal/nyaa/model" - // "github.com/ewhal/nyaa/service/user" + + "github.com/ewhal/nyaa/templates" + "github.com/ewhal/nyaa/service/user/form" + "github.com/ewhal/nyaa/util/modelHelper" + "github.com/gorilla/mux" ) +var viewRegisterTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/user/register.html")) +//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")) + // Getting View User Registration func UserRegisterFormHandler(w http.ResponseWriter, r *http.Request) { + b := form.RegistrationForm{} + modelHelper.BindValueForm(b, r) + htv := UserRegisterTemplateVariables{b, templates.NewSearchForm(), templates.Navigation{}, r.URL, mux.CurrentRoute(r)} + err := viewTemplate.ExecuteTemplate(w, "index.html", htv) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } } // Getting View User Login diff --git a/service/user/cookieHelper.go b/service/user/cookieHelper.go index 99371d6f..8c327cbd 100644 --- a/service/user/cookieHelper.go +++ b/service/user/cookieHelper.go @@ -116,7 +116,7 @@ func SetCookieHandler(w http.ResponseWriter, email string, pass string) (int, er } // RegisterHanderFromForm sets cookie from a RegistrationForm. -func RegisterHanderFromForm(w http.ResponseWriter, registrationForm RegistrationForm) (int, error) { +func RegisterHanderFromForm(w http.ResponseWriter, registrationForm formStruct.RegistrationForm) (int, error) { email := registrationForm.Email pass := registrationForm.Password log.Debugf("RegisterHandler UserEmail : %s", email) @@ -126,7 +126,7 @@ func RegisterHanderFromForm(w http.ResponseWriter, registrationForm Registration // RegisterHandler sets a cookie when user registered. func RegisterHandler(w http.ResponseWriter, r *http.Request) (int, error) { - var registrationForm RegistrationForm + var registrationForm formStruct.RegistrationForm modelHelper.BindValueForm(®istrationForm, r) return RegisterHanderFromForm(w, registrationForm) } diff --git a/service/user/formValidator.go b/service/user/form/formValidator.go similarity index 99% rename from service/user/formValidator.go rename to service/user/form/formValidator.go index 1ca06130..2711d88a 100644 --- a/service/user/formValidator.go +++ b/service/user/form/formValidator.go @@ -1,4 +1,4 @@ -package userService +package form import ( "regexp" diff --git a/service/user/user.go b/service/user/user.go index bd60bb9f..91638a8a 100644 --- a/service/user/user.go +++ b/service/user/user.go @@ -10,6 +10,7 @@ import ( "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/model" + formStruct "github.com/ewhal/nyaa/service/user/form" "github.com/ewhal/nyaa/util/crypto" "github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/modelHelper" @@ -43,7 +44,7 @@ func SuggestUsername(username string) string { } // CreateUserFromForm creates a user from a registration form. -func CreateUserFromForm(registrationForm RegistrationForm) (model.User, error) { +func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.User, error) { var user model.User log.Debugf("registrationForm %+v\n", registrationForm) modelHelper.AssignValue(&user, ®istrationForm) @@ -64,7 +65,7 @@ func CreateUserFromForm(registrationForm RegistrationForm) (model.User, error) { // CreateUser creates a user. func CreateUser(w http.ResponseWriter, r *http.Request) (int, error) { var user model.User - var registrationForm RegistrationForm + var registrationForm formStruct.RegistrationForm var status int var err error @@ -161,7 +162,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request, id string) (*model.User, } switch r.FormValue("type") { case "password": - var passwordForm PasswordForm + var passwordForm formStruct.PasswordForm modelHelper.BindValueForm(&passwordForm, r) log.Debugf("form %+v\n", passwordForm) err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(passwordForm.CurrentPassword)) @@ -178,7 +179,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request, id string) (*model.User, } } default: - var form UserForm + var form formStruct.UserForm modelHelper.BindValueForm(&form, r) log.Debugf("form %+v\n", form) modelHelper.AssignValue(&user, &form) @@ -207,7 +208,7 @@ func DeleteUser(w http.ResponseWriter, id string) (int, error) { // AddRoleToUser adds a role to a user. func AddRoleToUser(r *http.Request) (int, error) { - var form UserRoleForm + var form formStruct.UserRoleForm var user model.User var role model.Role var roles []model.Role @@ -313,7 +314,7 @@ func RetrieveUsersForAdmin() []model.User { // ActivateUser toggle activation of a user. func ActivateUser(r *http.Request, id string) (model.User, int, error) { var user model.User - var form ActivateForm + var form formStruct.ActivateForm modelHelper.BindValueForm(&form, r) if db.ORM.First(&user, id).RecordNotFound() { return user, http.StatusNotFound, errors.New("User is not found.") @@ -327,7 +328,7 @@ func ActivateUser(r *http.Request, id string) (model.User, int, error) { // CreateUserAuthentication creates user authentication. func CreateUserAuthentication(w http.ResponseWriter, r *http.Request) (int, error) { - var form LoginForm + var form formStruct.LoginForm modelHelper.BindValueForm(&form, r) email := form.Email pass := form.Password diff --git a/service/user/verification.go b/service/user/verification.go index c772542d..ec8786f4 100644 --- a/service/user/verification.go +++ b/service/user/verification.go @@ -67,7 +67,7 @@ func SendVerification(r *http.Request) (int, error) { // EmailVerification verifies an email of user. func EmailVerification(w http.ResponseWriter, r *http.Request) (int, error) { var user model.User - var verifyEmailForm VerifyEmailForm + var verifyEmailForm formStruct.VerifyEmailForm modelHelper.BindValueForm(&verifyEmailForm, r) log.Debugf("verifyEmailForm.ActivationToken : %s", verifyEmailForm.ActivationToken) if db.ORM.Where(&model.User{ActivationToken: verifyEmailForm.ActivationToken}).First(&user).RecordNotFound() { diff --git a/templates/user/register.html b/templates/user/register.html new file mode 100644 index 00000000..ad21db4b --- /dev/null +++ b/templates/user/register.html @@ -0,0 +1,66 @@ +{{define "title"}}{{.Torrent.Name}}{{end}} +{{define "contclass"}}cont-view{{end}} +{{define "content"}} +
+
+
+
+

Please Sign Up It's free and always will be.

+
+
+ +
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+ + + + +
+
+ By clicking Register, you agree to the Terms and Conditions set out by this site, including our Cookie Use. +
+
+ +
+
+
+ +
+
+
+
+ + +
+{{end}} \ No newline at end of file