-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_test_signal.go
72 lines (57 loc) · 1.91 KB
/
create_test_signal.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
package commands
import (
"context"
"database/sql"
"fmt"
"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 CreateTestSignalCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewCreateTestSignalCommandHandler(dbs func() *db.ReaderWriter) CreateTestSignalCommandHandler {
return CreateTestSignalCommandHandler{DBS: dbs}
}
type CreateTestSignalCommandRequest struct {
DeviceDefinitionID string
DBCCodesID string
UserDeviceID string
AutoPIUnitID string
Value string
Approved bool
}
type CreateTestSignalCommandResponse struct {
ID string
}
func (h CreateTestSignalCommandHandler) Execute(ctx context.Context, command *CreateTestSignalCommandRequest) (*CreateTestSignalCommandResponse, error) {
dbc, err := models.DBCCodes(models.DBCCodeWhere.ID.EQ(command.DBCCodesID)).One(ctx, h.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{
Err: err,
}
}
if errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("could not find dbc_code id: %s", command.DBCCodesID),
}
}
}
test := models.TestSignal{}
test.ID = ksuid.New().String()
test.DeviceDefinitionID = command.DeviceDefinitionID
test.UserDeviceID = command.UserDeviceID
test.DBCCodesID = dbc.ID
test.AutopiUnitID = command.AutoPIUnitID
test.Value = command.Value
test.Approved = command.Approved
err = test.Insert(ctx, h.DBS().Writer, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{Err: errors.Wrapf(err, "error inserting test signal: %s", command.Value)}
}
return &CreateTestSignalCommandResponse{ID: test.ID}, nil
}