Skip to content

Commit

Permalink
added model for subcategory
Browse files Browse the repository at this point in the history
Signed-off-by: pransh62390 <[email protected]>
  • Loading branch information
pransh62390 committed Jan 21, 2025
1 parent bd81bfa commit 8d0b1d5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
7 changes: 4 additions & 3 deletions models/v1beta1/category/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions models/v1beta1/subCategory/sub_category.go
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"`
}
92 changes: 92 additions & 0 deletions models/v1beta1/subCategory/sub_category_helper.go
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
}

0 comments on commit 8d0b1d5

Please sign in to comment.