-
Notifications
You must be signed in to change notification settings - Fork 1
/
device_definition_repo.go
105 lines (83 loc) · 3.25 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
//go:generate mockgen -source device_definition_repo.go -destination mocks/device_definition_repo_mock.go -package mocks
package repositories
import (
"context"
"database/sql"
"errors"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/models"
"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)
}
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_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 {
return nil, err
}
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 {
return nil, err
}
return dd, err
}
func (r *deviceDefinitionRepository) GetByID(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(qm.Rels(models.DeviceDefinitionRels.DeviceIntegrations, models.DeviceIntegrationRels.Integration))).
One(ctx, r.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
panic(err)
}
return nil, nil
}
if dd.R == nil || dd.R.DeviceMake == nil {
return nil, 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) {
panic(err)
}
return nil, nil
}
return dd, nil
}