/
get_ds_by_dd_id.go
66 lines (52 loc) · 1.97 KB
/
get_ds_by_dd_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
//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 GetDeviceStyleByDeviceDefinitionIDQuery struct {
DeviceDefinitionID string `json:"device_definition_id"`
}
func (*GetDeviceStyleByDeviceDefinitionIDQuery) Key() string {
return "GetDeviceStyleByDeviceDefinitionIDQuery"
}
type GetDeviceStyleByDeviceDefinitionIDQueryHandler struct {
DBS func() *db.ReaderWriter
}
func NewGetDeviceStyleByDeviceDefinitionIDQueryHandler(dbs func() *db.ReaderWriter) GetDeviceStyleByDeviceDefinitionIDQueryHandler {
return GetDeviceStyleByDeviceDefinitionIDQueryHandler{DBS: dbs}
}
func (ch GetDeviceStyleByDeviceDefinitionIDQueryHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
qry := query.(*GetDeviceStyleByDeviceDefinitionIDQuery)
styles, err := models.DeviceStyles(models.DeviceStyleWhere.DeviceDefinitionID.EQ(qry.DeviceDefinitionID)).All(ctx, ch.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to get device styles"),
}
}
}
response := []coremodels.GetDeviceStyleQueryResult{}
for _, v := range styles {
deviceStyle := coremodels.GetDeviceStyleQueryResult{
ID: v.ID,
DeviceDefinitionID: v.DeviceDefinitionID,
Name: v.Name,
ExternalStyleID: v.ExternalStyleID,
Source: v.Source,
SubModel: v.SubModel,
}
if v.HardwareTemplateID.Valid {
deviceStyle.HardwareTemplateID = v.HardwareTemplateID.String
}
response = append(response, deviceStyle)
}
return response, nil
}