Код готов.
parent
42989eb601
commit
0f93a9cf5a
|
@ -67,9 +67,7 @@ func main() {
|
|||
m.Get("/", auth.LoginRequired, handlers.GetHome)
|
||||
|
||||
m.Get("/list", auth.LoginRequired, handlers.GetUserList)
|
||||
|
||||
m.Get("/search", auth.LoginRequired, handlers.GetSearch)
|
||||
m.Post("/search", auth.LoginRequired, handlers.PostSearch)
|
||||
m.Post("/list", auth.LoginRequired, handlers.PostUserList)
|
||||
|
||||
m.NotFound(func(r render.Render) {
|
||||
r.HTML(404, "404", nil)
|
||||
|
|
|
@ -96,7 +96,19 @@ func PostSignup(app application.App, postedUser auth.UserModel, r render.Render)
|
|||
r.Redirect("/login")
|
||||
}
|
||||
|
||||
func GetUserList(app application.App, user auth.User, r render.Render) {
|
||||
func GetUserList(app application.App, r render.Render) {
|
||||
doc := make(map[string]interface{})
|
||||
doc["UsersFound"] = 0
|
||||
var tmp int
|
||||
if err := app.DB.QueryRow(`SELECT COUNT(*) FROM users`).Scan(&tmp); err != nil {
|
||||
err500("can't get total of user profiles from DB: ", err, r)
|
||||
}
|
||||
doc["UsersTotal"] = tmp
|
||||
r.HTML(200, "list", doc)
|
||||
}
|
||||
|
||||
func PostUserList(app application.App, user auth.User, r render.Render, req *http.Request) {
|
||||
pref := req.FormValue("pref")
|
||||
doc := make(map[string]interface{})
|
||||
doc["user"] = user.(*auth.UserModel)
|
||||
var users []auth.UserModel
|
||||
|
@ -119,9 +131,12 @@ func GetUserList(app application.App, user auth.User, r render.Render) {
|
|||
FROM
|
||||
relations
|
||||
WHERE
|
||||
relations.userId=?)`,
|
||||
relations.userId=?)
|
||||
AND ( users.Name LIKE concat(?, '%') OR users.Surname LIKE concat(?, '%') )`,
|
||||
user.(*auth.UserModel).Id,
|
||||
user.(*auth.UserModel).Id,
|
||||
pref,
|
||||
pref,
|
||||
)
|
||||
if err != nil || results == nil {
|
||||
err500("can't get user list from DB: ", err, r)
|
||||
|
@ -135,8 +150,18 @@ func GetUserList(app application.App, user auth.User, r render.Render) {
|
|||
tmp.BirthDate = str2Time(tmpTime, r)
|
||||
tmp.YearsOld = int(time.Since(tmp.BirthDate).Hours() / 8760)
|
||||
users = append(users, tmp)
|
||||
if len(users) >= 100 {
|
||||
doc["msg"] = "( Too much rows in result. We will display only the first 100. )"
|
||||
break
|
||||
}
|
||||
}
|
||||
doc["table"] = users
|
||||
doc["UsersFound"] = len(users)
|
||||
var uTotal int
|
||||
if err := app.DB.QueryRow(`SELECT COUNT(*) FROM users`).Scan(&uTotal); err != nil {
|
||||
err500("can't get total of user profiles from DB: ", err, r)
|
||||
}
|
||||
doc["UsersTotal"] = uTotal
|
||||
r.HTML(200, "list", doc)
|
||||
}
|
||||
|
||||
|
@ -200,14 +225,6 @@ func GetUnSubscribe(app application.App, r render.Render, user auth.User, req *h
|
|||
|
||||
}
|
||||
|
||||
func GetSearch(r render.Render) {
|
||||
r.HTML(200, "search", nil)
|
||||
}
|
||||
|
||||
func PostSearch() {
|
||||
|
||||
}
|
||||
|
||||
func str2Time(s string, r render.Render) time.Time {
|
||||
t, err := time.Parse("2006-01-02 15:04:05", s)
|
||||
if err != nil {
|
||||
|
|
|
@ -18,7 +18,17 @@
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h2>List of all unrelated users</h2>
|
||||
<h2>Search users</h2>
|
||||
<form method="POST">
|
||||
<div>
|
||||
<td><b>Поиск по префиксу имени и(или) фамилии</b></td>
|
||||
<td><input type="text" name="pref" /></td>
|
||||
<td><button>Search</button></td>
|
||||
</div>
|
||||
</form>
|
||||
<br />
|
||||
<div><b>Выбрано {{ .UsersFound }} учеток из {{ .UsersTotal }}</b> <b style="color: red;">{{ .msg }}</b></div>
|
||||
<br />
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
<!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>Search users</h2>
|
||||
<form method="POST">
|
||||
<table>
|
||||
<tr>
|
||||
<td>Префиксу имени и(или) фамилии</td>
|
||||
<td><input type="text" name="pref" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
<button>Search</button>
|
||||
</form>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Years Old</th>
|
||||
<th>Gender</th>
|
||||
<th>City</th>
|
||||
<th>Action</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>
|
||||
<td><a href="/subscribe?id={{ $value.Id }}">Subscribe</a></td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>Available actions:</h2>
|
||||
<input type="button" onclick="location.href='/';" value="Home" />
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue