-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete_integration_feature.go
60 lines (46 loc) · 1.63 KB
/
delete_integration_feature.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
package commands
import (
"context"
"database/sql"
"fmt"
"github.com/DIMO-Network/device-definitions-api/internal/core/mediator"
"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 DeleteIntegrationFeatureCommand struct {
ID string `json:"id"`
}
type DeleteIntegrationFeatureResult struct {
ID string `json:"id"`
}
func (*DeleteIntegrationFeatureCommand) Key() string { return "DeleteIntegrationFeatureCommand" }
type DeleteIntegrationFeatureCommandHandler struct {
DBS func() *db.ReaderWriter
}
func NewDeleteIntegrationFeatureCommandHandler(dbs func() *db.ReaderWriter) DeleteIntegrationFeatureCommandHandler {
return DeleteIntegrationFeatureCommandHandler{DBS: dbs}
}
func (ch DeleteIntegrationFeatureCommandHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
command := query.(*DeleteIntegrationFeatureCommand)
feature, err := models.IntegrationFeatures(models.IntegrationFeatureWhere.FeatureKey.EQ(command.ID)).One(ctx, ch.DBS().Reader)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{
Err: err,
}
}
if errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("could not find integration feature id: %s", command.ID),
}
}
}
if _, err := feature.Delete(ctx, ch.DBS().Writer.DB); err != nil {
return nil, &exceptions.InternalError{
Err: err,
}
}
return DeleteIntegrationFeatureCommand{ID: feature.FeatureKey}, nil
}