/
condition-checkpoint.go
155 lines (119 loc) · 4.18 KB
/
condition-checkpoint.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
148
149
150
151
152
153
154
155
package repo
import (
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
"github.com/aaronchen2k/deeptest/internal/pkg/domain"
checkpointHelpper "github.com/aaronchen2k/deeptest/internal/pkg/helper/checkpoint"
model "github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/jinzhu/copier"
"gorm.io/gorm"
)
type CheckpointRepo struct {
*BaseRepo `inject:""`
DB *gorm.DB `inject:""`
}
func (r *CheckpointRepo) Get(tenantId consts.TenantId, id uint) (checkpoint model.DebugConditionCheckpoint, err error) {
err = r.GetDB(tenantId).
Where("id=?", id).
Where("NOT deleted").
First(&checkpoint).Error
return
}
func (r *CheckpointRepo) GetByName(tenantId consts.TenantId, name string, interfaceId uint) (checkpoint model.DebugConditionCheckpoint, err error) {
var checkpoints []model.DebugConditionCheckpoint
db := r.GetDB(tenantId).Model(&checkpoint).
Where("name = ? AND endpoint_interface_id =? AND not deleted", name, interfaceId)
err = db.Find(&checkpoints).Error
if err != nil {
return
}
if len(checkpoints) > 0 {
checkpoint = checkpoints[0]
}
return
}
func (r *CheckpointRepo) Save(tenantId consts.TenantId, checkpoint *model.DebugConditionCheckpoint) (err error) {
r.UpdateDesc(tenantId, checkpoint)
err = r.GetDB(tenantId).Save(checkpoint).Error
return
}
func (r *CheckpointRepo) UpdateDesc(tenantId consts.TenantId, po *model.DebugConditionCheckpoint) (err error) {
desc := checkpointHelpper.GenDesc(po.Type, po.Operator, po.Value, po.Expression,
po.ExtractorVariable, po.ExtractorType, po.ExtractorExpression)
values := map[string]interface{}{
"desc": desc,
}
err = r.GetDB(tenantId).Model(&model.DebugCondition{}).
Where("id=?", po.ConditionId).
Updates(values).Error
return
}
func (r *CheckpointRepo) Delete(tenantId consts.TenantId, id uint) (err error) {
err = r.GetDB(tenantId).Model(&model.DebugConditionCheckpoint{}).
Where("id=?", id).
Update("deleted", true).
Error
return
}
func (r *CheckpointRepo) DeleteByCondition(tenantId consts.TenantId, conditionId uint) (err error) {
err = r.GetDB(tenantId).Model(&model.DebugConditionCheckpoint{}).
Where("condition_id=?", conditionId).
Update("deleted", true).
Error
return
}
func (r *CheckpointRepo) UpdateResult(tenantId consts.TenantId, checkpoint domain.CheckpointBase) (err error) {
values := map[string]interface{}{
"actual_result": checkpoint.ActualResult,
"result_status": checkpoint.ResultStatus,
}
err = r.GetDB(tenantId).Model(&model.DebugConditionCheckpoint{}).
Where("id=?", checkpoint.ConditionEntityId).
Updates(values).
Error
return
}
func (r *CheckpointRepo) CreateLog(tenantId consts.TenantId, checkpoint domain.CheckpointBase) (
log model.ExecLogCheckpoint, err error) {
copier.CopyWithOption(&log, checkpoint, copier.Option{DeepCopy: true})
log.ID = 0
log.ConditionId = checkpoint.ConditionId
log.ConditionEntityId = checkpoint.ConditionEntityId
log.InvokeId = checkpoint.InvokeId
log.CreatedAt = nil
log.UpdatedAt = nil
err = r.GetDB(tenantId).Save(&log).Error
return
}
func (r *CheckpointRepo) CreateDefault(tenantId consts.TenantId, conditionId uint) (po model.DebugConditionCheckpoint) {
po.ConditionId = conditionId
po = model.DebugConditionCheckpoint{
CheckpointBase: domain.CheckpointBase{
ConditionId: conditionId,
Type: consts.ResponseStatus,
Operator: consts.Equal,
Expression: "",
ExtractorVariable: "",
Value: "200",
},
}
r.Save(tenantId, &po)
return
}
func (r *CheckpointRepo) GetLog(tenantId consts.TenantId, conditionId, invokeId uint) (ret model.ExecLogCheckpoint, err error) {
err = r.GetDB(tenantId).
Where("condition_id=? AND invoke_id=?", conditionId, invokeId).
Where("NOT deleted").
First(&ret).Error
ret.ConditionEntityType = consts.ConditionTypeCheckpoint
return
}
func (r *CheckpointRepo) GetLogFromScriptAssert(tenantId consts.TenantId, conditionId, invokeId uint) (ret []model.ExecLogCheckpoint, err error) {
err = r.GetDB(tenantId).
Where("condition_id=? AND invoke_id=?", conditionId, invokeId).
Where("NOT deleted").
Find(&ret).Error
for index, _ := range ret {
ret[index].ConditionEntityType = consts.ConditionTypeCheckpoint
}
return
}