-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathentity_mutations.go
352 lines (316 loc) · 9.29 KB
/
entity_mutations.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
package dbfinder
import (
"context"
"errors"
"fmt"
"strconv"
sq "github.com/Masterminds/squirrel"
"github.com/interline-io/transitland-dbutil/dbutil"
"github.com/interline-io/transitland-lib/tl"
"github.com/interline-io/transitland-lib/tl/tt"
"github.com/interline-io/transitland-lib/tldb"
"github.com/interline-io/transitland-mw/auth/authz"
"github.com/interline-io/transitland-server/model"
)
func (f *Finder) StopCreate(ctx context.Context, input model.StopSetInput) (int, error) {
if input.FeedVersion == nil || input.FeedVersion.ID == nil {
return 0, errors.New("feed_version_id required")
}
return createUpdateStop(ctx, input)
}
func (f *Finder) StopUpdate(ctx context.Context, input model.StopSetInput) (int, error) {
if input.ID == nil {
return 0, errors.New("id required")
}
return createUpdateStop(ctx, input)
}
func (f *Finder) StopDelete(ctx context.Context, id int) error {
ent := tl.Stop{}
ent.ID = id
return deleteEnt(ctx, &ent)
}
func createUpdateStop(ctx context.Context, input model.StopSetInput) (int, error) {
return createUpdateEnt(
ctx,
input.ID,
fvint(input.FeedVersion),
&tl.Stop{},
func(ent *tl.Stop) ([]string, error) {
var cols []string
cols = checkCol(&ent.StopID, input.StopID, "stop_id", cols)
cols = checkCol(&ent.StopCode, input.StopCode, "stop_code", cols)
cols = checkCol(&ent.StopDesc, input.StopDesc, "stop_desc", cols)
cols = checkCol(&ent.StopTimezone, input.StopTimezone, "stop_timezone", cols)
cols = checkCol(&ent.StopName, input.StopName, "stop_name", cols)
cols = checkCol(&ent.StopURL, input.StopURL, "stop_url", cols)
cols = checkCol(&ent.LocationType, input.LocationType, "location_type", cols)
cols = checkCol(&ent.WheelchairBoarding, input.WheelchairBoarding, "wheelchair_boarding", cols)
cols = checkCol(&ent.ZoneID, input.ZoneID, "zone_id", cols)
cols = scanCol(&ent.TtsStopName, input.TtsStopName, "tts_stop_name", cols)
cols = scanCol(&ent.PlatformCode, input.PlatformCode, "platform_code", cols)
if input.Geometry != nil && input.Geometry.Valid {
cols = checkCol(&ent.Geometry, input.Geometry, "geometry", cols)
}
if v := input.Parent; v != nil {
cols = append(cols, "parent_station")
if v.ID == nil {
ent.ParentStation.Valid = false
} else {
fmt.Println("setting to", *v.ID)
checkParent := tl.Stop{}
checkParent.ID = *v.ID
ent.ParentStation = tt.NewKey(strconv.Itoa(checkParent.ID))
}
}
if v := input.Level; v != nil {
cols = append(cols, "level_id")
if v.ID == nil {
ent.LevelID.Valid = false
} else {
checkLevel := tl.Level{}
checkLevel.ID = *v.ID
ent.LevelID = tt.NewKey(strconv.Itoa(checkLevel.ID))
}
}
return cols, nil
})
}
///////////
func (f *Finder) PathwayCreate(ctx context.Context, input model.PathwaySetInput) (int, error) {
if input.FeedVersion == nil || input.FeedVersion.ID == nil {
return 0, errors.New("feed_version_id required")
}
return createUpdatePathway(ctx, input)
}
func (f *Finder) PathwayUpdate(ctx context.Context, input model.PathwaySetInput) (int, error) {
if input.ID == nil {
return 0, errors.New("id required")
}
return createUpdatePathway(ctx, input)
}
func (f *Finder) PathwayDelete(ctx context.Context, id int) error {
ent := tl.Pathway{}
ent.ID = id
return deleteEnt(ctx, &ent)
}
func createUpdatePathway(ctx context.Context, input model.PathwaySetInput) (int, error) {
return createUpdateEnt(
ctx,
input.ID,
fvint(input.FeedVersion),
&tl.Pathway{},
func(ent *tl.Pathway) ([]string, error) {
var cols []string
cols = checkCol(&ent.PathwayID, input.PathwayID, "pathway_id", cols)
cols = checkCol(&ent.PathwayMode, input.PathwayMode, "pathway_mode", cols)
cols = checkCol(&ent.IsBidirectional, input.IsBidirectional, "is_bidirectional", cols)
cols = checkCol(&ent.Length, input.Length, "length", cols)
cols = checkCol(&ent.TraversalTime, input.TraversalTime, "traversal_time", cols)
cols = checkCol(&ent.StairCount, input.StairCount, "stair_count", cols)
cols = checkCol(&ent.MaxSlope, input.MaxSlope, "max_slope", cols)
cols = checkCol(&ent.MinWidth, input.MinWidth, "min_width", cols)
cols = checkCol(&ent.SignpostedAs, input.SignpostedAs, "signposted_as", cols)
cols = checkCol(&ent.ReverseSignpostedAs, input.ReverseSignpostedAs, "reverse_signposted_as", cols)
if v := input.FromStop; v != nil {
cols = append(cols, "from_stop_id")
if v.ID == nil {
ent.FromStopID = ""
} else {
checkStop := tl.Stop{}
checkStop.ID = *v.ID
ent.FromStopID = strconv.Itoa(checkStop.ID)
}
}
if v := input.ToStop; v != nil {
cols = append(cols, "to_stop_id")
if v.ID == nil {
ent.ToStopID = ""
} else {
checkStop := tl.Stop{}
checkStop.ID = *v.ID
ent.ToStopID = strconv.Itoa(checkStop.ID)
}
}
return cols, nil
})
}
///////////
func (f *Finder) LevelCreate(ctx context.Context, input model.LevelSetInput) (int, error) {
if input.FeedVersion == nil || input.FeedVersion.ID == nil {
return 0, errors.New("feed_version_id required")
}
return createUpdateLevel(ctx, input)
}
func (f *Finder) LevelUpdate(ctx context.Context, input model.LevelSetInput) (int, error) {
if input.ID == nil {
return 0, errors.New("id required")
}
return createUpdateLevel(ctx, input)
}
func (f *Finder) LevelDelete(ctx context.Context, id int) error {
ent := tl.Level{}
ent.ID = id
return deleteEnt(ctx, &ent)
}
func createUpdateLevel(ctx context.Context, input model.LevelSetInput) (int, error) {
return createUpdateEnt(
ctx,
input.ID,
fvint(input.FeedVersion),
&model.Level{},
func(ent *model.Level) ([]string, error) {
var cols []string
cols = checkCol(&ent.LevelID, input.LevelID, "level_id", cols)
cols = checkCol(&ent.LevelName, input.LevelName, "level_name", cols)
cols = checkCol(&ent.LevelIndex, input.LevelIndex, "level_index", cols)
cols = checkCol(&ent.Geometry, input.Geometry, "geometry", cols)
if v := input.Parent; v != nil {
cols = append(cols, "parent_station")
if v.ID == nil {
ent.ParentStation.Valid = false
} else {
checkParent := tl.Stop{}
checkParent.ID = *v.ID
ent.ParentStation = tt.NewKey(strconv.Itoa(checkParent.ID))
}
}
return cols, nil
})
}
///////////
func checkCol[T any, P *T](val P, inval P, colname string, cols []string) []string {
if inval != nil {
*val = *inval
cols = append(cols, colname)
}
return cols
}
type canScan interface {
Scan(any) error
}
func scanCol[T any, PT *T](val canScan, inval PT, colname string, cols []string) []string {
if inval != nil {
if err := val.Scan(*inval); err != nil {
panic(err)
}
cols = append(cols, colname)
}
return cols
}
type hasTableName interface {
TableName() string
GetFeedVersionID() int
SetFeedVersionID(int)
GetID() int
SetID(int)
Errors() []error
}
func fvint(fvi *model.FeedVersionInput) *int {
if fvi == nil {
return nil
}
return fvi.ID
}
func toAtx(ctx context.Context) tldb.Adapter {
return tldb.NewPostgresAdapterFromDBX(model.ForContext(ctx).Finder.DBX())
}
// ensure we have edit rights to fvid
func createUpdateEnt[T hasTableName](
ctx context.Context,
entId *int,
fvid *int,
baseEnt T,
updateFunc func(baseEnt T) ([]string, error),
) (int, error) {
update := (entId != nil && *entId > 0)
atx := toAtx(ctx)
retId := 0
// Update or create?
if update {
baseEnt.SetID(*entId)
if err := atx.Find(baseEnt); err != nil {
return 0, err
}
} else if fvid != nil {
baseEnt.SetFeedVersionID(*fvid)
} else {
return 0, errors.New("id or feed_version_id required")
}
// Check we can edit this feed version
if err := checkFeedEdit(ctx, baseEnt.GetFeedVersionID()); err != nil {
return 0, err
}
// Update columns
cols, err := updateFunc(baseEnt)
if err != nil {
return 0, err
}
// Validate
if errs := baseEnt.Errors(); len(errs) > 0 {
return 0, errs[0]
}
// Save
err = nil
if update {
retId = baseEnt.GetID()
err = atx.Update(baseEnt, cols...)
} else {
retId, err = atx.Insert(baseEnt)
}
if err != nil {
return 0, err
}
return retId, nil
}
// ensure we have edit rights to fvid
func deleteEnt(ctx context.Context, ent hasTableName) error {
// Get fvid
entId := ent.GetID()
fvid := ent.GetFeedVersionID()
db := model.ForContext(ctx).Finder.DBX()
if err := dbutil.Get(
ctx,
db,
sq.StatementBuilder.
Select("feed_version_id").
From(ent.TableName()).
Where(sq.Eq{"id": entId}),
&fvid,
); err != nil {
return err
}
// // Check if we can edit fv
if err := checkFeedEdit(ctx, fvid); err != nil {
return err
}
// Perform deletion
// TODO: use dbutil.Delete
atx := toAtx(ctx)
_, err := atx.Sqrl().Delete(ent.TableName()).Where(sq.Eq{"id": entId}).Exec()
return err
}
func checkFeedEdit(ctx context.Context, fvid int) error {
if fvid <= 0 {
return errors.New("invalid feed version id")
}
cfg := model.ForContext(ctx)
// Check feed permissions
if whoami := cfg.Whoami; whoami == nil {
// Not ok
} else if me, err := whoami.Me(ctx, &authz.MeRequest{}); err != nil {
// Not ok
} else if me.IsGlobalAdmin {
// OK
return nil
}
if checker := cfg.Checker; checker == nil {
// Not ok
} else if check, err := checker.FeedVersionPermissions(ctx, &authz.FeedVersionRequest{Id: int64(fvid)}); err != nil {
// Not ok
} else if check.Actions.CanEdit {
// OK
return nil
}
// Failed
return authz.ErrUnauthorized
}