Skip to content

Commit 4add1d7

Browse files
authored
[type:feat] refactor remove user function (#118)
* [type:feat] scopespace add user_id field and refactor remove user * [type:feat] refactor remove user
1 parent 69e0744 commit 4add1d7

File tree

7 files changed

+49
-33
lines changed

7 files changed

+49
-33
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require github.com/acmestack/godkits v0.0.10
66

77
require (
88
github.com/acmestack/gobatis v0.2.8
9+
github.com/acmestack/pagehelper v1.0.0
910
github.com/gin-gonic/gin v1.8.1
1011
github.com/go-sql-driver/mysql v1.6.0
1112
github.com/golang-jwt/jwt/v4 v4.4.2
@@ -17,7 +18,6 @@ require (
1718
github.com/Masterminds/goutils v1.1.1 // indirect
1819
github.com/Masterminds/semver/v3 v3.1.1 // indirect
1920
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
20-
github.com/acmestack/pagehelper v1.0.0 // indirect
2121
github.com/coreos/go-semver v0.3.0 // indirect
2222
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
2323
github.com/gin-contrib/sse v0.1.0 // indirect

internal/core/openapi/user.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/acmestack/envcd/internal/core/storage/dao"
25+
"github.com/acmestack/envcd/internal/pkg/constant"
2526
"github.com/acmestack/envcd/internal/pkg/entity"
2627
"github.com/acmestack/envcd/pkg/entity/result"
2728
"github.com/acmestack/godkits/array"
@@ -49,10 +50,6 @@ const (
4950
hmacSecret = "9C035514A15F78"
5051
userIdKey = "userId"
5152
tokenKey = "accessToken"
52-
53-
userStateEnabled = "enabled"
54-
userStateDisabled = "disabled"
55-
userStateDeleted = "deleted"
5653
)
5754

5855
type pageUserVO struct {
@@ -187,7 +184,7 @@ func (openapi *Openapi) createUser(ginCtx *gin.Context) {
187184
Password: password,
188185
Salt: salt,
189186
Identity: param.Identity,
190-
State: stringsx.DefaultIfEmpty(param.State, userStateEnabled),
187+
State: stringsx.DefaultIfEmpty(param.State, constant.EnabledState),
191188
CreatedAt: time.Now(),
192189
UpdatedAt: time.Now(),
193190
}
@@ -253,14 +250,34 @@ func (openapi *Openapi) removeUser(ginCtx *gin.Context) {
253250
}
254251
// update user state to deleted
255252
// todo tx and catch error @liuzhaowei
256-
user.State = userStateDeleted
253+
user.State = constant.DeletedState
257254
daoAction.UpdateUser(user)
258255

259-
// update user dictionary state to deleted
260-
daoAction.DeleteDictionaryByUserId(entity.Dictionary{UserId: user.Id})
256+
// get all the user's dictionary and update state to deleted
257+
dictParam := entity.Dictionary{UserId: user.Id}
258+
dictionaries, err := daoAction.SelectDictionary(dictParam, nil)
259+
if err != nil {
260+
return result.InternalFailure(err)
261+
}
262+
if len(dictionaries) != 0 {
263+
for i := range dictionaries {
264+
dictionaries[i].State = constant.DeletedState
265+
}
266+
daoAction.UpdateDictionaryBatch(dictionaries)
267+
}
261268

262-
// update user permission state to deleted
263-
daoAction.DeletePermissionByUserId(entity.Permission{UserId: user.Id})
269+
// get all the user's scopespace and update state to deleted
270+
spaceParam := entity.ScopeSpace{UserId: user.Id}
271+
spaces, err := daoAction.SelectScopeSpace(spaceParam)
272+
if err != nil {
273+
return result.InternalFailure(err)
274+
}
275+
if len(spaces) != 0 {
276+
for i := range spaces {
277+
spaces[i].State = constant.DeletedState
278+
}
279+
daoAction.UpdateScopeSpaceBatch(spaces)
280+
}
264281

265282
return result.Success(nil)
266283
})

internal/core/storage/dao/dictionary.go

-6
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ func (dao *Dao) DeleteDictionary(model entity.Dictionary) (int64, error) {
7979
return ret, err
8080
}
8181

82-
func (dao *Dao) DeleteDictionaryByUserId(model entity.Dictionary) (int64, error) {
83-
var ret int64
84-
err := dao.storage.NewSession().Delete("dao.deleteDictionaryByUserId").Param(model).Result(&ret)
85-
return ret, err
86-
}
87-
8882
func (dao *Dao) DeleteDictionaryBatch(model []entity.Dictionary) (int64, error) {
8983
var ret int64
9084
err := dao.storage.NewSession().Delete("dao.deleteDictionaryBatch").Param(model).Result(&ret)

internal/core/storage/dao/permission.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ func (dao *Dao) UpdatePermission(model entity.Permission) (int64, error) {
6060
return ret, err
6161
}
6262

63-
func (dao *Dao) DeletePermission(model entity.Permission) (int64, error) {
63+
func (dao *Dao) UpdatePermissionBatch(model []entity.Permission) (int64, error) {
6464
var ret int64
65-
err := dao.storage.NewSession().Delete("dao.deletePermission").Param(model).Result(&ret)
65+
err := dao.storage.NewSession().Update("dao.updatePermissionBatch").Param(model).Result(&ret)
6666
return ret, err
6767
}
6868

69-
func (dao *Dao) DeletePermissionByUserId(model entity.Permission) (int64, error) {
69+
func (dao *Dao) DeletePermission(model entity.Permission) (int64, error) {
7070
var ret int64
71-
err := dao.storage.NewSession().Delete("dao.deletePermissionByUserId").Param(model).Result(&ret)
71+
err := dao.storage.NewSession().Delete("dao.deletePermission").Param(model).Result(&ret)
7272
return ret, err
7373
}

internal/core/storage/xml/dictionary_mapper.xml

-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@
117117
</where>
118118
</delete>
119119

120-
<delete id="deleteDictionaryByUserId">
121-
UPDATE dictionary set state = "deleted"
122-
WHERE user_id = #{Dictionary.user_id}
123-
</delete>
124-
125120
<delete id="deleteDictionaryBatch">
126121
DELETE FROM dictionary
127122
WHERE id IN

internal/core/storage/xml/permission_mapper.xml

+16-6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@
8383
WHERE id = #{Permission.id}
8484
</update>
8585

86+
<update id="updatePermissionBatch">
87+
UPDATE permission
88+
SET state =
89+
<foreach collection="{0}" index="index" item="item" separator=" " open="CASE id " close=" END,">
90+
WHEN #{item.Permission.id} THEN #{item.Permission.state}
91+
</foreach>
92+
updated_at =
93+
<foreach collection="{0}" index="index" item="item" separator=" " open="CASE id " close=" END ">
94+
WHEN #{item.Permission.id} THEN #{item.Permission.updated_at}
95+
</foreach>
96+
WHERE id IN
97+
<foreach collection="{0}" index="index" item="item" open="(" close=")" separator=",">
98+
#{item.Permission.id}
99+
</foreach>
100+
</update>
101+
86102
<delete id="deletePermission">
87103
DELETE FROM permission
88104
<where>
@@ -96,10 +112,4 @@
96112
<if test="{Permission.updated_at} != nil">AND updated_at = #{Permission.updated_at} </if>
97113
</where>
98114
</delete>
99-
100-
101-
<delete id="deletePermissionByUserId">
102-
UPDATE permission set state = "deleted"
103-
WHERE user_id = #{Permission.user_id}
104-
</delete>
105115
</mapper>

internal/core/storage/xml/scopespace_mapper.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
-->
1818

1919
<mapper namespace="dao">
20-
<sql id="columns_id">id,name,note,state,created_at,updated_at</sql>
20+
<sql id="columns_id">id,user_id,name,note,state,created_at,updated_at</sql>
2121

2222
<select id="selectScopeSpace">
2323
SELECT <include refid="columns_id"> </include> FROM scopespace

0 commit comments

Comments
 (0)