Основной функционал готов. Аутентификация, создание и отображение профиля, отображение списка профилей.

actency-mysql57-replication
Andrey Ivanov 2021-01-09 07:16:23 -05:00 committed by Andrey Ivanov
parent 58e31116e8
commit 5c6fd7ebd8
7 changed files with 96 additions and 23 deletions

View File

@ -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)

View File

@ -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"`

View File

@ -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
}
}
}
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)
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<body>
<p>404. Page not found</p>
<h2>404. Page not found</h2>
<a href="/">Home</a><br/>
</body>
</html>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<body>
<p>500. INTERNAL SERVER ERROR!</p>
<h2>500. INTERNAL SERVER ERROR!</h2>
<p>{{ .Error }}</p>
</body>
</html>

View File

@ -1,9 +1,10 @@
<!DOCTYPE html>
<html>
<body>
<h2> Your account data</h2>
<p> Hello <b>{{ .Name }} {{ .Surname }}</b></p>
<p>You gender is <b>{{ .Gender }}</b></p>
<p>You <b>{{ .BirthDate }}</b> years old</p>
<p>You <b>{{ .YearsOld }}</b> years old</p>
<p>You now live in <b>{{ .City }}</b></p>
<p>You interests is: <b>{{ .Interests }}</b></p>

View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
border: 1px solid black;
width: 100%;
background-color: azure;
}
table th {
border: 1px solid black;
background-color: aquamarine;
}
table td {
border: 1px solid black;
}
</style>
</head>
<body>
<h2>List of all users</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Years Old</th>
<th>Gender</th>
<th>City</th>
</tr>
</thead>
<tbody>
{{ range $value:=.table }}
<tr>
<td><a>{{ $value.Name }} {{ $value.Surname }}</a></td>
<td><a>{{ $value.YearsOld }}</a></td>
<td><a>{{ $value.Gender }}</a></td>
<td><a>{{ $value.City }}</a></td>
</tr>
{{ end }}
</tbody>
</table>
<input type="button" onclick="location.href='/';" value="Home" />
</body>
</html>