forked from brocaar/chirpstack-application-server
/
application.go
370 lines (332 loc) · 8.62 KB
/
application.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package storage
import (
"fmt"
"regexp"
"github.com/brocaar/lora-app-server/internal/codec"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
uuid "github.com/gofrs/uuid"
log "github.com/sirupsen/logrus"
)
var applicationNameRegexp = regexp.MustCompile(`^[\w-]+$`)
// Application represents an application.
type Application struct {
ID int64 `db:"id"`
Name string `db:"name"`
Description string `db:"description"`
OrganizationID int64 `db:"organization_id"`
ServiceProfileID uuid.UUID `db:"service_profile_id"`
PayloadCodec codec.Type `db:"payload_codec"`
PayloadEncoderScript string `db:"payload_encoder_script"`
PayloadDecoderScript string `db:"payload_decoder_script"`
}
// ApplicationListItem devices the application as a list item.
type ApplicationListItem struct {
Application
ServiceProfileName string `db:"service_profile_name"`
}
// Validate validates the data of the Application.
func (a Application) Validate() error {
if !applicationNameRegexp.MatchString(a.Name) {
return ErrApplicationInvalidName
}
return nil
}
// CreateApplication creates the given Application.
func CreateApplication(db sqlx.Queryer, item *Application) error {
if err := item.Validate(); err != nil {
return errors.Wrap(err, "validate error")
}
err := sqlx.Get(db, &item.ID, `
insert into application (
name,
description,
organization_id,
service_profile_id,
payload_codec,
payload_encoder_script,
payload_decoder_script
) values ($1, $2, $3, $4, $5, $6, $7) returning id`,
item.Name,
item.Description,
item.OrganizationID,
item.ServiceProfileID,
item.PayloadCodec,
item.PayloadEncoderScript,
item.PayloadDecoderScript,
)
if err != nil {
return handlePSQLError(Insert, err, "insert error")
}
log.WithFields(log.Fields{
"id": item.ID,
"name": item.Name,
}).Info("application created")
return nil
}
// GetApplication returns the Application for the given id.
func GetApplication(db sqlx.Queryer, id int64) (Application, error) {
var app Application
err := sqlx.Get(db, &app, "select * from application where id = $1", id)
if err != nil {
return app, handlePSQLError(Select, err, "select error")
}
return app, nil
}
// GetApplicationCount returns the total number of applications.
func GetApplicationCount(db sqlx.Queryer, search string) (int, error) {
var count int
if search != "" {
search = "%" + search + "%"
}
err := sqlx.Get(db, &count, `
select
count(*)
from application
where
$1 = ''
or ($1 != '' and name ilike $1)`,
search,
)
if err != nil {
return 0, errors.Wrap(err, "select error")
}
return count, nil
}
// GetApplicationCountForUser returns the total number of applications
// available for the given user.
// When an organizationID is given, the results will be filtered by this
// organization ID.
func GetApplicationCountForUser(db sqlx.Queryer, username string, organizationID int64, search string) (int, error) {
var count int
if search != "" {
search = "%" + search + "%"
}
err := sqlx.Get(db, &count, `
select
count(a.*)
from application a
inner join organization_user ou
on a.organization_id = ou.organization_id
inner join "user" u
on u.id = ou.user_id
where
u.username = $1
and u.is_active = true
and (
$2 = 0
or a.organization_id = $2
)
and (
$3 = ''
or ($3 != '' and a.name ilike $3)
)
`, username, organizationID, search)
if err != nil {
return 0, errors.Wrap(err, "select error")
}
return count, nil
}
// GetApplicationCountForOrganizationID returns the total number of
// applications for the given organization.
func GetApplicationCountForOrganizationID(db sqlx.Queryer, organizationID int64, search string) (int, error) {
var count int
if search != "" {
search = "%" + search + "%"
}
err := sqlx.Get(db, &count, `
select
count(*)
from application
where
organization_id = $1
and (
$2 = ''
or ($2 != '' and name ilike $2)
)`,
organizationID,
search,
)
if err != nil {
return 0, errors.Wrap(err, "select error")
}
return count, nil
}
// GetApplications returns a slice of applications, sorted by name and
// respecting the given limit and offset.
func GetApplications(db sqlx.Queryer, limit, offset int, search string) ([]ApplicationListItem, error) {
var apps []ApplicationListItem
if search != "" {
search = "%" + search + "%"
}
err := sqlx.Select(db, &apps, `
select
a.*,
sp.name as service_profile_name
from application a
inner join service_profile sp
on sp.service_profile_id = a.service_profile_id
where
$3 = ''
or ($3 != '' and a.name ilike $3)
order by
name
limit $1
offset $2`,
limit,
offset,
search,
)
if err != nil {
return nil, errors.Wrap(err, "select error")
}
return apps, nil
}
// GetApplicationsForUser returns a slice of application of which the given
// user is a member of.
func GetApplicationsForUser(db sqlx.Queryer, username string, organizationID int64, limit, offset int, search string) ([]ApplicationListItem, error) {
var apps []ApplicationListItem
if search != "" {
search = "%" + search + "%"
}
err := sqlx.Select(db, &apps, `
select
a.*,
sp.name as service_profile_name
from application a
inner join service_profile sp
on sp.service_profile_id = a.service_profile_id
inner join organization_user ou
on a.organization_id = ou.organization_id
inner join "user" u
on u.id = ou.user_id
where
u.username = $1
and u.is_active = true
and (
$2 = 0
or a.organization_id = $2
)
and (
$5 = ''
or ($5 != '' and a.name ilike $5)
)
order by a.name
limit $3 offset $4
`, username, organizationID, limit, offset, search)
if err != nil {
return nil, errors.Wrap(err, "select error")
}
return apps, nil
}
// GetApplicationsForOrganizationID returns a slice of applications for the given
// organization.
func GetApplicationsForOrganizationID(db sqlx.Queryer, organizationID int64, limit, offset int, search string) ([]ApplicationListItem, error) {
var apps []ApplicationListItem
if search != "" {
search = "%" + search + "%"
}
err := sqlx.Select(db, &apps, `
select
a.*,
sp.name as service_profile_name
from application a
inner join service_profile sp
on sp.service_profile_id = a.service_profile_id
where
a.organization_id = $1
and (
$4 = ''
or ($4 != '' and a.name ilike $4)
)
order by a.name
limit $2 offset $3`,
organizationID,
limit,
offset,
search,
)
if err != nil {
return nil, errors.Wrap(err, "select error")
}
return apps, nil
}
// UpdateApplication updates the given Application.
func UpdateApplication(db sqlx.Execer, item Application) error {
if err := item.Validate(); err != nil {
return fmt.Errorf("validate application error: %s", err)
}
res, err := db.Exec(`
update application
set
name = $2,
description = $3,
organization_id = $4,
service_profile_id = $5,
payload_codec = $6,
payload_encoder_script = $7,
payload_decoder_script = $8
where id = $1`,
item.ID,
item.Name,
item.Description,
item.OrganizationID,
item.ServiceProfileID,
item.PayloadCodec,
item.PayloadEncoderScript,
item.PayloadDecoderScript,
)
if err != nil {
return handlePSQLError(Update, err, "update error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"id": item.ID,
"name": item.Name,
}).Info("application updated")
return nil
}
// DeleteApplication deletes the Application matching the given ID.
func DeleteApplication(db sqlx.Ext, id int64) error {
err := DeleteAllDevicesForApplicationID(db, id)
if err != nil {
return errors.Wrap(err, "delete all nodes error")
}
res, err := db.Exec("delete from application where id = $1", id)
if err != nil {
return handlePSQLError(Delete, err, "delete error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"id": id,
}).Info("application deleted")
return nil
}
// DeleteAllApplicationsForOrganizationID deletes all applications
// given an organization id.
func DeleteAllApplicationsForOrganizationID(db sqlx.Ext, organizationID int64) error {
var apps []Application
err := sqlx.Select(db, &apps, "select * from application where organization_id = $1", organizationID)
if err != nil {
return handlePSQLError(Select, err, "select error")
}
for _, app := range apps {
err = DeleteApplication(db, app.ID)
if err != nil {
return errors.Wrap(err, "delete application error")
}
}
return nil
}