-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiber_controller_admin.go
174 lines (155 loc) · 4.97 KB
/
fiber_controller_admin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package backend
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/gopsql/pagination/v2"
"github.com/gopsql/psql"
)
// NewFiberAdminsCtrl creates a simple admins controller for fiber.
func (backend *Backend) NewFiberAdminsCtrl() *fiberAdminsCtrl {
return &fiberAdminsCtrl{
backend: backend,
}
}
type fiberAdminsCtrl struct {
backend *Backend
}
func (ctrl fiberAdminsCtrl) List(c FiberCtx) error {
mAdmins := ctrl.backend.ModelByName(getName(c, "Admin"))
q := pagination.PaginationQuerySort{
pagination.Pagination{
MaxPer: 50,
DefaultPer: 20,
},
pagination.Query{},
pagination.Sort{
AllowedSorts: map[string]string{
"name": mAdmins.ToColumnName("Name"),
"created_at": mAdmins.ToColumnName("CreatedAt"),
},
DefaultSort: "created_at",
DefaultOrder: "asc",
},
}
pagination.Bind(&q, c.QueryParser)
var cond []string
var args []interface{}
if pattern := q.GetLikePattern(); pattern != "" {
var like string
if mAdmins.Connection() != nil && mAdmins.Connection().DriverName() == "sqlite" {
like = "LIKE" // SQLite LIKE operator is case-insensitive
} else {
like = "ILIKE"
}
cond = append(cond, fmt.Sprintf("%s %s $?", mAdmins.ToColumnName("Name"), like))
args = append(args, pattern)
}
if c.Query("status") == "deleted" {
cond = append(cond, fmt.Sprintf("%s IS NOT NULL", mAdmins.ToColumnName("DeletedAt")))
} else {
cond = append(cond, fmt.Sprintf("%s IS NULL", mAdmins.ToColumnName("DeletedAt")))
}
var sql string
if len(cond) > 0 {
for i := range cond {
cond[i] = strings.Replace(cond[i], "$?", fmt.Sprintf("$%d", i+1), -1)
}
sql = strings.Join(cond, " AND ")
}
count := mAdmins.Where(sql, args...).MustCount()
admins := mAdmins.NewSlice()
mAdmins.Find().Where(sql, args...).OrderBy(q.OrderByValue()).Limit(q.Limit()).Offset(q.Offset()).MustQuery(admins.Interface())
ret := struct {
Admins []interface{}
SessionsCount map[int]int
Pagination pagination.PaginationQuerySortResult
}{Pagination: q.PaginationQuerySortResult(count)}
var ids []string
for i := 0; i < admins.Elem().Len(); i++ {
elem := admins.Elem().Index(i).Addr().Interface()
if admin, ok := elem.(Serializable); ok {
ret.Admins = append(ret.Admins, admin.Serialize("list"))
} else {
ret.Admins = append(ret.Admins, elem)
}
if admin, ok := elem.(IsAdmin); ok {
ids = append(ids, strconv.Itoa(admin.GetId()))
}
}
if len(ids) > 0 {
adminId := getName(c, "AdminId")
m := ctrl.backend.ModelByName(getName(c, "AdminSession"))
m.Select(m.ToColumnName(adminId), "COUNT(*)").Tap(func(s *psql.SelectSQL) *psql.SelectSQL {
return s.Where(fmt.Sprintf("%s IN (%s)", m.ToColumnName(adminId), strings.Join(ids, ",")))
}).GroupBy(m.ToColumnName(adminId)).MustQuery(&ret.SessionsCount)
}
return c.JSON(ret)
}
func (ctrl fiberAdminsCtrl) Show(c FiberCtx) error {
m := ctrl.backend.ModelByName(getName(c, "Admin"))
admin := m.New().Interface()
m.Find().WHERE("Id", "=", c.Params("id")).MustQuery(admin)
if u, ok := admin.(Serializable); ok {
return c.JSON(u.Serialize("show"))
}
return c.JSON(admin)
}
func (ctrl fiberAdminsCtrl) Create(c FiberCtx) error {
m := ctrl.backend.ModelByName(getName(c, "Admin"))
admin := m.New().Interface()
if c.Get("Content-Length") == "0" {
if u, ok := admin.(Serializable); ok {
return c.JSON(u.Serialize("show"))
}
return c.JSON(admin)
}
var id int
changes := m.MustAssign(
admin,
m.Permit(ctrl.params(c, "create")...).Filter(c.Body()),
m.CreatedAt(),
m.UpdatedAt(),
)
ctrl.backend.MustValidateStruct(admin)
m.Insert(changes...).Returning(m.ToColumnName("Id")).MustQueryRow(&id)
m.Find().WHERE("Id", "=", id).MustQuery(admin)
return c.JSON(admin)
}
func (ctrl fiberAdminsCtrl) Update(c FiberCtx) error {
id, _ := strconv.Atoi(c.Params("id"))
m := ctrl.backend.ModelByName(getName(c, "Admin"))
admin := m.New().Interface()
if admin, ok := admin.(IsAdmin); ok {
admin.SetId(id)
}
changes := m.MustAssign(
admin,
m.Permit(ctrl.params(c, "update")...).Filter(c.Body()),
m.UpdatedAt(),
)
ctrl.backend.MustValidateStruct(admin)
m.Update(changes...).WHERE("Id", "=", id).MustExecute()
m.Find().WHERE("Id", "=", id).MustQuery(admin)
return c.JSON(admin)
}
func (ctrl fiberAdminsCtrl) Restore(c FiberCtx) error {
ctrl.backend.ModelByName(getName(c, "Admin")).Update("DeletedAt", nil).WHERE("Id", "=", c.Params("id")).MustExecute()
return ctrl.Show(c)
}
func (ctrl fiberAdminsCtrl) Destroy(c FiberCtx) error {
ctrl.backend.ModelByName(getName(c, "Admin")).
Update("DeletedAt", time.Now().UTC().Truncate(time.Second)).
WHERE("Id", "=", c.Params("id")).MustExecute()
ctrl.backend.ModelByName(getName(c, "AdminSession")).
Delete().WHERE(getName(c, "AdminId"), "=", c.Params("id")).MustExecute()
return ctrl.Show(c)
}
func (ctrl fiberAdminsCtrl) params(c FiberCtx, action string) []string {
admin := ctrl.backend.ModelByName(getName(c, "Admin")).New().Interface()
if admin, ok := admin.(HasParams); ok {
return admin.Params(action)
}
return []string{"Name", "Password"}
}