-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_test_signal.go
71 lines (56 loc) · 1.8 KB
/
update_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
package commands
import (
"context"
"database/sql"
"fmt"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/exceptions"
"github.com/pkg/errors"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
)
type UpdateTestSignalCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewUpdateTestSignalCommandHandler(dbs func() *db.ReaderWriter) UpdateTestSignalCommandHandler {
return UpdateTestSignalCommandHandler{DBS: dbs}
}
type UpdateTestSignalCommandRequest struct {
ID string
DeviceDefinitionID string
DBCCodesID string
UserDeviceID string
AutoPIUnitID string
Value string
Approved bool
}
type UpdateTestSignalCommandResponse struct {
ID string
}
func (h UpdateTestSignalCommandHandler) Execute(ctx context.Context, command *UpdateTestSignalCommandRequest) (*UpdateTestSignalCommandResponse, error) {
test, err := models.TestSignals(models.TestSignalWhere.ID.EQ(command.ID)).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.ID),
}
}
}
test.DeviceDefinitionID = command.DeviceDefinitionID
test.DBCCodesID = command.DBCCodesID
test.AutopiUnitID = command.AutoPIUnitID
test.UserDeviceID = command.UserDeviceID
test.Value = command.Value
test.Approved = command.Approved
if _, err := test.Update(ctx, h.DBS().Writer.DB, boil.Infer()); err != nil {
return nil, &exceptions.InternalError{
Err: err,
}
}
return &UpdateTestSignalCommandResponse{ID: test.ID}, nil
}