|
| 1 | +// The file implements the Entity interface on the SubCategoryDefinition struct. |
| 2 | +package subCategory |
| 3 | + |
| 4 | +import ( |
| 5 | + "crypto/md5" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/gofrs/uuid" |
| 12 | + "github.com/layer5io/meshkit/database" |
| 13 | + "github.com/layer5io/meshkit/models/meshmodel/entity" |
| 14 | + "gorm.io/gorm" |
| 15 | +) |
| 16 | + |
| 17 | +var subCategoryCreationLock sync.Mutex |
| 18 | + |
| 19 | +// GenerateID generates a unique ID for a sub-category based on its name and category ID. |
| 20 | +func (cat *SubCategoryDefinition) GenerateID() (uuid.UUID, error) { |
| 21 | + categoryIdentifier := struct { |
| 22 | + Name string |
| 23 | + CategoryID uuid.UUID |
| 24 | + }{ |
| 25 | + Name: cat.Name, |
| 26 | + CategoryID: cat.CategoryID, |
| 27 | + } |
| 28 | + |
| 29 | + byt, err := json.Marshal(categoryIdentifier) |
| 30 | + if err != nil { |
| 31 | + return uuid.UUID{}, err |
| 32 | + } |
| 33 | + |
| 34 | + hash := md5.Sum(byt) |
| 35 | + return uuid.FromString(hex.EncodeToString(hash[:])) |
| 36 | +} |
| 37 | + |
| 38 | +// Create adds a new sub-category to the database. |
| 39 | +func (cat *SubCategoryDefinition) Create(db *database.Handler) (uuid.UUID, error) { |
| 40 | + if cat.Name == "" { |
| 41 | + return uuid.UUID{}, fmt.Errorf("sub-category name cannot be empty") |
| 42 | + } |
| 43 | + |
| 44 | + if cat.CategoryID == uuid.Nil { |
| 45 | + return uuid.UUID{}, fmt.Errorf("category ID is required") |
| 46 | + } |
| 47 | + |
| 48 | + // Generate ID for the sub-category. |
| 49 | + catID, err := cat.GenerateID() |
| 50 | + if err != nil { |
| 51 | + return uuid.UUID{}, err |
| 52 | + } |
| 53 | + |
| 54 | + // Ensure thread safety. |
| 55 | + subCategoryCreationLock.Lock() |
| 56 | + defer subCategoryCreationLock.Unlock() |
| 57 | + |
| 58 | + var existingSubCategory SubCategoryDefinition |
| 59 | + err = db.First(&existingSubCategory, "id = ?", catID).Error |
| 60 | + if err != nil && err != gorm.ErrRecordNotFound { |
| 61 | + return uuid.UUID{}, err |
| 62 | + } |
| 63 | + |
| 64 | + // If no record exists, create a new one. |
| 65 | + if err == gorm.ErrRecordNotFound { |
| 66 | + cat.Id = catID |
| 67 | + err = db.Create(&cat).Error |
| 68 | + if err != nil { |
| 69 | + return uuid.UUID{}, err |
| 70 | + } |
| 71 | + return cat.Id, nil |
| 72 | + } |
| 73 | + |
| 74 | + // If record exists, return the existing ID. |
| 75 | + return existingSubCategory.Id, nil |
| 76 | +} |
| 77 | + |
| 78 | +// LinkCategory creates a relationship between a category and its sub-categories. |
| 79 | +func LinkCategory(db *database.Handler, category *CategoryDefinition, subCategories []SubCategoryDefinition) error { |
| 80 | + if category.Id == uuid.Nil { |
| 81 | + return fmt.Errorf("category ID is required") |
| 82 | + } |
| 83 | + |
| 84 | + for i := range subCategories { |
| 85 | + subCategories[i].CategoryID = category.Id |
| 86 | + if _, err := subCategories[i].Create(db); err != nil { |
| 87 | + return fmt.Errorf("failed to create sub-category: %w", err) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
0 commit comments