generated from meshery/meshery
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: pransh62390 <[email protected]>
- Loading branch information
1 parent
bd81bfa
commit 8d0b1d5
Showing
3 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package subCategory | ||
|
||
import "github.com/gofrs/uuid" | ||
|
||
type SubCategoryDefinition struct { | ||
Id uuid.UUID `json:"-"` | ||
Name string `json:"name" gorm:"name"` | ||
CategoryID uuid.UUID `json:"category_id" gorm:"category_id"` | ||
Metadata map[string]interface{} `json:"metadata,omitempty" yaml:"metadata,omitempty" gorm:"type:bytes;serializer:json"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// The file implements the Entity interface on the SubCategoryDefinition struct. | ||
package subCategory | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/layer5io/meshkit/database" | ||
"github.com/layer5io/meshkit/models/meshmodel/entity" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var subCategoryCreationLock sync.Mutex | ||
|
||
// GenerateID generates a unique ID for a sub-category based on its name and category ID. | ||
func (cat *SubCategoryDefinition) GenerateID() (uuid.UUID, error) { | ||
categoryIdentifier := struct { | ||
Name string | ||
CategoryID uuid.UUID | ||
}{ | ||
Name: cat.Name, | ||
CategoryID: cat.CategoryID, | ||
} | ||
|
||
byt, err := json.Marshal(categoryIdentifier) | ||
if err != nil { | ||
return uuid.UUID{}, err | ||
} | ||
|
||
hash := md5.Sum(byt) | ||
return uuid.FromString(hex.EncodeToString(hash[:])) | ||
} | ||
|
||
// Create adds a new sub-category to the database. | ||
func (cat *SubCategoryDefinition) Create(db *database.Handler) (uuid.UUID, error) { | ||
if cat.Name == "" { | ||
return uuid.UUID{}, fmt.Errorf("sub-category name cannot be empty") | ||
} | ||
|
||
if cat.CategoryID == uuid.Nil { | ||
return uuid.UUID{}, fmt.Errorf("category ID is required") | ||
} | ||
|
||
// Generate ID for the sub-category. | ||
catID, err := cat.GenerateID() | ||
if err != nil { | ||
return uuid.UUID{}, err | ||
} | ||
|
||
// Ensure thread safety. | ||
subCategoryCreationLock.Lock() | ||
defer subCategoryCreationLock.Unlock() | ||
|
||
var existingSubCategory SubCategoryDefinition | ||
err = db.First(&existingSubCategory, "id = ?", catID).Error | ||
if err != nil && err != gorm.ErrRecordNotFound { | ||
return uuid.UUID{}, err | ||
} | ||
|
||
// If no record exists, create a new one. | ||
if err == gorm.ErrRecordNotFound { | ||
cat.Id = catID | ||
err = db.Create(&cat).Error | ||
if err != nil { | ||
return uuid.UUID{}, err | ||
} | ||
return cat.Id, nil | ||
} | ||
|
||
// If record exists, return the existing ID. | ||
return existingSubCategory.Id, nil | ||
} | ||
|
||
// LinkCategory creates a relationship between a category and its sub-categories. | ||
func LinkCategory(db *database.Handler, category *CategoryDefinition, subCategories []SubCategoryDefinition) error { | ||
if category.Id == uuid.Nil { | ||
return fmt.Errorf("category ID is required") | ||
} | ||
|
||
for i := range subCategories { | ||
subCategories[i].CategoryID = category.Id | ||
if _, err := subCategories[i].Create(db); err != nil { | ||
return fmt.Errorf("failed to create sub-category: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |