-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystems.go
124 lines (98 loc) · 3.28 KB
/
systems.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package controllers
import (
"context"
"github.com/google/uuid"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/api/ports"
)
// CreateSystemCommand ...
type CreateSystemCommand struct {
Name string `json:"name" validate:"required,min=3,max=255"`
Description string `json:"description" validate:"max=1024"`
OperatorID uuid.UUID `json:"operator_id" validate:"required"`
}
// DeleteSystemCommand ...
type DeleteSystemCommand struct {
ID uuid.UUID `json:"id" validate:"required"`
}
// GetSystemQuery ...
type GetSystemQuery struct {
ID uuid.UUID `json:"id" validate:"required"`
}
// ListSystemsQuery ...
type ListSystemsQuery struct {
Limit int `json:"limit" validate:"required"`
Offset int `json:"offset" validate:"required"`
Search string `json:"search"`
Sort string `json:"sort"`
}
var _ SystemsController = (*SystemsControllerImpl)(nil)
// SystemsController is the controller for systems.
type SystemsController interface {
// CreateSystem creates a new system.
CreateSystem(ctx context.Context, cmd CreateSystemCommand) (models.System, error)
// DeleteSystem deletes a system.
DeleteSystem(ctx context.Context, cmd DeleteSystemCommand) error
// GetSystem retrieves a system by its ID.
GetSystem(ctx context.Context, query GetSystemQuery) (models.System, error)
// ListSystems retrieves a list of systems.
ListSystems(ctx context.Context, query ListSystemsQuery) (models.Pagination[models.System], error)
}
// SystemsControllerImpl ...
type SystemsControllerImpl struct {
db ports.Systems
}
// NewSystemsController ...
func NewSystemsController(db ports.Systems) *SystemsControllerImpl {
return &SystemsControllerImpl{db}
}
// CreateSystem is the implementation of the CreateSystem method.
func (s *SystemsControllerImpl) CreateSystem(ctx context.Context, cmd CreateSystemCommand) (models.System, error) {
system := models.System{}
system.Name = cmd.Name
system.Description = cmd.Description
system.OperatorID = cmd.OperatorID
if err := s.db.CreateSystem(ctx, &system); err != nil {
return system, err
}
return system, nil
}
// DeleteSystem is deleting a system.
func (s *SystemsControllerImpl) DeleteSystem(ctx context.Context, cmd DeleteSystemCommand) error {
return s.db.DeleteSystem(ctx, cmd.ID)
}
// GetSystem ...
func (s *SystemsControllerImpl) GetSystem(ctx context.Context, query GetSystemQuery) (models.System, error) {
op := models.System{ID: query.ID}
err := s.db.GetSystem(ctx, &op)
if err != nil {
return op, err
}
return op, nil
}
// ListSystems ...
func (s *SystemsControllerImpl) ListSystems(ctx context.Context, query ListSystemsQuery) (models.Pagination[models.System], error) {
sys := models.Pagination[models.System]{}
sys.Limit = query.Limit
sys.Offset = query.Offset
sys.Search = query.Search
err := s.db.ListSystems(ctx, &sys)
if err != nil {
return sys, err
}
return sys, nil
}
// UpdateSystemOperator ...
func (s *SystemsControllerImpl) UpdateSystemOperator(ctx context.Context, systemId, operatorID uuid.UUID) (models.System, error) {
system := models.System{}
system.ID = systemId
err := s.db.GetSystem(ctx, &system)
if err != nil {
return system, err
}
system.OperatorID = operatorID
if err := s.db.UpdateSystem(ctx, &system); err != nil {
return system, err
}
return system, nil
}