-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_ds.go
87 lines (68 loc) · 2.32 KB
/
update_ds.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
76
77
78
79
80
81
82
83
84
85
86
87
//nolint:tagliatelle
package commands
import (
"context"
"database/sql"
"github.com/DIMO-Network/device-definitions-api/internal/core/mediator"
"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/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
)
type UpdateDeviceStyleCommand struct {
ID string `json:"id"`
DeviceDefinitionID string `json:"device_definition_id"`
Name string `json:"name"`
ExternalStyleID string `json:"external_style_id"`
Source string `json:"source"`
SubModel string `json:"sub_model"`
HardwareTemplateID string `json:"hardware_template_id,omitempty"`
}
type UpdateDeviceStyleCommandResult struct {
ID string `json:"id"`
}
func (*UpdateDeviceStyleCommand) Key() string { return "UpdateDeviceStyleCommand" }
type UpdateDeviceStyleCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewUpdateDeviceStyleCommandHandler(dbs func() *db.ReaderWriter) UpdateDeviceStyleCommandHandler {
return UpdateDeviceStyleCommandHandler{DBS: dbs}
}
func (ch UpdateDeviceStyleCommandHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
command := query.(*UpdateDeviceStyleCommand)
ds, err := models.DeviceStyles(models.DeviceStyleWhere.ID.EQ(command.ID)).One(ctx, ch.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{
Err: err,
}
}
}
if ds == nil {
ds = &models.DeviceStyle{
ID: command.ID,
DeviceDefinitionID: command.DeviceDefinitionID,
}
}
if len(command.Name) > 0 {
ds.Name = command.Name
}
if len(command.ExternalStyleID) > 0 {
ds.ExternalStyleID = command.ExternalStyleID
}
if len(command.Source) > 0 {
ds.Source = command.Source
}
if len(command.SubModel) > 0 {
ds.SubModel = command.SubModel
}
ds.HardwareTemplateID = null.StringFrom(command.HardwareTemplateID)
if err := ds.Upsert(ctx, ch.DBS().Writer.DB, true, []string{models.DeviceStyleColumns.ID}, boil.Infer(), boil.Infer()); err != nil {
return nil, &exceptions.InternalError{
Err: err,
}
}
return UpdateDeviceStyleCommandResult{ID: ds.ID}, nil
}