-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_template.go
65 lines (54 loc) · 1.75 KB
/
update_template.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
package commands
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/exceptions"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
)
type UpdateTemplateCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewUpdateTemplateCommandHandler(dbs func() *db.ReaderWriter) UpdateTemplateCommandHandler {
return UpdateTemplateCommandHandler{DBS: dbs}
}
type UpdateTemplateCommandRequest struct {
Name string
ParentTemplateName string
Version string
Protocol string
Powertrain string
DBC string
TemplateVehicles []string
}
type UpdateTemplateCommandResponse struct {
Name string
}
func (h UpdateTemplateCommandHandler) Execute(ctx context.Context, req *UpdateTemplateCommandRequest) (*UpdateTemplateCommandResponse, error) {
template, err := models.Templates(models.TemplateWhere.TemplateName.EQ(req.Name)).One(ctx, h.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("template not found name: %s", req.Name),
}
}
return nil, &exceptions.InternalError{
Err: err,
}
}
template.TemplateName = req.Name
template.ParentTemplateName = null.StringFrom(req.ParentTemplateName)
template.Version = req.Version
template.Protocol = req.Protocol
template.Powertrain = req.Powertrain
if _, err := template.Update(ctx, h.DBS().Writer.DB, boil.Infer()); err != nil {
return nil, &exceptions.InternalError{
Err: err,
}
}
return &UpdateTemplateCommandResponse{Name: template.TemplateName}, nil
}