-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_dbc_file_by_template_name.go
50 lines (41 loc) · 1.37 KB
/
get_dbc_file_by_template_name.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
package queries
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
grpc "github.com/DIMO-Network/vehicle-signal-decoding/pkg/grpc"
"github.com/rs/zerolog"
)
type GetDbcByTemplateNameQueryHandler struct {
DBS func() *db.ReaderWriter
logger *zerolog.Logger
}
func NewGetDbcByTemplateNameQueryHandler(dbs func() *db.ReaderWriter, logger *zerolog.Logger) GetDbcByTemplateNameQueryHandler {
return GetDbcByTemplateNameQueryHandler{
DBS: dbs,
logger: logger,
}
}
type GetDbcByTemplateNameQueryRequest struct {
TemplateName string
}
func (h GetDbcByTemplateNameQueryHandler) Handle(ctx context.Context, query *GetDbcByTemplateNameQueryRequest) (*grpc.GetDbcByTemplateNameResponse, error) {
item, err := models.DBCFiles(models.DBCFileWhere.TemplateName.EQ(query.TemplateName)).One(ctx, h.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
h.logger.Info().Msgf("failed to get DBC File by template name: %s", err)
return &grpc.GetDbcByTemplateNameResponse{Dbc: &grpc.DbcConfig{}}, nil
}
return nil, fmt.Errorf("failed to get DBC File by template name: %s", err)
}
result := &grpc.GetDbcByTemplateNameResponse{
Dbc: &grpc.DbcConfig{
TemplateName: item.TemplateName,
DbcFile: item.DBCFile,
},
}
return result, nil
}