Skip to content

Commit

Permalink
Fix/usermodel (#37)
Browse files Browse the repository at this point in the history
* fix(model):role create
  • Loading branch information
chanoe authored Feb 18, 2022
1 parent 46f4306 commit e9889c6
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions model/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package model

import (
"github.com/tkeel-io/security/utils"
"time"

"gorm.io/gorm"
Expand All @@ -32,9 +33,21 @@ func (Role) TableName() string {
return "sys_t_role"
}

func (dao *Role) BeforeCreate(_ *gorm.DB) error {
if dao.ID == "" {
roleID, err := GenRoleID()
if err != nil {
return err
}
dao.ID = roleID
}
return nil
}

func (dao *Role) Create(db *gorm.DB) error {
return db.Create(dao).Error
}

func (dao *Role) IsExisted(db *gorm.DB, where map[string]interface{}) (bool, error) {
roles := []Role{}
err := db.Where(where).Find(&roles).Error
Expand Down Expand Up @@ -67,3 +80,7 @@ func (dao *Role) Delete(db *gorm.DB) (affected int64, err error) {
result := db.Delete(dao)
return result.RowsAffected, result.Error
}

func GenRoleID() (string, error) {
return utils.RandStringWithPrefix("role", 12)
}

0 comments on commit e9889c6

Please sign in to comment.