-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_make_repo.go
75 lines (63 loc) · 2.49 KB
/
device_make_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
//go:generate mockgen -source device_make_repo.go -destination mocks/device_make_repo_mock.go -package mocks
package repositories
import (
"context"
"database/sql"
"strings"
"github.com/DIMO-Network/shared"
"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 DeviceMakeRepository interface {
GetAll(ctx context.Context) ([]*models.DeviceMake, error)
GetOrCreate(ctx context.Context, makeName string, logURL string, externalIDs string, metadata string, hardwareTemplateID string) (*models.DeviceMake, error)
}
type deviceMakeRepository struct {
DBS func() *db.ReaderWriter
}
func NewDeviceMakeRepository(dbs func() *db.ReaderWriter) DeviceMakeRepository {
return &deviceMakeRepository{
DBS: dbs,
}
}
func (r *deviceMakeRepository) GetAll(ctx context.Context) ([]*models.DeviceMake, error) {
makes, err := models.DeviceMakes(qm.OrderBy(models.DeviceMakeColumns.Name)).All(ctx, r.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return []*models.DeviceMake{}, err
}
return nil, &exceptions.InternalError{Err: err}
}
return makes, err
}
func (r *deviceMakeRepository) GetOrCreate(ctx context.Context, makeName string, logURL string, externalIDs string, metadata string, hardwareTemplateID string) (*models.DeviceMake, error) {
m, err := models.DeviceMakes(models.DeviceMakeWhere.Name.EQ(strings.TrimSpace(makeName))).One(ctx, r.DBS().Writer)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// create
m = &models.DeviceMake{
ID: ksuid.New().String(),
Name: makeName,
NameSlug: shared.SlugString(makeName),
LogoURL: null.StringFrom(logURL),
ExternalIds: null.JSONFrom([]byte(externalIDs)),
Metadata: null.JSONFrom([]byte(metadata)),
HardwareTemplateID: null.StringFrom(hardwareTemplateID),
}
// todo set TokenID. increase from latest tokenId
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", makeName)}
}
return m, nil
}
return nil, errors.Wrapf(err, "error querying for make: %s", makeName)
}
return m, nil
}