-
Notifications
You must be signed in to change notification settings - Fork 462
/
ClusterNoteService.go
147 lines (133 loc) · 4.69 KB
/
ClusterNoteService.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cluster
import (
"fmt"
"time"
"github.com/devtron-labs/devtron/internal/constants"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/cluster/repository"
"github.com/go-pg/pg"
"go.uber.org/zap"
)
type ClusterNoteBean struct {
Id int `json:"id" validate:"number"`
ClusterId int `json:"clusterId" validate:"required"`
Description string `json:"description" validate:"required"`
UpdatedBy int32 `json:"updatedBy"`
UpdatedOn time.Time `json:"updatedOn"`
}
type ClusterNoteResponseBean struct {
Id int `json:"id" validate:"number"`
Description string `json:"description" validate:"required"`
UpdatedBy string `json:"updatedBy"`
UpdatedOn time.Time `json:"updatedOn"`
}
type ClusterNoteService interface {
Save(bean *ClusterNoteBean, userId int32) (*ClusterNoteBean, error)
Update(bean *ClusterNoteBean, userId int32) (*ClusterNoteBean, error)
}
type ClusterNoteServiceImpl struct {
clusterNoteRepository repository.ClusterNoteRepository
clusterNoteHistoryService ClusterNoteHistoryService
logger *zap.SugaredLogger
}
func NewClusterNoteServiceImpl(repository repository.ClusterNoteRepository, clusterNoteHistoryService ClusterNoteHistoryService, logger *zap.SugaredLogger) *ClusterNoteServiceImpl {
clusterNoteService := &ClusterNoteServiceImpl{
clusterNoteRepository: repository,
clusterNoteHistoryService: clusterNoteHistoryService,
logger: logger,
}
return clusterNoteService
}
func (impl *ClusterNoteServiceImpl) Save(bean *ClusterNoteBean, userId int32) (*ClusterNoteBean, error) {
existingModel, err := impl.clusterNoteRepository.FindByClusterId(bean.ClusterId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Error(err)
return nil, err
}
if existingModel.Id > 0 {
impl.logger.Errorw("error on fetching cluster, duplicate", "id", bean.ClusterId)
return nil, fmt.Errorf("cluster note already exists")
}
model := &repository.ClusterNote{
ClusterId: bean.ClusterId,
Description: bean.Description,
}
model.CreatedBy = userId
model.UpdatedBy = userId
model.CreatedOn = time.Now()
model.UpdatedOn = time.Now()
err = impl.clusterNoteRepository.Save(model)
if err != nil {
impl.logger.Errorw("error in saving cluster note in db", "err", err)
err = &util.ApiError{
Code: constants.ClusterCreateDBFailed,
InternalMessage: "cluster note creation failed in db",
UserMessage: fmt.Sprintf("requested by %d", userId),
}
return nil, err
}
bean.Id = model.Id
bean.UpdatedBy = model.UpdatedBy
bean.UpdatedOn = model.UpdatedOn
// audit the existing description to cluster audit history
clusterAudit := &ClusterNoteHistoryBean{
NoteId: bean.Id,
Description: bean.Description,
CreatedOn: model.CreatedOn,
CreatedBy: model.CreatedBy,
}
_, _ = impl.clusterNoteHistoryService.Save(clusterAudit, userId)
return bean, err
}
func (impl *ClusterNoteServiceImpl) Update(bean *ClusterNoteBean, userId int32) (*ClusterNoteBean, error) {
model, err := impl.clusterNoteRepository.FindByClusterId(bean.ClusterId)
if err != nil {
impl.logger.Error(err)
return nil, err
}
if model.Id == 0 {
impl.logger.Errorw("error on fetching cluster note, not found", "id", bean.Id)
return nil, fmt.Errorf("cluster note not found")
}
// update the cluster description with new data
model.Description = bean.Description
model.UpdatedBy = userId
model.UpdatedOn = time.Now()
err = impl.clusterNoteRepository.Update(model)
if err != nil {
err = &util.ApiError{
Code: constants.ClusterUpdateDBFailed,
InternalMessage: "cluster note update failed in db",
UserMessage: fmt.Sprintf("requested by %d", userId),
}
return nil, err
}
bean.Id = model.Id
bean.UpdatedBy = model.UpdatedBy
bean.UpdatedOn = model.UpdatedOn
// audit the existing description to cluster audit history
clusterAudit := &ClusterNoteHistoryBean{
NoteId: bean.Id,
Description: bean.Description,
CreatedOn: model.CreatedOn,
CreatedBy: model.CreatedBy,
}
_, _ = impl.clusterNoteHistoryService.Save(clusterAudit, userId)
return bean, err
}