mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-06 18:10:25 +08:00
所有数据库相关的操作支持事务
This commit is contained in:
@@ -36,24 +36,24 @@ func init() {
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *AdminDAO) EnableAdmin(id int64) (rowsAffected int64, err error) {
|
||||
return this.Query().
|
||||
func (this *AdminDAO) EnableAdmin(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", AdminStateEnabled).
|
||||
Update()
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *AdminDAO) DisableAdmin(id int64) (rowsAffected int64, err error) {
|
||||
return this.Query().
|
||||
func (this *AdminDAO) DisableAdmin(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", AdminStateDisabled).
|
||||
Update()
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *AdminDAO) FindEnabledAdmin(id int64) (*Admin, error) {
|
||||
result, err := this.Query().
|
||||
func (this *AdminDAO) FindEnabledAdmin(tx *dbs.Tx, id int64) (*Admin, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", AdminStateEnabled).
|
||||
Find()
|
||||
@@ -64,27 +64,27 @@ func (this *AdminDAO) FindEnabledAdmin(id int64) (*Admin, error) {
|
||||
}
|
||||
|
||||
// 检查管理员是否存在
|
||||
func (this *AdminDAO) ExistEnabledAdmin(adminId int64) (bool, error) {
|
||||
return this.Query().
|
||||
func (this *AdminDAO) ExistEnabledAdmin(tx *dbs.Tx, adminId int64) (bool, error) {
|
||||
return this.Query(tx).
|
||||
Pk(adminId).
|
||||
State(AdminStateEnabled).
|
||||
Exist()
|
||||
}
|
||||
|
||||
// 获取管理员名称
|
||||
func (this *AdminDAO) FindAdminFullname(adminId int64) (string, error) {
|
||||
return this.Query().
|
||||
func (this *AdminDAO) FindAdminFullname(tx *dbs.Tx, adminId int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(adminId).
|
||||
Result("fullname").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 检查用户名、密码
|
||||
func (this *AdminDAO) CheckAdminPassword(username string, encryptedPassword string) (int64, error) {
|
||||
func (this *AdminDAO) CheckAdminPassword(tx *dbs.Tx, username string, encryptedPassword string) (int64, error) {
|
||||
if len(username) == 0 || len(encryptedPassword) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return this.Query().
|
||||
return this.Query(tx).
|
||||
Attr("username", username).
|
||||
Attr("password", encryptedPassword).
|
||||
Attr("state", AdminStateEnabled).
|
||||
@@ -94,8 +94,8 @@ func (this *AdminDAO) CheckAdminPassword(username string, encryptedPassword stri
|
||||
}
|
||||
|
||||
// 根据用户名查询管理员ID
|
||||
func (this *AdminDAO) FindAdminIdWithUsername(username string) (int64, error) {
|
||||
one, err := this.Query().
|
||||
func (this *AdminDAO) FindAdminIdWithUsername(tx *dbs.Tx, username string) (int64, error) {
|
||||
one, err := this.Query(tx).
|
||||
Attr("username", username).
|
||||
State(AdminStateEnabled).
|
||||
ResultPk().
|
||||
@@ -110,19 +110,19 @@ func (this *AdminDAO) FindAdminIdWithUsername(username string) (int64, error) {
|
||||
}
|
||||
|
||||
// 更改管理员密码
|
||||
func (this *AdminDAO) UpdateAdminPassword(adminId int64, password string) error {
|
||||
func (this *AdminDAO) UpdateAdminPassword(tx *dbs.Tx, adminId int64, password string) error {
|
||||
if adminId <= 0 {
|
||||
return errors.New("invalid adminId")
|
||||
}
|
||||
op := NewAdminOperator()
|
||||
op.Id = adminId
|
||||
op.Password = stringutil.Md5(password)
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建管理员
|
||||
func (this *AdminDAO) CreateAdmin(username string, password string, fullname string, isSuper bool, modulesJSON []byte) (int64, error) {
|
||||
func (this *AdminDAO) CreateAdmin(tx *dbs.Tx, username string, password string, fullname string, isSuper bool, modulesJSON []byte) (int64, error) {
|
||||
op := NewAdminOperator()
|
||||
op.IsOn = true
|
||||
op.State = AdminStateEnabled
|
||||
@@ -135,7 +135,7 @@ func (this *AdminDAO) CreateAdmin(username string, password string, fullname str
|
||||
} else {
|
||||
op.Modules = "[]"
|
||||
}
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -143,19 +143,19 @@ func (this *AdminDAO) CreateAdmin(username string, password string, fullname str
|
||||
}
|
||||
|
||||
// 修改管理员个人资料
|
||||
func (this *AdminDAO) UpdateAdminInfo(adminId int64, fullname string) error {
|
||||
func (this *AdminDAO) UpdateAdminInfo(tx *dbs.Tx, adminId int64, fullname string) error {
|
||||
if adminId <= 0 {
|
||||
return errors.New("invalid adminId")
|
||||
}
|
||||
op := NewAdminOperator()
|
||||
op.Id = adminId
|
||||
op.Fullname = fullname
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改管理员详细信息
|
||||
func (this *AdminDAO) UpdateAdmin(adminId int64, username string, password string, fullname string, isSuper bool, modulesJSON []byte, isOn bool) error {
|
||||
func (this *AdminDAO) UpdateAdmin(tx *dbs.Tx, adminId int64, username string, password string, fullname string, isSuper bool, modulesJSON []byte, isOn bool) error {
|
||||
if adminId <= 0 {
|
||||
return errors.New("invalid adminId")
|
||||
}
|
||||
@@ -173,13 +173,13 @@ func (this *AdminDAO) UpdateAdmin(adminId int64, username string, password strin
|
||||
op.Modules = "[]"
|
||||
}
|
||||
op.IsOn = isOn
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查用户名是否存在
|
||||
func (this *AdminDAO) CheckAdminUsername(adminId int64, username string) (bool, error) {
|
||||
query := this.Query().
|
||||
func (this *AdminDAO) CheckAdminUsername(tx *dbs.Tx, adminId int64, username string) (bool, error) {
|
||||
query := this.Query(tx).
|
||||
State(AdminStateEnabled).
|
||||
Attr("username", username)
|
||||
if adminId > 0 {
|
||||
@@ -191,7 +191,7 @@ func (this *AdminDAO) CheckAdminUsername(adminId int64, username string) (bool,
|
||||
}
|
||||
|
||||
// 修改管理员登录信息
|
||||
func (this *AdminDAO) UpdateAdminLogin(adminId int64, username string, password string) error {
|
||||
func (this *AdminDAO) UpdateAdminLogin(tx *dbs.Tx, adminId int64, username string, password string) error {
|
||||
if adminId <= 0 {
|
||||
return errors.New("invalid adminId")
|
||||
}
|
||||
@@ -201,19 +201,19 @@ func (this *AdminDAO) UpdateAdminLogin(adminId int64, username string, password
|
||||
if len(password) > 0 {
|
||||
op.Password = stringutil.Md5(password)
|
||||
}
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改管理员可以管理的模块
|
||||
func (this *AdminDAO) UpdateAdminModules(adminId int64, allowModulesJSON []byte) error {
|
||||
func (this *AdminDAO) UpdateAdminModules(tx *dbs.Tx, adminId int64, allowModulesJSON []byte) error {
|
||||
if adminId <= 0 {
|
||||
return errors.New("invalid adminId")
|
||||
}
|
||||
op := NewAdminOperator()
|
||||
op.Id = adminId
|
||||
op.Modules = allowModulesJSON
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -221,8 +221,8 @@ func (this *AdminDAO) UpdateAdminModules(adminId int64, allowModulesJSON []byte)
|
||||
}
|
||||
|
||||
// 查询所有管理的权限
|
||||
func (this *AdminDAO) FindAllAdminModules() (result []*Admin, err error) {
|
||||
_, err = this.Query().
|
||||
func (this *AdminDAO) FindAllAdminModules(tx *dbs.Tx) (result []*Admin, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(AdminStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Result("id", "modules", "isSuper").
|
||||
@@ -232,15 +232,15 @@ func (this *AdminDAO) FindAllAdminModules() (result []*Admin, err error) {
|
||||
}
|
||||
|
||||
// 计算所有管理员数量
|
||||
func (this *AdminDAO) CountAllEnabledAdmins() (int64, error) {
|
||||
return this.Query().
|
||||
func (this *AdminDAO) CountAllEnabledAdmins(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(AdminStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// 列出单页的管理员
|
||||
func (this *AdminDAO) ListEnabledAdmins(offset int64, size int64) (result []*Admin, err error) {
|
||||
_, err = this.Query().
|
||||
func (this *AdminDAO) ListEnabledAdmins(tx *dbs.Tx, offset int64, size int64) (result []*Admin, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(AdminStateEnabled).
|
||||
Result("id", "isOn", "username", "fullname", "isSuper", "createdAt").
|
||||
Offset(offset).
|
||||
|
||||
Reference in New Issue
Block a user