-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmddel.go
73 lines (60 loc) · 1.85 KB
/
cmddel.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
package redmos
import (
"fmt"
"github.com/redis/go-redis/v9"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// Del 刪除行為, 以索引字串與資料到主要/次要資料庫中刪除資料, 使用上有以下幾點須注意
// - 執行前設定好 MajorEnable, MinorEnable
// - 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
// - 執行前設定好 Key 並且不能為空字串
type Del struct {
Behave // 行為物件
MajorEnable bool // 啟用主要資料庫
MinorEnable bool // 啟用次要資料庫
Meta Metaer // 元資料
Key string // 索引值
cmd *redis.IntCmd // 命令結果
}
// Prepare 前置處理
func (this *Del) Prepare() error {
if this.Meta == nil {
return fmt.Errorf("del prepare: meta nil")
} // if
if this.Key == "" {
return fmt.Errorf("del prepare: key empty")
} // if
if this.MajorEnable {
key := this.Meta.MajorKey(this.Key)
this.cmd = this.Major().Del(this.Ctx(), key)
} // if
if this.MinorEnable {
if this.Meta.MinorTable() == "" {
return fmt.Errorf("del prepare: table empty")
} // if
if this.Meta.MinorField() == "" {
return fmt.Errorf("del prepare: field empty")
} // if
} // if
return nil
}
// Complete 完成處理
func (this *Del) Complete() error {
if this.Meta == nil {
return fmt.Errorf("del complete: meta nil")
} // if
if this.MajorEnable {
if _, err := this.cmd.Result(); err != nil {
return fmt.Errorf("del complete: %w: %v", err, this.Key)
} // if
} // if
if this.MinorEnable {
table := this.Meta.MinorTable()
field := this.Meta.MinorField()
key := this.Meta.MinorKey(this.Key)
filter := bson.D{{Key: field, Value: key}}
this.Minor().Operate(table, mongo.NewDeleteOneModel().SetFilter(filter))
} // if
return nil
}