Skip to content

Commit

Permalink
Merge pull request #38 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
feature(database): model add UpsertModelsOnUniqueID and InsertModelsO…
  • Loading branch information
Matrix-X authored Aug 14, 2022
2 parents 7912637 + dc83eb6 commit 10d9d46
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions authorization/rbac/models/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func (mdl *Permission) GetForeignValue() string {

func (mdl *Permission) GetComposedUniqueID() string {

strKey := *mdl.ModuleID + "-" + mdl.Action + "-" + mdl.SubjectValue
strKey := mdl.Action + "-" + mdl.SubjectValue
//fmt2.Dump(strKey)
hashKey := security.HashStringData(strKey)

return hashKey
}

func (mdl *Permission) CheckPermissionModuleNameAvailable(db *gorm.DB) (err error) {
func (mdl *Permission) CheckPermissionNameAvailable(db *gorm.DB) (err error) {

result := db.
//Debug().
Expand Down
36 changes: 33 additions & 3 deletions database/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"math"
"reflect"
Expand Down Expand Up @@ -103,9 +104,6 @@ func (mdl *PowerModel) GetForeignReferValue() string {
// ---------------------------------------------------------------------------------------------------------------------
// PowerCompactModel
// ---------------------------------------------------------------------------------------------------------------------
func (mdl *PowerCompactModel) GetID() int32 {
return mdl.ID
}

func (mdl *PowerCompactModel) GetTableName(needFull bool) string {
return ""
Expand All @@ -115,6 +113,9 @@ func (mdl *PowerCompactModel) GetPowerModel() ModelInterface {
return mdl
}

func (mdl *PowerCompactModel) GetID() int32 {
return mdl.ID
}
func (mdl *PowerCompactModel) GetUUID() string {
return ""
}
Expand Down Expand Up @@ -247,6 +248,35 @@ func GetAllList(db *gorm.DB, conditions *map[string]interface{},
return nil
}

func InsertModelsOnUniqueID(db *gorm.DB, mdl interface{}, uniqueName string, models interface{}) error {

result := db.Model(mdl).
Debug().
Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: uniqueName}},
DoNothing: true,
}).Create(models)

return result.Error
}

func UpsertModelsOnUniqueID(db *gorm.DB, mdl interface{}, uniqueName string,
models interface{}, fieldsToUpdate []string) error {

if len(fieldsToUpdate) <= 0 {
fieldsToUpdate = GetModelFields(mdl)
}

result := db.Model(mdl).
Debug().
Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: uniqueName}},
DoUpdates: clause.AssignmentColumns(fieldsToUpdate),
}).Create(models)

return result.Error
}

/**
* model methods
*/
Expand Down

0 comments on commit 10d9d46

Please sign in to comment.