/
get_dd_by_id_with_rels.go
69 lines (56 loc) · 2.17 KB
/
get_dd_by_id_with_rels.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
package queries
import (
"context"
"encoding/json"
"fmt"
"github.com/DIMO-Network/device-definitions-api/internal/core/common"
"github.com/DIMO-Network/device-definitions-api/internal/core/mediator"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/repositories"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/exceptions"
)
type GetDeviceDefinitionWithRelsQuery struct {
DeviceDefinitionID string `json:"deviceDefinitionId" validate:"required"`
}
type GetDeviceDefinitionWithRelsQueryResult struct {
ID string `json:"id"`
Type string `json:"type"`
Style string `json:"style"`
Vendor string `json:"vendor"`
Region string `json:"region"`
Capabilities json.RawMessage `json:"capabilities"`
}
func (*GetDeviceDefinitionWithRelsQuery) Key() string {
return "GetDeviceDefinitionWithRelsQuery"
}
type GetDeviceDefinitionWithRelsQueryHandler struct {
Repository repositories.DeviceDefinitionRepository
}
func NewGetDeviceDefinitionWithRelsQueryHandler(repository repositories.DeviceDefinitionRepository) GetDeviceDefinitionWithRelsQueryHandler {
return GetDeviceDefinitionWithRelsQueryHandler{
Repository: repository,
}
}
func (ch GetDeviceDefinitionWithRelsQueryHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
qry := query.(*GetDeviceDefinitionWithRelsQuery)
dd, _ := ch.Repository.GetWithIntegrations(ctx, qry.DeviceDefinitionID)
if dd == nil {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("could not find device definition id: %s", qry.DeviceDefinitionID),
}
}
// build object for integrations that have all the info
var integrations []GetDeviceDefinitionWithRelsQueryResult
if dd.R != nil {
for _, di := range dd.R.DeviceIntegrations {
integrations = append(integrations, GetDeviceDefinitionWithRelsQueryResult{
ID: di.R.Integration.ID,
Type: di.R.Integration.Type,
Style: di.R.Integration.Style,
Vendor: di.R.Integration.Vendor,
Region: di.Region,
Capabilities: common.JSONOrDefault(di.Features),
})
}
}
return integrations, nil
}