Skip to content

Commit 124a291

Browse files
committed
add: user.list
1 parent 8a2e859 commit 124a291

File tree

7 files changed

+156
-41
lines changed

7 files changed

+156
-41
lines changed

app-server.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,25 @@ const usage = `
2020
Postgres-CI app-server
2121
2222
Usage:
23-
-c /path/to/config.yaml (default is /etc/postgres-ci/app-server.yaml)
23+
-c /path/to/config.yaml (if empty app will use environment variables)
2424
-debug (enable debug mode)
25+
26+
Environment variables:
27+
28+
APP_ADDRESS (example: 127.0.0.1:8888)
29+
APP_TEMPLATES (example: /opt/postgres-ci/app-server/templates/)
30+
APP_LOG_LEVEL (one of: info/warning/error)
31+
DB_HOST (example: 10.20.11.42)
32+
DB_PORT (example: 5432)
33+
DB_USERNAME (example: postgres_ci)
34+
DB_PASSWORD (example: PcSd23@@a)
35+
DB_DATABASE (example: postgres_ci)
2536
`
2637

2738
func main() {
2839

2940
flag.BoolVar(&debug, "debug", false, "")
30-
flag.StringVar(&pathToConfig, "c", "/etc/postgres-ci/app-server.yaml", "")
41+
flag.StringVar(&pathToConfig, "c", "", "")
3142

3243
flag.Usage = func() {
3344

assets/templates_src/users/index.html

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ <h1>Users</h1> {% endblock %} {% block breadcrumb%}
77
<div class="box-header">
88
<h3 class="box-title">Users</h3>
99
<div class="box-tools">
10-
<div class="input-group input-group-sm" style="width: 250px;">
11-
<input type="text" name="table_search" class="form-control pull-right" placeholder="Search">
12-
<div class="input-group-btn">
13-
<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
10+
<form method="get" action="/users/">
11+
<div class="input-group input-group-sm" style="width: 250px;">
12+
<input type="text" name="q" value="{{ query }}" class="form-control pull-right" placeholder="Search">
13+
<div class="input-group-btn">
14+
<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
15+
</div>
1416
</div>
15-
</div>
17+
</form>
1618
</div>
1719
</div>
1820
<div class="box-body no-padding">
@@ -23,26 +25,27 @@ <h3 class="box-title">Users</h3>
2325
<th>user name</th>
2426
<th>login</th>
2527
<th>email</th>
28+
<th width="210px">created at</th>
29+
<th width="210px">updated at</th>
2630
{% if CurrentUser.IsSuperuser %}
2731
<th width="150px">-</th>
2832
{% endif %}
2933
</tr>
30-
34+
{% for user in users %}
3135
<tr>
32-
<td>42</td>
33-
<td>user name</td>
34-
<td>login</td>
35-
<td>email</td>
36+
<td>{{ user.ID }}</td>
37+
<td>{{ user.Name }}</td>
38+
<td>{{ user.Login }}</td>
39+
<td>{{ user.Email }}</td>
40+
<td>{{ user.CreatedAt | time:"Mon, 02 Jan 2006 15:04:05 -0700" }}</td>
41+
<td>{{ user.UpdatedAt | time:"Mon, 02 Jan 2006 15:04:05 -0700" }}</td>
3642
{% if CurrentUser.IsSuperuser %}
3743
<td>
38-
<a href=""><i class="fa fa-pencil-square-o" aria-hidden="true"></i>edit</a>
39-
<a href="#" data-toggle="modal" data-target="#confirmAction" data-action="/users/delete/42/"><i class="fa fa-trash" aria-hidden="true"></i>delete</a>
44+
<a href=""><i class="fa fa-pencil-square-o" aria-hidden="true"></i>edit</a>
45+
<a href="#" data-toggle="modal" data-target="#confirmAction" data-action="/users/delete/{{ user.ID }}/"><i class="fa fa-trash" aria-hidden="true"></i>delete</a>
4046
</td>
4147
{% endif %}
4248
</tr>
43-
{% for user in users %}
44-
<tr>
45-
</tr>
4649
{% endfor %}
4750
</tbody>
4851
</table>
@@ -54,6 +57,3 @@ <h3 class="box-title">Users</h3>
5457
</div>
5558
</div>
5659
{% endblock %}
57-
58-
59-

src/app/controllers/users/list.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package users
2+
3+
import (
4+
"github.com/postgres-ci/app-server/src/app/models/users"
5+
"github.com/postgres-ci/app-server/src/tools/limit"
6+
"github.com/postgres-ci/app-server/src/tools/render"
7+
"github.com/postgres-ci/app-server/src/tools/render/pagination"
8+
"github.com/postgres-ci/http200ok"
9+
)
10+
11+
func listHandler(c *http200ok.Context) {
12+
13+
var (
14+
perPage int32 = 20
15+
query = c.Request.URL.Query()
16+
)
17+
list, err := users.List(perPage, limit.Offset(c, perPage), query.Get("q"))
18+
19+
if err != nil {
20+
21+
return
22+
}
23+
24+
render.HTML(c, "users/index.html", render.Context{
25+
"total": list.Total,
26+
"users": list.Users,
27+
"query": query.Get("q"),
28+
"pagination": pagination.New(c, list.Total, perPage),
29+
})
30+
31+
}

src/app/controllers/users/route.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@ package users
22

33
import (
44
"github.com/postgres-ci/app-server/src/app/middleware"
5-
"github.com/postgres-ci/app-server/src/tools/render"
6-
"github.com/postgres-ci/app-server/src/tools/render/pagination"
75
"github.com/postgres-ci/http200ok"
86
)
97

108
func Route(server *http200ok.Server) {
11-
12-
server.Get("/users/", func(c *http200ok.Context) {
13-
14-
render.HTML(c, "users/index.html", render.Context{
15-
"pagination": pagination.New(c, 2562, 20),
16-
})
17-
})
18-
9+
server.Get("/users/", listHandler)
1910
server.Post("/users/delete/:UserID/", middleware.CheckPassword, func(c *http200ok.Context) {
2011

2112
})

src/app/models/build/list.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,9 @@ type list struct {
7272

7373
func List(projectID, branchID, limit, offset int32) (*list, error) {
7474

75-
var (
76-
err error
77-
result list
78-
)
79-
80-
if branchID > 0 {
81-
err = env.Connect().Get(&result, `SELECT total, branches, items FROM build.list_by_branch($1, $2, $3, $4)`, projectID, branchID, limit, offset)
82-
} else {
83-
err = env.Connect().Get(&result, `SELECT total, branches, items FROM build.list($1, $2, $3)`, projectID, limit, offset)
84-
}
75+
var list list
76+
77+
err := env.Connect().Get(&list, `SELECT total, branches, items FROM build.list($1, $2, $3, $4)`, projectID, branchID, limit, offset)
8578

8679
if err != nil {
8780

@@ -90,5 +83,5 @@ func List(projectID, branchID, limit, offset int32) (*list, error) {
9083
return nil, err
9184
}
9285

93-
return &result, nil
86+
return &list, nil
9487
}

src/app/models/users/list.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package users
2+
3+
import (
4+
log "github.com/Sirupsen/logrus"
5+
"github.com/postgres-ci/app-server/src/env"
6+
7+
"encoding/json"
8+
"fmt"
9+
"strings"
10+
"time"
11+
)
12+
13+
type User struct {
14+
ID int32 `json:"user_id"`
15+
Name string `json:"user_name"`
16+
Login string `json:"user_login"`
17+
Email string `json:"user_email"`
18+
IsSuperuser bool `json:"is_superuser"`
19+
CreatedAt time.Time `json:"created_at"`
20+
UpdatedAt time.Time `json:"updated_at"`
21+
}
22+
23+
type Users []User
24+
25+
func (u *Users) Scan(src interface{}) error {
26+
27+
var source []byte
28+
29+
switch src.(type) {
30+
case string:
31+
source = []byte(src.(string))
32+
case []byte:
33+
source = src.([]byte)
34+
default:
35+
return fmt.Errorf("Incompatible type for Users")
36+
}
37+
38+
return json.Unmarshal(source, u)
39+
}
40+
41+
type list struct {
42+
Total int32 `db:"total"`
43+
Users Users `db:"users"`
44+
}
45+
46+
func List(limit, offset int32, query string) (*list, error) {
47+
48+
var list list
49+
50+
err := env.Connect().Get(&list, `SELECT total, users FROM users.list($1, $2, $3)`, limit, offset, strings.TrimSpace(query))
51+
52+
if err != nil {
53+
54+
log.Errorf("Error when fetching users: %v", err)
55+
56+
return nil, err
57+
}
58+
59+
return &list, nil
60+
}

src/common/config.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ import (
77
"fmt"
88
"io/ioutil"
99
"os"
10+
"strconv"
1011
)
1112

1213
func ReadConfig(path string) (Config, error) {
1314

1415
var config Config
1516

17+
if path == "" {
18+
19+
config.setFromEnv()
20+
21+
return config, nil
22+
}
23+
1624
if _, err := os.Open(path); err != nil {
1725

1826
if os.IsNotExist(err) {
@@ -53,6 +61,27 @@ type Connect struct {
5361
Database string `yaml:"database"`
5462
}
5563

64+
func (c *Config) setFromEnv() {
65+
66+
var port uint32 = 5432
67+
68+
if value, err := strconv.ParseUint(os.Getenv("DB_PORT"), 10, 32); err == nil {
69+
70+
port = uint32(value)
71+
}
72+
73+
c.Address = os.Getenv("APP_ADDRESS")
74+
c.Templates = os.Getenv("APP_TEMPLATES")
75+
c.Loglevel = os.Getenv("APP_LOG_LEVEL")
76+
c.Connect = Connect{
77+
Host: os.Getenv("DB_HOST"),
78+
Port: port,
79+
Username: os.Getenv("DB_USERNAME"),
80+
Password: os.Getenv("DB_PASSWORD"),
81+
Database: os.Getenv("DB_DATABASE"),
82+
}
83+
}
84+
5685
func (c *Connect) DSN() string {
5786

5887
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", c.Host, c.Username, c.Password, c.Database)

0 commit comments

Comments
 (0)