-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_style_repo.go
47 lines (38 loc) · 1.5 KB
/
device_style_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
//go:generate mockgen -source device_style_repo.go -destination mocks/device_style_repo_mock.go -package mocks
package repositories
import (
"context"
"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/segmentio/ksuid"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
)
type DeviceStyleRepository interface {
Create(ctx context.Context, deviceDefinitionID string, name string, externalStyleID string, source string, subModel string, templateID string) (*models.DeviceStyle, error)
}
type deviceStyleRepository struct {
DBS func() *db.ReaderWriter
}
func NewDeviceStyleRepository(dbs func() *db.ReaderWriter) DeviceStyleRepository {
return &deviceStyleRepository{DBS: dbs}
}
func (r *deviceStyleRepository) Create(ctx context.Context, deviceDefinitionID string, name string, externalStyleID string, source string, subModel string, templateID string) (*models.DeviceStyle, error) {
ds := &models.DeviceStyle{
ID: ksuid.New().String(),
DeviceDefinitionID: deviceDefinitionID,
Name: name,
ExternalStyleID: externalStyleID,
Source: source,
SubModel: subModel,
HardwareTemplateID: null.StringFrom(templateID),
}
err := ds.Insert(ctx, r.DBS().Writer, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{
Err: err,
}
}
return ds, nil
}