From 5c6fd7ebd8417f7eaa23b926ffa99a9affa52b17 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Sat, 9 Jan 2021 07:16:23 -0500 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D0=B9=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2.=20=D0=90=D1=83=D1=82?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F,=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B8=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D1=84=D0=B8=D0=BB=D1=8F,=20?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=84=D0=B8=D0=BB=D0=B5=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 4 +-- internal/auth/user.go | 1 + internal/handlers/handlers.go | 63 +++++++++++++++++++++++++---------- templates/404.tmpl | 2 +- templates/500.tmpl | 2 +- templates/index.tmpl | 3 +- templates/list.tmpl | 44 ++++++++++++++++++++++++ 7 files changed, 96 insertions(+), 23 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 69d5b50..5a4e9ee 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -54,8 +54,8 @@ func main() { }) // Регистрация пользователя, после которой нас перебрасывает на страницу логина - m.Get("/signup", handlers.GetSigned) - m.Post("/signup", binding.Bind(auth.UserModel{}), handlers.PostSigned) + m.Get("/signup", handlers.GetSignup) + m.Post("/signup", binding.Bind(auth.UserModel{}), handlers.PostSignup) //Анкета текущего пользователя m.Get("/", auth.LoginRequired, handlers.GetHome) diff --git a/internal/auth/user.go b/internal/auth/user.go index adbe3eb..5ad9274 100644 --- a/internal/auth/user.go +++ b/internal/auth/user.go @@ -13,6 +13,7 @@ type UserModel struct { Name string `db:"name" form:"name"` Surname string `db:"surname" form:"surname"` BirthDate time.Time `db:"birthdate"` + YearsOld int `db:"-" form:"-"` FormBirthDate string `form:"birthdate"` Gender string `db:"gender" form:"gender"` City string `db:"city" form:"city"` diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 3ceaf53..c1cdb4c 100755 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -13,17 +13,16 @@ import ( ) func GetHome(r render.Render, user auth.User) { + h := user.(*auth.UserModel).BirthDate + user.(*auth.UserModel).YearsOld = int(time.Since(h).Hours()/8760) r.HTML(200, "index", user) } -func GetSigned(r render.Render) { - doc := map[string]interface{}{ - "PageTitle": "page not exists", - } - r.HTML(200, "signup", doc) +func GetSignup(r render.Render) { + r.HTML(200, "signup", nil) } -func PostSigned(app application.App, session sessions.Session, postedUser auth.UserModel, r render.Render, req *http.Request) { +func PostSignup(app application.App, postedUser auth.UserModel, r render.Render) { t, err := time.Parse("2006-1-2", postedUser.FormBirthDate) if err != nil { e := fmt.Errorf("can't parce date: %w", err) @@ -46,20 +45,32 @@ func PostSigned(app application.App, session sessions.Session, postedUser auth.U ) _, 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) + err500("can't create account in DB: ", err, r) } r.Redirect("/login") } -func GetUserList(r render.Render) { - doc := map[string]interface{}{ - "PageTitle": "page not exists", +func GetUserList(app application.App, user auth.User, r render.Render) { + doc := make(map[string]interface{}) + doc["user"]=user.(*auth.UserModel) + var users []auth.UserModel + var tmp auth.UserModel + var tmpTime string + var results, err = app.DB.Query(`SELECT name, surname, birthdate, gender, city FROM users`) + if err != nil || results==nil { + err500("can't get user list from DB: ", err, r) } + defer results.Close() + for results.Next() { + err = results.Scan(&tmp.Name, &tmp.Surname, &tmpTime, &tmp.Gender, &tmp.City) + if err != nil { + err500("can't scan result from DB: ", err, r) + } + tmp.BirthDate = str2Time(tmpTime, r) + tmp.YearsOld = int(time.Since(tmp.BirthDate).Hours()/8760) + users = append(users,tmp) + } + doc["table"]=users r.HTML(200, "list", doc) } @@ -75,12 +86,28 @@ func PostLogin(app application.App, session sessions.Session, postedUser auth.Us } else { err := auth.AuthenticateSession(session, &user) if err != nil { - r.JSON(500, err) + err500("can't auth session: ", err, r) } - params := req.URL.Query() redirect := params.Get(auth.RedirectParam) r.Redirect(redirect) return } -} \ No newline at end of file +} + +func str2Time(s string, r render.Render) time.Time { + t, err := time.Parse("2006-01-02 15:04:05", s) + if err != nil { + err500("can't parce date: ", err, r) + } + return t +} + +func err500(s string, err error, r render.Render) { + e := fmt.Errorf("s% %w", s, err) + log.Println(e) + doc := map[string]interface{}{ + "Error": e, + } + r.HTML(500, "500", doc) +} diff --git a/templates/404.tmpl b/templates/404.tmpl index 900c91a..58da1fb 100644 --- a/templates/404.tmpl +++ b/templates/404.tmpl @@ -1,7 +1,7 @@ -

404. Page not found

+

404. Page not found

Home
\ No newline at end of file diff --git a/templates/500.tmpl b/templates/500.tmpl index 3d6f70e..4cee529 100644 --- a/templates/500.tmpl +++ b/templates/500.tmpl @@ -1,7 +1,7 @@ -

500. INTERNAL SERVER ERROR!

+

500. INTERNAL SERVER ERROR!

{{ .Error }}

\ No newline at end of file diff --git a/templates/index.tmpl b/templates/index.tmpl index c567373..f536666 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -1,9 +1,10 @@ +

Your account data

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

You gender is {{ .Gender }}

-

You {{ .BirthDate }} years old

+

You {{ .YearsOld }} years old

You now live in {{ .City }}

You interests is: {{ .Interests }}

diff --git a/templates/list.tmpl b/templates/list.tmpl index e69de29..aff0ab0 100644 --- a/templates/list.tmpl +++ b/templates/list.tmpl @@ -0,0 +1,44 @@ + + + + + + +

List of all users

+ + + + + + + + + + + {{ range $value:=.table }} + + + + + + + {{ end }} + +
NameYears OldGenderCity
{{ $value.Name }} {{ $value.Surname }}{{ $value.YearsOld }}{{ $value.Gender }}{{ $value.City }}
+ + + \ No newline at end of file