-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_dbc_file.go
59 lines (47 loc) · 1.56 KB
/
create_dbc_file.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
package commands
import (
"context"
"fmt"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/exceptions"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/DIMO-Network/shared/db"
)
type CreateDbcCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewCreateDbcCommandHandler(dbs func() *db.ReaderWriter) CreateDbcCommandHandler {
return CreateDbcCommandHandler{DBS: dbs}
}
type CreateDbcCommandRequest struct {
TemplateName string
DbcFile string
}
type CreateDbcCommandResponse struct {
Name string
}
func (h CreateDbcCommandHandler) Execute(ctx context.Context, req *CreateDbcCommandRequest) (*CreateDbcCommandResponse, error) {
exists, err := models.DBCFiles(models.DBCFileWhere.TemplateName.EQ(req.TemplateName)).Exists(ctx, h.DBS().Reader)
if err != nil {
return nil, &exceptions.InternalError{
Err: errors.Wrapf(err, "error checking if DbcConfig exists: %s", req.TemplateName),
}
}
if exists {
return nil, &exceptions.ConflictError{
Err: fmt.Errorf("DbcConfig with name %s already exists", req.TemplateName),
}
}
dbcConfig := &models.DBCFile{
TemplateName: req.TemplateName,
DBCFile: req.DbcFile,
}
err = dbcConfig.Insert(ctx, h.DBS().Writer, boil.Infer())
if err != nil {
return nil, &exceptions.InternalError{
Err: errors.Wrapf(err, "error inserting DbcConfig with template name: %s", req.TemplateName),
}
}
return &CreateDbcCommandResponse{Name: dbcConfig.TemplateName}, nil
}