-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_dbc_code.go
57 lines (44 loc) · 1.54 KB
/
create_dbc_code.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
package commands
import (
"context"
"github.com/volatiletech/null/v8"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/exceptions"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
"github.com/segmentio/ksuid"
)
type CreateDBCCodeCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewCreateDBCCodeCommandHandler(dbs func() *db.ReaderWriter) CreateDBCCodeCommandHandler {
return CreateDBCCodeCommandHandler{DBS: dbs}
}
type CreateDBCCodeCommandRequest struct {
Name string
DBCContents string
Header int
Trigger string
RecordingEnabled bool
MaxSampleSize int32
}
type CreateDBCCodeCommandResponse struct {
ID string
Name string
}
func (h CreateDBCCodeCommandHandler) Execute(ctx context.Context, command *CreateDBCCodeCommandRequest) (*CreateDBCCodeCommandResponse, error) {
dbc := &models.DBCCode{}
dbc.ID = ksuid.New().String()
dbc.Name = command.Name
dbc.DBCContents = null.StringFrom(command.DBCContents)
dbc.Header = null.IntFrom(command.Header)
dbc.Trigger = command.Trigger
dbc.RecordingEnabled = command.RecordingEnabled
dbc.MaxSampleSize = int(command.MaxSampleSize)
err := dbc.Insert(ctx, h.DBS().Writer, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{Err: errors.Wrapf(err, "error inserting dbc_code: %s", command.Name)}
}
return &CreateDBCCodeCommandResponse{ID: dbc.ID, Name: dbc.Name}, nil
}