/
create_in.go
62 lines (47 loc) · 1.65 KB
/
create_in.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
//nolint:tagliatelle
package commands
import (
"context"
"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/segmentio/ksuid"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/DIMO-Network/device-definitions-api/internal/core/mediator"
)
type CreateIntegrationCommand struct {
Vendor string `json:"vendor"`
Type string `json:"type"`
Style string `json:"style"`
TokenID int `json:"token_id"`
}
type CreateIntegrationCommandResult struct {
ID string `json:"id"`
}
func (*CreateIntegrationCommand) Key() string { return "CreateIntegrationCommand" }
type CreateIntegrationCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewCreateIntegrationCommandHandler(dbs func() *db.ReaderWriter) CreateIntegrationCommandHandler {
return CreateIntegrationCommandHandler{DBS: dbs}
}
func (ch CreateIntegrationCommandHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
command := query.(*CreateIntegrationCommand)
i := models.Integration{}
i.ID = ksuid.New().String()
i.Vendor = command.Vendor
i.Type = command.Type
i.Style = command.Style
if command.TokenID == 0 {
i.TokenID = null.Int{}
} else {
i.TokenID = null.IntFrom(command.TokenID)
}
err := i.Insert(ctx, ch.DBS().Writer, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{Err: errors.Wrapf(err, "error inserting integration: %s", command.Vendor)}
}
return CreateIntegrationCommandResult{ID: i.ID}, nil
}