-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
zone.go
90 lines (75 loc) · 2.4 KB
/
zone.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
package repo
import (
"context"
"database/sql"
"github.com/pkg/errors"
"github.com/uptrace/bun"
"exusiai.dev/backend-next/internal/model"
modelv2 "exusiai.dev/backend-next/internal/model/v2"
"exusiai.dev/backend-next/internal/pkg/pgerr"
"exusiai.dev/backend-next/internal/repo/selector"
)
type Zone struct {
db *bun.DB
v2sel selector.S[modelv2.Zone]
v3sel selector.S[model.Zone]
}
func NewZone(db *bun.DB) *Zone {
return &Zone{
db: db,
v2sel: selector.New[modelv2.Zone](db),
v3sel: selector.New[model.Zone](db),
}
}
func (r *Zone) GetZones(ctx context.Context) ([]*model.Zone, error) {
return r.v3sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Order("zone_id ASC")
})
}
func (r *Zone) GetZoneById(ctx context.Context, id int) (*model.Zone, error) {
return r.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("zone_id = ?", id)
})
}
func (c *Zone) GetZoneByArkId(ctx context.Context, arkZoneId string) (*model.Zone, error) {
return c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("ark_zone_id = ?", arkZoneId)
})
}
func (r *Zone) GetShimZones(ctx context.Context) ([]*modelv2.Zone, error) {
var zones []*modelv2.Zone
err := r.db.NewSelect().
Model(&zones).
// `Stages` shall match the model's `Stages` field name, rather for any else references
Relation("Stages", func(q *bun.SelectQuery) *bun.SelectQuery {
// zone_id is for go-pg/pg's internal joining for has-many relationship. removing it will cause an error
// see https://github.com/go-pg/pg/issues/1315
return q.Column("ark_stage_id", "zone_id")
}).
Order("zone_id ASC").
Scan(ctx)
if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}
return zones, nil
}
func (r *Zone) GetShimZoneByArkId(ctx context.Context, arkZoneId string) (*modelv2.Zone, error) {
var zone modelv2.Zone
err := r.db.NewSelect().
Model(&zone).
Relation("Stages", func(q *bun.SelectQuery) *bun.SelectQuery {
// zone_id is for go-pg/pg's internal joining for has-many relationship. removing it will cause an error
// see https://github.com/go-pg/pg/issues/1315
return q.Column("ark_stage_id", "zone_id")
}).
Where("ark_zone_id = ?", arkZoneId).
Scan(ctx)
if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}
return &zone, nil
}