-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_dm_by_token_id.go
92 lines (74 loc) · 2.8 KB
/
get_dm_by_token_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
88
89
90
91
92
package queries
import (
"context"
"database/sql"
"fmt"
"math/big"
"strings"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/DIMO-Network/device-definitions-api/internal/core/common"
"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"
p_grpc "github.com/DIMO-Network/device-definitions-api/pkg/grpc"
"github.com/DIMO-Network/shared/db"
"github.com/ericlagergren/decimal"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"github.com/volatiletech/sqlboiler/v4/types"
)
type GetDeviceMakeByTokenIDQuery struct {
TokenID string `json:"tokenId"`
}
func (*GetDeviceMakeByTokenIDQuery) Key() string { return "GetDeviceMakeByTokenIDQuery" }
type GetDeviceMakeByTokenIDQueryHandler struct {
DBS func() *db.ReaderWriter
}
func NewGetDeviceMakeByTokenIDQueryHandler(dbs func() *db.ReaderWriter) GetDeviceMakeByTokenIDQueryHandler {
return GetDeviceMakeByTokenIDQueryHandler{DBS: dbs}
}
func (ch GetDeviceMakeByTokenIDQueryHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
qry := query.(*GetDeviceMakeByTokenIDQuery)
qry.TokenID = strings.TrimSpace(qry.TokenID)
ti, ok := new(big.Int).SetString(qry.TokenID, 10)
if !ok {
return nil, &exceptions.ValidationError{
Err: fmt.Errorf("couldn't parse token id"),
}
}
tid := types.NewNullDecimal(new(decimal.Big).SetBigMantScale(ti, 0))
v, err := models.DeviceMakes(qm.Where("token_id = ?", qry.TokenID)).One(ctx, ch.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("could not find device make with tokenId param: %s, and bigint tid %v", qry.TokenID, tid),
}
}
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to get device make by token id"),
}
}
eids := common.BuildExternalIDs(v.ExternalIds)
md := &coremodels.DeviceMakeMetadata{}
_ = v.Metadata.Unmarshal(md)
result := &p_grpc.DeviceMake{
Id: v.ID,
Name: v.Name,
LogoUrl: v.LogoURL.String,
OemPlatformName: v.OemPlatformName.String,
NameSlug: v.NameSlug,
ExternalIds: string(v.ExternalIds.JSON),
ExternalIdsTyped: common.ExternalIDsToGRPC(eids),
Metadata: common.DeviceMakeMetadataToGRPC(md),
CreatedAt: timestamppb.New(v.CreatedAt),
UpdatedAt: timestamppb.New(v.UpdatedAt),
}
if !v.TokenID.IsZero() {
result.TokenId = v.TokenID.Big.Int(new(big.Int)).Uint64()
}
if v.HardwareTemplateID.Valid {
result.HardwareTemplateId = v.HardwareTemplateID.String
}
return result, nil
}