Skip to content

Commit

Permalink
add db invalid check
Browse files Browse the repository at this point in the history
  • Loading branch information
snowlyg committed Mar 30, 2023
1 parent 38bd0c8 commit b222b60
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 16 deletions.
6 changes: 6 additions & 0 deletions gin/admin/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func FindByUserName(scopes ...func(db *gorm.DB) *gorm.DB) (*Response, error) {
}

func FindPasswordByUserName(db *gorm.DB, username string, scopes ...func(db *gorm.DB) *gorm.DB) (*LoginResponse, error) {
if db == nil {
return nil, gorm.ErrInvalidDB
}
admin := &LoginResponse{}
db = db.Model(&Admin{}).Select("id,password").Where("username = ?", username)

Expand Down Expand Up @@ -169,6 +172,9 @@ func AddRoleForUser(admin *Admin) error {
}

func UpdateAvatar(db *gorm.DB, id uint, avatar string) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(&Admin{}).Where("id = ?", id).Update("header_img", avatar).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand Down
9 changes: 9 additions & 0 deletions gin/admin/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Avatar struct {

// Create 添加
func (item *Admin) Create(db *gorm.DB) (uint, error) {
if db == nil {
return 0, gorm.ErrInvalidDB
}
err := db.Model(item).Create(item).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -53,6 +56,9 @@ func (item *Admin) mc() map[string]interface{} {

// Update 更新
func (item *Admin) Update(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(item).Scopes(scopes...).Updates(item.mc()).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -63,6 +69,9 @@ func (item *Admin) Update(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) err

// Delete 删除
func (item *Admin) Delete(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(item).Unscoped().Scopes(scopes...).Delete(item).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand Down
9 changes: 9 additions & 0 deletions gin/admin/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type LoginResponse struct {
}

func (res *Response) First(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(&Admin{}).Scopes(scopes...).First(res).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -54,6 +57,9 @@ type PageResponse struct {
}

func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) (int64, error) {
if db == nil {
return 0, gorm.ErrInvalidDB
}
db = db.Model(&Admin{})
var count int64
if len(scopes) > 0 {
Expand All @@ -76,6 +82,9 @@ func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm
}

func (res *PageResponse) Find(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
db = db.Model(&Admin{})
err := db.Scopes(scopes...).Find(&res.Item).Error
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions gin/api/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

// CreatenInBatches 批量加入
func CreatenInBatches(db *gorm.DB, apis ApiCollection) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(&Api{}).CreateInBatches(&apis, 500).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand Down
9 changes: 9 additions & 0 deletions gin/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (item *Api) mc() map[string]interface{} {

// Create 添加
func (item *Api) Create(db *gorm.DB) (uint, error) {
if db == nil {
return 0, gorm.ErrInvalidDB
}
err := db.Model(item).Create(item).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -48,6 +51,9 @@ func (item *Api) Create(db *gorm.DB) (uint, error) {

// Update 更新
func (item *Api) Update(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(item).Scopes(scopes...).Updates(item.mc()).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -58,6 +64,9 @@ func (item *Api) Update(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error

// Delete 删除
func (item *Api) Delete(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(item).Unscoped().Scopes(scopes...).Delete(item).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand Down
9 changes: 9 additions & 0 deletions gin/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Response struct {
}

func (res *Response) First(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(&Api{}).Scopes(scopes...).First(res).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -29,6 +32,9 @@ type PageResponse struct {
}

func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) (int64, error) {
if db == nil {
return 0, gorm.ErrInvalidDB
}
db = db.Model(&Api{})
var count int64
if len(scopes) > 0 {
Expand All @@ -49,6 +55,9 @@ func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm
}

func (res *PageResponse) Find(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
db = db.Model(&Api{})
err := db.Scopes(scopes...).Find(&res.Item).Error
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions gin/authority/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (item *Authority) mc() map[string]interface{} {

// Create 添加
func (item *Authority) Create(db *gorm.DB) (uint, error) {
if db == nil {
return 0, gorm.ErrInvalidDB
}
err := db.Model(item).Create(item).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -44,6 +47,9 @@ func (item *Authority) Create(db *gorm.DB) (uint, error) {

// Update 更新
func (item *Authority) Update(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(item).Scopes(scopes...).Updates(item.mc()).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -54,6 +60,9 @@ func (item *Authority) Update(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB)

// Delete 删除
func (item *Authority) Delete(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(item).Unscoped().Scopes(scopes...).Delete(item).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand Down
21 changes: 19 additions & 2 deletions gin/authority/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Response struct {
}

func (res *Response) First(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(&Authority{}).Scopes(scopes...).First(res).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -32,6 +35,9 @@ type PageResponse struct {
}

func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) (int64, error) {
if db == nil {
return 0, gorm.ErrInvalidDB
}
db = db.Model(&Authority{})
var count int64
if len(scopes) > 0 {
Expand Down Expand Up @@ -61,6 +67,9 @@ func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm
}

func (res *PageResponse) Find(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
db = db.Model(&Authority{})
err := db.Scopes(scopes...).Find(&res.Item).Error
if err != nil {
Expand All @@ -83,7 +92,11 @@ func (res *PageResponse) Find(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB)

// findChildrenAuthority
func findChildrenAuthority(item *Response) error {
err := database.Instance().Where("parent_id = ?", item.Id).Find(&item.Children).Error
db := database.Instance()
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Where("parent_id = ?", item.Id).Find(&item.Children).Error
if len(item.Children) > 0 {
for k := range item.Children {
err = findChildrenAuthority(&item.Children[k])
Expand All @@ -95,7 +108,11 @@ func findChildrenAuthority(item *Response) error {
// getPermsForRoleMap
func getPermsForRoleMap(uuid string) []map[string]string {
apisForRoles := []map[string]string{}
perms := casbin.Instance().GetPermissionsForUser(uuid)
ca := casbin.Instance()
if ca == nil {
return nil
}
perms := ca.GetPermissionsForUser(uuid)
for _, perm := range perms {
if len(perm) < 3 {
continue
Expand Down
9 changes: 9 additions & 0 deletions gin/oplog/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Response struct {
}

func (res *Response) First(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
err := db.Model(&operation.Oplog{}).Scopes(scopes...).First(res).Error
if err != nil {
zap_server.ZAPLOG.Error(err.Error())
Expand All @@ -25,6 +28,9 @@ type PageResponse struct {
}

func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) (int64, error) {
if db == nil {
return 0, gorm.ErrInvalidDB
}
db = db.Model(&operation.Oplog{})
var count int64
err := db.Scopes(scopes...).Count(&count).Error
Expand All @@ -42,6 +48,9 @@ func (res *PageResponse) Paginate(db *gorm.DB, pageScope func(db *gorm.DB) *gorm
}

func (res *PageResponse) Find(db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) error {
if db == nil {
return gorm.ErrInvalidDB
}
db = db.Model(&operation.Oplog{})
err := db.Scopes(scopes...).Find(&res.Item).Error
if err != nil {
Expand Down
Loading

0 comments on commit b222b60

Please sign in to comment.