Skip to content

Commit

Permalink
Merge pull request #29 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
feature(database): tagGroup GetDefaultTagGroup
  • Loading branch information
Matrix-X authored Jul 22, 2022
2 parents 63480a6 + 4829b80 commit d983db9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion database/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tag

import (
"database/sql"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v2/database"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/ArtisanCloud/PowerLibs/v2/security"
Expand Down Expand Up @@ -34,6 +35,7 @@ const TABLE_NAME_TAG = "tags"
const TAG_UNIQUE_ID = "index_tag_id"

const TAG_TYPE_NORMAL int8 = 1
const TAG_TYPE_STAGE int8 = 2

func NewTag(mapObject *object.Collection) *Tag {

Expand Down Expand Up @@ -68,7 +70,7 @@ func (mdl *Tag) GetForeignKey() string {

func (mdl *Tag) GetComposedUniqueID() string {

strKey := mdl.GroupID + "-" + mdl.Name
strKey := fmt.Sprintf("%d", mdl.Type) + "-" + mdl.GroupID + "-" + mdl.Name
hashKey := security.HashStringData(strKey)

return hashKey
Expand Down
33 changes: 33 additions & 0 deletions database/tag/tagGroup.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tag

import (
"errors"
"github.com/ArtisanCloud/PowerLibs/v2/database"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/ArtisanCloud/PowerLibs/v2/security"
"gorm.io/gorm"
)

// TableName overrides the table name used by TagGroup to `profiles`
Expand All @@ -24,6 +26,9 @@ type TagGroup struct {
const TABLE_NAME_TAG_GROUP = "tag_groups"
const TAG_GROUP_UNIQUE_ID = "index_tag_group_id"

const DEFAULT_OWNER_TYPE = "default"
const DEFAULT_GROUP_NAME = "默认组"

func NewTagGroup(mapObject *object.Collection) *TagGroup {
if mapObject == nil {
mapObject = object.NewCollection(&object.HashMap{})
Expand All @@ -46,6 +51,34 @@ func NewTagGroup(mapObject *object.Collection) *TagGroup {
return tagGroup
}

func GetDefaultTagGroup(db *gorm.DB) (defaultTagGroup *TagGroup, err error) {

defaultTagGroup = &TagGroup{}

conditions := &map[string]interface{}{
"group_name": DEFAULT_GROUP_NAME,
"owner_type": DEFAULT_OWNER_TYPE,
}

err = database.GetFirst(db, conditions, defaultTagGroup, nil)

if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
defaultTagGroup = &TagGroup{
GroupName: DEFAULT_GROUP_NAME,
OwnerType: DEFAULT_OWNER_TYPE,
}
defaultTagGroup.UniqueID = defaultTagGroup.GetComposedUniqueID()

result := db.Create(defaultTagGroup)
err = result.Error
if err != nil {
return nil, err
}
}

return defaultTagGroup, err
}

func (mdl *TagGroup) GetTableName(needFull bool) string {
tableName := TABLE_NAME_TAG_GROUP
if needFull {
Expand Down

0 comments on commit d983db9

Please sign in to comment.