-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_di.go
89 lines (70 loc) · 3.28 KB
/
create_di.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
//nolint:tagliatelle
package commands
import (
"context"
"database/sql"
"fmt"
"github.com/DIMO-Network/device-definitions-api/internal/core/services"
"github.com/pkg/errors"
"github.com/DIMO-Network/device-definitions-api/internal/core/common"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/exceptions"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/device-definitions-api/internal/core/mediator"
coremodels "github.com/DIMO-Network/device-definitions-api/internal/core/models"
repoModel "github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/models"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/repositories"
)
type CreateDeviceIntegrationCommand struct {
DeviceDefinitionID string `json:"device_definition_id"`
IntegrationID string `json:"integration_id"`
Region string `json:"region"`
Features []*coremodels.UpdateDeviceIntegrationFeatureAttribute `json:"features"`
}
type CreateDeviceIntegrationCommandResult struct {
ID string `json:"id"`
}
func (*CreateDeviceIntegrationCommand) Key() string { return "CreateDeviceIntegrationCommand" }
type CreateDeviceIntegrationCommandHandler struct {
Repository repositories.DeviceIntegrationRepository
DBS func() *db.ReaderWriter
DDCache services.DeviceDefinitionCacheService
DeviceDefinitionRepository repositories.DeviceDefinitionRepository
}
func NewCreateDeviceIntegrationCommandHandler(repository repositories.DeviceIntegrationRepository,
dbs func() *db.ReaderWriter,
cache services.DeviceDefinitionCacheService,
deviceDefinitionRepository repositories.DeviceDefinitionRepository) CreateDeviceIntegrationCommandHandler {
return CreateDeviceIntegrationCommandHandler{Repository: repository, DBS: dbs, DDCache: cache, DeviceDefinitionRepository: deviceDefinitionRepository}
}
func (ch CreateDeviceIntegrationCommandHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
command := query.(*CreateDeviceIntegrationCommand)
features, err := repoModel.IntegrationFeatures().All(ctx, ch.DBS().Reader)
if err != nil {
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to get integration features"),
}
}
integrationFeaturesValues, err := common.BuildDeviceIntegrationFeatureAttribute(command.Features, features)
if err != nil {
return nil, &exceptions.ValidationError{
Err: err,
}
}
di, err := ch.Repository.Create(ctx, command.DeviceDefinitionID, command.IntegrationID, command.Region, integrationFeaturesValues)
if err != nil {
return nil, err
}
dd, err := ch.DeviceDefinitionRepository.GetByID(ctx, command.DeviceDefinitionID)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.InternalError{
Err: err,
}
}
}
// Remove Cache
ch.DDCache.DeleteDeviceDefinitionCacheByID(ctx, dd.ID)
ch.DDCache.DeleteDeviceDefinitionCacheByMakeModelAndYears(ctx, dd.R.DeviceMake.Name, dd.Model, int(dd.Year))
ch.DDCache.DeleteDeviceDefinitionCacheBySlug(ctx, dd.ModelSlug, int(dd.Year))
return CreateDeviceIntegrationCommandResult{ID: di.IntegrationID}, nil
}