/
account_tx.go
91 lines (86 loc) · 2.97 KB
/
account_tx.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
package tx
import (
"context"
"errors"
db "github.com/0RAJA/chat_app/src/dao/postgres/sqlc"
"github.com/0RAJA/chat_app/src/dao/redis/query"
"github.com/0RAJA/chat_app/src/pkg/tool"
)
var (
ErrAccountOverNum = errors.New("账户数量超过限制")
ErrAccountNameExists = errors.New("账户名已存在")
ErrAccountGroupLeader = errors.New("账户是群主")
)
// CreateAccountWithTx 检查数量,账户名后创建账户并建立和自己的关系
func (store *SqlStore) CreateAccountWithTx(c context.Context, rdb *query.Queries, maxAccountNum int32, arg *db.CreateAccountParams) error {
return store.execTx(c, func(queries *db.Queries) error {
var err error
var accountNum int32
// 检查数量
err = tool.DoThat(err, func() error {
accountNum, err = queries.CountAccountByUserID(c, arg.UserID)
return err
})
if accountNum >= maxAccountNum {
return ErrAccountOverNum
}
// 检查账户名
var exists bool
err = tool.DoThat(err, func() error {
exists, err = queries.ExistsAccountByNameAndUserID(c, &db.ExistsAccountByNameAndUserIDParams{UserID: arg.UserID, Name: arg.Name})
return err
})
if exists {
return ErrAccountNameExists
}
err = tool.DoThat(err, func() error { return queries.CreateAccount(c, arg) })
var relationID int64
err = tool.DoThat(err, func() error {
relationID, err = queries.CreateFriendRelation(c, &db.CreateFriendRelationParams{Account1ID: arg.ID, Account2ID: arg.ID})
return err
})
err = tool.DoThat(err, func() error {
return queries.CreateSetting(c, &db.CreateSettingParams{
AccountID: arg.ID,
RelationID: relationID,
IsSelf: true,
})
})
// 添加自己一个人的关系到redis
err = tool.DoThat(err, func() error { return rdb.AddRelationAccount(c, relationID, arg.ID) })
return err
})
}
// DeleteAccountWithTx 删除账户并删除与之相关的关系
func (store *SqlStore) DeleteAccountWithTx(c context.Context, rdb *query.Queries, accountID int64) error {
return store.execTx(c, func(queries *db.Queries) error {
var err error
// 判断是否是群主
var isLeader bool
err = tool.DoThat(err, func() error {
isLeader, err = queries.ExistsGroupLeaderByAccountIDWithLock(c, accountID)
return err
})
if isLeader {
return ErrAccountGroupLeader
}
// 删除好友
var friendRelationIDs []int64
err = tool.DoThat(err, func() error {
friendRelationIDs, err = queries.DeleteFriendRelationsByAccountID(c, accountID)
return err
})
// 删除群
var groupRelationIDs []int64
err = tool.DoThat(err, func() error {
groupRelationIDs, err = queries.DeleteSettingsByAccountID(c, accountID)
return err
})
// 删除账户
err = tool.DoThat(err, func() error { return queries.DeleteAccount(c, accountID) })
// 从redis中删除对应的关系
err = tool.DoThat(err, func() error { return rdb.DelRelations(c, friendRelationIDs...) })
err = tool.DoThat(err, func() error { return rdb.DelAccountFromRelations(c, accountID, groupRelationIDs...) })
return err
})
}