package functionset import ( "log" function "example.com/database/function" "gorm.io/gorm" ) type FunctionSet struct { gorm.Model Functions []function.Function `gorm:"many2many:functionset_function_associations" json:"functions"` } func (functionSet FunctionSet) Create(db *gorm.DB) error { result := db.Create(&functionSet) if result.Error != nil { return result.Error } return nil } func (functionSet *FunctionSet) getAssociations(db *gorm.DB) { db.Model(&functionSet).Association("Functions").Find(&functionSet.Functions) } func (functionSet *FunctionSet) Get(db *gorm.DB, inputFunctionSet int) { db.Where("id = ?", inputFunctionSet).Take(&functionSet) functionSet.getAssociations(db) } func (functionSet FunctionSet) Update(db *gorm.DB) error { result := db.Save(&functionSet) if result.Error != nil { return result.Error } return nil } func Create(db *gorm.DB, functions []string) error { return FunctionSet{ Functions: *function.Get(db, functions), }.Create(db) } func Get(db *gorm.DB, inputFunctionSets []int) *[]FunctionSet { var outputFunctionSets []FunctionSet for _, inputFunctionSet := range inputFunctionSets { var outputFunctionSet FunctionSet outputFunctionSet.Get(db, inputFunctionSet) outputFunctionSets = append(outputFunctionSets, outputFunctionSet) } return &outputFunctionSets } func GetAll(db *gorm.DB) *[]FunctionSet { var outputFunctionSetIDs []int result := db.Model(&FunctionSet{}).Select("id").Find(&outputFunctionSetIDs) if result.Error != nil { log.Println(result.Error) } return Get(db, outputFunctionSetIDs) } func Update(db *gorm.DB, id int, functions []string) error { outputFunctionSet := FunctionSet{ Functions: *function.Get(db, functions), } outputFunctionSet.ID = uint(id) return outputFunctionSet.Update(db) }