diff --git a/cmd/main.go b/cmd/main.go index 983d02b..69d5b50 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -55,7 +55,7 @@ func main() { // Регистрация пользователя, после которой нас перебрасывает на страницу логина m.Get("/signup", handlers.GetSigned) - m.Post("/signup", handlers.PostSigned) + m.Post("/signup", binding.Bind(auth.UserModel{}), handlers.PostSigned) //Анкета текущего пользователя m.Get("/", auth.LoginRequired, handlers.GetHome) diff --git a/internal/auth/user.go b/internal/auth/user.go index c5d3b63..adbe3eb 100644 --- a/internal/auth/user.go +++ b/internal/auth/user.go @@ -1,24 +1,23 @@ package auth import ( - "database/sql" "fmt" "github.com/tiburon-777/OTUS_HighLoad/internal/application" "time" ) type UserModel struct { - Id int64 `form:"id" db:"id"` - Username string `form:"name" db:"username"` - Password string `form:"password" db:"password"` - Name string `form:"name" db:"name"` - Surname string `form:"surname" db:"surname"` - BirthDate time.Time `form:"birthdate" db:"birthdate"` - Male bool `form:"male" db:"male"` - City string `form:"city" db:"city"` - Interests string `form:"interests" db:"interests"` - authenticated bool `form:"-" db:"-"` - Db *sql.DB + Id int64 `db:"id" form:"id"` + Username string `db:"username" form:"username"` + Password string `db:"password" form:"password"` + Name string `db:"name" form:"name"` + Surname string `db:"surname" form:"surname"` + BirthDate time.Time `db:"birthdate"` + FormBirthDate string `form:"birthdate"` + Gender string `db:"gender" form:"gender"` + City string `db:"city" form:"city"` + Interests string `db:"interests" form:"interests"` + authenticated bool `db:"-" form:"-"` } func GenerateAnonymousUser() User { @@ -49,11 +48,16 @@ func (u *UserModel) UniqueId() interface{} { } func (u *UserModel) GetById(app application.App, id interface{}) error { - query := fmt.Sprintf("SELECT username FROM users WHERE id=%d", id) - var v []uint8 - err := app.DB.QueryRow(query).Scan(&v) + var v string + query := fmt.Sprintf("SELECT username, name, surname, birthdate, gender, city, interests FROM users WHERE id=%d", id) + err := app.DB.QueryRow(query).Scan(&u.Username, &u.Name, &u.Surname, &v, &u.Gender, &u.City, &u.Interests) if err != nil { return err } + u.BirthDate, err = time.Parse("2006-01-02 15:04:05", v) + if err != nil { + return err + } + u.Id=id.(int64) return nil } \ No newline at end of file diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index df523d3..3ceaf53 100755 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -1,34 +1,61 @@ package handlers import ( + "encoding/base64" "fmt" "github.com/codegangsta/martini-contrib/render" "github.com/codegangsta/martini-contrib/sessions" "github.com/tiburon-777/OTUS_HighLoad/internal/application" "github.com/tiburon-777/OTUS_HighLoad/internal/auth" - "net" + "log" "net/http" + "time" ) -func GetHome(r render.Render) { - doc := map[string]interface{}{ - "PageTitle": "Вы имеете доступ к проектам", - } - r.HTML(200, "index", doc) +func GetHome(r render.Render, user auth.User) { + r.HTML(200, "index", user) } func GetSigned(r render.Render) { doc := map[string]interface{}{ "PageTitle": "page not exists", } - r.HTML(200, "signin", doc) + r.HTML(200, "signup", doc) } -func PostSigned(app application.App, r render.Render) { - r.Redirect(net.JoinHostPort(app.Config.Server.Address, app.Config.Server.Port)+"/login") +func PostSigned(app application.App, session sessions.Session, postedUser auth.UserModel, r render.Render, req *http.Request) { + t, err := time.Parse("2006-1-2", postedUser.FormBirthDate) + if err != nil { + e := fmt.Errorf("can't parce date: %w", err) + log.Println(e) + doc := map[string]interface{}{ + "Error": e, + } + r.HTML(500, "500", doc) + } + query := fmt.Sprintf(`INSERT INTO users (username, password, name, surname, birthdate, gender, city, interests) + values ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")`, + postedUser.Username, + base64.StdEncoding.EncodeToString([]byte(postedUser.Username + ":" + postedUser.Password)), + postedUser.Name, + postedUser.Surname, + t.Format("2006-01-02 15:04:05"), + postedUser.Gender, + postedUser.City, + postedUser.Interests, + ) + _, err = app.DB.Exec(query) + if err != nil { + e := fmt.Errorf("can't create account in DB: %w", err) + log.Println(e) + doc := map[string]interface{}{ + "Error": e, + } + r.HTML(500, "500", doc) + } + r.Redirect("/login") } - func GetUserList(r render.Render) { doc := map[string]interface{}{ "PageTitle": "page not exists", @@ -37,9 +64,10 @@ func GetUserList(r render.Render) { } func PostLogin(app application.App, session sessions.Session, postedUser auth.UserModel, r render.Render, req *http.Request) { + hash := base64.StdEncoding.EncodeToString([]byte(postedUser.Username + ":" + postedUser.Password)) user := auth.UserModel{} - query := fmt.Sprintf("SELECT * FROM users WHERE username=\"%s\" and password =\"%s\"", postedUser.Username, postedUser.Password) - err := app.DB.QueryRow(query).Scan(&user.Id, &user.Username, &user.Password) + query := fmt.Sprintf("SELECT id FROM users WHERE username=\"%s\" and password =\"%s\"", postedUser.Username, hash) + err := app.DB.QueryRow(query).Scan(&user.Id) if err != nil || user.Id==0 { r.Redirect(auth.RedirectUrl) diff --git a/templates/404.tmpl b/templates/404.tmpl index e69de29..900c91a 100644 --- a/templates/404.tmpl +++ b/templates/404.tmpl @@ -0,0 +1,7 @@ + + + +

404. Page not found

+ Home
+ + \ No newline at end of file diff --git a/templates/500.tmpl b/templates/500.tmpl new file mode 100644 index 0000000..3d6f70e --- /dev/null +++ b/templates/500.tmpl @@ -0,0 +1,7 @@ + + + +

500. INTERNAL SERVER ERROR!

+

{{ .Error }}

+ + \ No newline at end of file diff --git a/templates/index.tmpl b/templates/index.tmpl index 767208e..c567373 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -1,8 +1,13 @@ -

This is a private link!

-

Hello {{ .Username }}

- Logout
+

Hello {{ .Name }} {{ .Surname }}

+

You gender is {{ .Gender }}

+

You {{ .BirthDate }} years old

+

You now live in {{ .City }}

+

You interests is: {{ .Interests }}

+ + +
\ No newline at end of file diff --git a/templates/login.tmpl b/templates/login.tmpl index 8ba424a..7fd7a1e 100644 --- a/templates/login.tmpl +++ b/templates/login.tmpl @@ -3,9 +3,10 @@

You must login!

-
- +
+
+
\ No newline at end of file diff --git a/templates/signin.tmpl b/templates/signin.tmpl deleted file mode 100644 index e69de29..0000000 diff --git a/templates/signup.tmpl b/templates/signup.tmpl new file mode 100644 index 0000000..e38cb39 --- /dev/null +++ b/templates/signup.tmpl @@ -0,0 +1,50 @@ + + + + + + +

Create account

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Логин
Имя
Фамилия
Дата рождения
Пол + Мужской + Женский + Другое +
Интересы
Город
Пароль
+ +
+ + \ No newline at end of file