/
get_in_by_id.go
87 lines (69 loc) · 2.44 KB
/
get_in_by_id.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 queries
import (
"context"
"database/sql"
"fmt"
"github.com/DIMO-Network/device-definitions-api/internal/core/mediator"
coremodels "github.com/DIMO-Network/device-definitions-api/internal/core/models"
"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"
)
type GetIntegrationByIDQuery struct {
IntegrationID []string `json:"integration_id" validate:"required"`
}
func (*GetIntegrationByIDQuery) Key() string { return "GetIntegrationByIDQuery" }
type GetIntegrationByIDQueryHandler struct {
DBS func() *db.ReaderWriter
}
func NewGetIntegrationByIDQueryHandler(dbs func() *db.ReaderWriter) GetIntegrationByIDQueryHandler {
return GetIntegrationByIDQueryHandler{DBS: dbs}
}
func (ch GetIntegrationByIDQueryHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
qry := query.(*GetIntegrationByIDQuery)
if len(qry.IntegrationID) == 0 {
return nil, &exceptions.ValidationError{
Err: errors.New("IntegrationID is required"),
}
}
v, err := models.Integrations(models.IntegrationWhere.ID.EQ(qry.IntegrationID[0])).One(ctx, ch.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("could not find integration id: %s", qry.IntegrationID),
}
}
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to get integration"),
}
}
result := coremodels.GetIntegrationQueryResult{}
im := new(coremodels.IntegrationsMetadata)
if v.Metadata.Valid {
err = v.Metadata.Unmarshal(&im)
if err != nil {
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to unmarshall integration metadata id %s", v.ID),
}
}
}
result = coremodels.GetIntegrationQueryResult{
ID: v.ID,
Type: v.Type,
Style: v.Style,
Vendor: v.Vendor,
AutoPiDefaultTemplateID: im.AutoPiDefaultTemplateID,
RefreshLimitSecs: v.RefreshLimitSecs,
TokenID: v.TokenID.Int,
Points: v.Points,
}
if !v.ManufacturerTokenID.IsZero() {
result.ManufacturerTokenID = v.ManufacturerTokenID.Int
}
if im.AutoPiPowertrainToTemplateID != nil {
result.AutoPiPowertrainToTemplateID = im.AutoPiPowertrainToTemplateID
}
return result, nil
}