-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_definition_repo.go
203 lines (168 loc) · 6.56 KB
/
device_definition_repo.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
//go:generate mockgen -source device_definition_repo.go -destination mocks/device_definition_repo_mock.go -package mocks
package repositories
import (
"context"
"database/sql"
"strings"
"github.com/DIMO-Network/device-definitions-api/internal/core/common"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/models"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/exceptions"
"github.com/DIMO-Network/shared/db"
"github.com/pkg/errors"
"github.com/segmentio/ksuid"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
type DeviceDefinitionRepository interface {
GetByID(ctx context.Context, id string) (*models.DeviceDefinition, error)
GetByMakeModelAndYears(ctx context.Context, make string, model string, year int, loadIntegrations bool) (*models.DeviceDefinition, error)
GetAll(ctx context.Context, verified bool) ([]*models.DeviceDefinition, error)
GetWithIntegrations(ctx context.Context, id string) (*models.DeviceDefinition, error)
GetOrCreate(ctx context.Context, source string, make string, model string, year int) (*models.DeviceDefinition, error)
FetchDeviceCompatibility(ctx context.Context, makeID, integrationID, region string) (models.DeviceDefinitionSlice, error)
}
type deviceDefinitionRepository struct {
DBS func() *db.ReaderWriter
}
func NewDeviceDefinitionRepository(dbs func() *db.ReaderWriter) DeviceDefinitionRepository {
return &deviceDefinitionRepository{DBS: dbs}
}
func (r *deviceDefinitionRepository) GetByMakeModelAndYears(ctx context.Context, make string, model string, year int, loadIntegrations bool) (*models.DeviceDefinition, error) {
qms := []qm.QueryMod{
qm.InnerJoin("device_definitions_api.device_makes dm on dm.id = device_definitions.device_make_id"),
qm.Where("dm.name ilike ?", make),
qm.And("model ilike ?", model),
models.DeviceDefinitionWhere.Year.EQ(int16(year)),
qm.Load(models.DeviceDefinitionRels.DeviceMake),
}
if loadIntegrations {
qms = append(qms,
qm.Load(models.DeviceDefinitionRels.DeviceIntegrations),
qm.Load(qm.Rels(models.DeviceDefinitionRels.DeviceIntegrations, models.DeviceIntegrationRels.Integration)))
}
query := models.DeviceDefinitions(qms...)
dd, err := query.One(ctx, r.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{Err: err}
}
return nil, nil
}
return dd, nil
}
func (r *deviceDefinitionRepository) GetAll(ctx context.Context, verified bool) ([]*models.DeviceDefinition, error) {
dd, err := models.DeviceDefinitions(qm.Where("verified = true"),
qm.OrderBy("device_make_id, model, year")).All(ctx, r.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return []*models.DeviceDefinition{}, err
}
return nil, &exceptions.InternalError{Err: err}
}
return dd, err
}
func (r *deviceDefinitionRepository) GetByID(ctx context.Context, id string) (*models.DeviceDefinition, error) {
dd, err := models.DeviceDefinitions(
models.DeviceDefinitionWhere.ID.EQ(id),
qm.Load(models.DeviceDefinitionRels.DeviceIntegrations),
qm.Load(models.DeviceDefinitionRels.DeviceMake),
qm.Load(qm.Rels(models.DeviceDefinitionRels.DeviceIntegrations, models.DeviceIntegrationRels.Integration)),
qm.Load(models.DeviceDefinitionRels.DeviceStyles)).
One(ctx, r.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{Err: err}
}
return nil, nil
}
if dd.R == nil || dd.R.DeviceMake == nil {
return nil, &exceptions.ConflictError{Err: errors.New("required DeviceMake relation is not set")}
}
return dd, nil
}
func (r *deviceDefinitionRepository) GetWithIntegrations(ctx context.Context, id string) (*models.DeviceDefinition, error) {
dd, err := models.DeviceDefinitions(
qm.Where("id = ?", id),
qm.Load(models.DeviceDefinitionRels.DeviceIntegrations),
qm.Load(models.DeviceDefinitionRels.DeviceMake),
qm.Load("DeviceIntegrations.Integration")).
One(ctx, r.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{Err: err}
}
return nil, nil
}
return dd, nil
}
func (r *deviceDefinitionRepository) GetOrCreate(ctx context.Context, source string, make string, model string, year int) (*models.DeviceDefinition, error) {
qms := []qm.QueryMod{
qm.InnerJoin("device_definitions_api.device_makes dm on dm.id = device_definitions.device_make_id"),
qm.Where("dm.name ilike ?", make),
qm.And("model ilike ?", model),
models.DeviceDefinitionWhere.Year.EQ(int16(year)),
qm.Load(models.DeviceDefinitionRels.DeviceMake),
}
query := models.DeviceDefinitions(qms...)
dd, err := query.One(ctx, r.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{Err: err}
}
}
if dd != nil {
return dd, nil
}
// Create device Make
m, err := models.DeviceMakes(models.DeviceMakeWhere.Name.EQ(strings.TrimSpace(make))).One(ctx, r.DBS().Writer)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// create
m = &models.DeviceMake{
ID: ksuid.New().String(),
Name: make,
NameSlug: common.SlugString(make),
}
err = m.Insert(ctx, r.DBS().Writer.DB, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{
Err: errors.Wrapf(err, "error inserting make: %s", make),
}
}
}
}
dd = &models.DeviceDefinition{
ID: ksuid.New().String(),
DeviceMakeID: m.ID,
Model: model,
Year: int16(year),
Source: null.StringFrom(source),
Verified: false,
ModelSlug: common.SlugString(model),
}
err = dd.Insert(ctx, r.DBS().Writer.DB, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{
Err: err,
}
}
return dd, nil
}
func (r *deviceDefinitionRepository) FetchDeviceCompatibility(ctx context.Context, makeID, integrationID, region string) (models.DeviceDefinitionSlice, error) {
res, err := models.DeviceDefinitions(
qm.InnerJoin(
models.TableNames.DeviceIntegrations+" ON "+models.TableNames.DeviceDefinitions+".id = "+models.TableNames.DeviceIntegrations+".device_definition_id",
),
qm.Where("device_definitions.device_make_id = ?", makeID),
qm.Where("device_definitions.year >= ?", 2008),
qm.Where("device_integrations.features IS NOT NULL"),
qm.Where("device_integrations.integration_id = ?", integrationID),
qm.Where("device_integrations.region = ?", region),
qm.Load(models.DeviceDefinitionRels.DeviceIntegrations),
).All(ctx, r.DBS().Reader)
if err != nil {
return nil, &exceptions.InternalError{Err: err}
}
return res, nil
}