-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_integration_feature.go
56 lines (43 loc) · 1.87 KB
/
create_integration_feature.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
//nolint:tagliatelle
package commands
import (
"context"
"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 CreateIntegrationFeatureCommand struct {
ID string `json:"id"`
ElasticProperty string `json:"elastic_property"`
DisplayName string `json:"display_name"`
CSSIcon string `json:"css_icon"`
FeatureWeight float64 `json:"feature_weight"`
}
type CreateIntegrationFeatureCommandResult struct {
ID string `json:"id"`
}
func (*CreateIntegrationFeatureCommand) Key() string { return "CreateIntegrationFeatureCommand" }
type CreateIntegrationFeatureCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewCreateIntegrationFeatureCommandHandler(dbs func() *db.ReaderWriter) CreateIntegrationFeatureCommandHandler {
return CreateIntegrationFeatureCommandHandler{DBS: dbs}
}
func (ch CreateIntegrationFeatureCommandHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
command := query.(*CreateIntegrationFeatureCommand)
feature := models.IntegrationFeature{}
feature.FeatureKey = command.ID
feature.ElasticProperty = command.ElasticProperty
feature.DisplayName = command.DisplayName
feature.CSSIcon = null.StringFrom(command.CSSIcon)
feature.FeatureWeight = null.Float64From(command.FeatureWeight)
err := feature.Insert(ctx, ch.DBS().Writer, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{Err: errors.Wrapf(err, "error inserting integration feature: %s", feature.FeatureKey)}
}
return CreateIntegrationFeatureCommandResult{ID: feature.FeatureKey}, nil
}