/
groups.go
263 lines (232 loc) · 6.68 KB
/
groups.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* ArkeoGIS - The Geographic Information System for Archaeologists
* Copyright (C) 2015-2016 CROLL SAS
*
* Authors :
* Nicolas Dimitrijevic <nicolas@croll.fr>
* Christophe Beveraggi <beve@croll.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package rest
import (
"encoding/json"
"log"
"net/http"
"reflect"
"strings"
db "github.com/croll/arkeogis-server/db"
"github.com/croll/arkeogis-server/model"
"github.com/croll/arkeogis-server/webserver/routes"
)
// GroupListParams is params struct for GroupList query
type GroupListParams struct {
Type string `default:"user" enum:"user,charac,chronology"`
Limit int `default:"10" min:"1" max:"100" error:"limit over boundaries"`
Page int `default:"1" min:"1" error:"page over boundaries"`
Order string `default:"g_tr.name" enum:"g.created_at,-g.created_at,g.updated_at,-g.updated_at,g_tr.name,-g_tr.name" error:"bad order"`
Filter string `default:""`
}
type GroupGetParams struct {
Id int `min:"0" error:"id over boundaries"`
}
type GroupSetPost struct {
model.Group
Users []model.User `json:"users" ignore:"true"`
}
func init() {
Routes := []*routes.Route{
/*&routes.Route{
Path: "/api/groups",
Description: "Create a new arkeogis group",
Func: GroupCreate,
Method: "POST",
Json: reflect.TypeOf(Groupcreate{}),
Permissions: []string{
//"AdminGroups",
},
},*/
&routes.Route{
Path: "/api/groups",
Description: "List arkeogis groups",
Func: GroupList,
Method: "GET",
Permissions: []string{
"request map", // logged at least
},
Params: reflect.TypeOf(GroupListParams{}),
},
&routes.Route{
Path: "/api/groups/{id:[0-9]+}",
Description: "Get an arkeogis group",
Func: GroupGet,
Method: "GET",
Permissions: []string{
"adminusers",
},
Params: reflect.TypeOf(GroupGetParams{}),
},
&routes.Route{
Path: "/api/groups/{id:[0-9]+}",
Description: "Update an arkeogis group",
Func: GroupSet,
Method: "POST",
Json: reflect.TypeOf(GroupSetPost{}),
Permissions: []string{
"adminusers",
},
},
/*&routes.Route{
Path: "/api/groups/{id:[0-9]+}",
Description: "Update an arkeogis group",
Func: GroupUpdate,
Method: "POST",
Json: reflect.TypeOf(Groupcreate{}),
Permissions: []string{
//"AdminGroups",
},
},*/
/*&routes.Route{
Path: "/api/groups",
Description: "Delete an arkeogis group",
Func: GroupDelete,
Method: "DELETE",
Permissions: []string{
//"AdminGroups",
},
},*/
}
routes.RegisterMultiple(Routes)
}
// GroupList List the groups.
func GroupList(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
type TRGroup struct {
model.Group
model.Group_tr
}
type Answer struct {
Data []TRGroup `json:"data"`
Count int `json:"count"`
}
answer := Answer{}
params := proute.Params.(*GroupListParams)
// decode order...
order := params.Order
orderdir := "ASC"
if strings.HasPrefix(order, "-") {
order = order[1:]
orderdir = "DESC"
}
/////
offset := (params.Page - 1) * params.Limit
// get groups
err := db.DB.Select(&answer.Data,
" SELECT * FROM \"group\" g "+
" LEFT JOIN \"group_tr\" g_tr ON g.id = g_tr.group_id "+
" WHERE g_tr.name ILIKE $1 AND g.type=$2 AND g.id > 0 AND g_tr.lang_isocode=$3"+
" ORDER BY "+order+" "+orderdir+
" OFFSET $4 "+
" LIMIT $5",
"%"+params.Filter+"%", params.Type, proute.Lang1.Isocode, offset, params.Limit)
/*
// get groups
err := db.DB.Select(&answer.Data,
" SELECT * FROM \"group\" g "+
" LEFT JOIN \"group_tr\" g_tr ON g.id = g_tr.group_id "+
" WHERE g_tr.name ILIKE $1 AND g.type=$2 AND g.id > 0"+
" ORDER BY "+order+" "+orderdir+
" OFFSET $3 "+
" LIMIT $4",
"%"+params.Filter+"%", params.Type, offset, params.Limit)
*/
if err != nil {
userSqlError(w, err)
log.Println("get groups failed", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
return
}
// get total count
err = db.DB.Get(&answer.Count, "SELECT count(g.*) FROM \"group\" g LEFT JOIN \"group_tr\" g_tr ON g.id = g_tr.group_id WHERE g_tr.name ILIKE $1 AND g.type=$2 AND g.id > 0 AND g_tr.lang_isocode=$3", "%"+params.Filter+"%", params.Type, proute.Lang1.Isocode)
if err != nil {
log.Println("get total count failed", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
return
}
j, _ := json.Marshal(answer)
w.Write(j)
}
func groupGet(w http.ResponseWriter, r *http.Request, params GroupGetParams) {
type Answer struct {
model.Group
Users []model.User `json:"users"`
}
tx, err := db.DB.Beginx()
if err != nil {
log.Println("can't start transaction", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
return
}
answer := Answer{}
answer.Id = params.Id
answer.Group.Get(tx)
answer.Users, err = answer.Group.GetUsers(tx)
err = tx.Commit()
if err != nil {
log.Println("can't commit", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
return
}
j, _ := json.Marshal(answer)
w.Write(j)
}
func GroupGet(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
params := proute.Params.(*GroupGetParams)
groupGet(w, r, *params)
}
func GroupSet(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
o := proute.Json.(*GroupSetPost)
tx, err := db.DB.Beginx()
if err != nil {
log.Println("can't start transaction", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
return
}
err = o.Group.Update(tx)
if err != nil {
log.Println("can't update group", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
_ = tx.Rollback()
return
}
err = o.Group.SetUsers(tx, o.Users)
if err != nil {
log.Println("can't set group users", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
_ = tx.Rollback()
return
}
err = tx.Commit()
if err != nil {
log.Println("can't commit", err)
routes.ServerError(w, 500, "INTERNAL ERROR")
_ = tx.Rollback()
return
}
groupGet(w, r, GroupGetParams{
Id: o.Id,
})
}
func GroupAddUser(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
}
func GroupDelUser(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
}