package functionset import ( "encoding/json" function "example.com/database/function" "gorm.io/gorm" ) type FunctionSet struct { gorm.Model Functions []function.Function `gorm:"many2many:functionset_function_associations" json:"functions"` } type FunctionSetParams struct { // ID(s) of the object being modified IDArray []uint // New fields Functions []uint `json:"functions"` } func (params *FunctionSetParams) parse(IDUintArray *[]uint, body *[]byte) error { var inputParams FunctionSetParams params.IDArray = *IDUintArray if len(*body) == 0 { return nil } err := json.Unmarshal(*body, &inputParams) if err != nil { return err } params.Functions = inputParams.Functions return nil } 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 uint) { db.Where("id = ?", inputFunctionSet).Take(&functionSet) functionSet.getAssociations(db) } func (functionSet FunctionSet) update(db *gorm.DB) error { // Get the original FunctionSet object var originalFunctionSet FunctionSet result := db.Model(&FunctionSet{}).Where("id = ?", functionSet.Model.ID).Take(&originalFunctionSet) if result.Error != nil { return result.Error } // Set the associated values by grabbing them from the database tagsError := db.Model(&originalFunctionSet).Association("Functions").Replace(&functionSet.Functions) if tagsError != nil { return tagsError } db.Save(&originalFunctionSet) return nil } func (functionSet FunctionSet) delete(db *gorm.DB) error { result := db.Unscoped().Delete(&functionSet) if result.Error != nil { return result.Error } return nil } func Create(db *gorm.DB, params FunctionSetParams) error { var newFunctions []function.Function if len(params.Functions) > 0 { newFunctions = *function.Get(db, params.Functions) } return FunctionSet{ Functions: newFunctions, }.create(db) } func Get(db *gorm.DB, inputFunctionSets []uint) *[]FunctionSet { var outputFunctionSets []FunctionSet if len(inputFunctionSets) < 1 { db.Model(&FunctionSet{}).Select("id").Find(&inputFunctionSets) } for _, inputFunctionSet := range inputFunctionSets { var outputFunctionSet FunctionSet outputFunctionSet.get(db, inputFunctionSet) outputFunctionSets = append(outputFunctionSets, outputFunctionSet) } return &outputFunctionSets } func Update(db *gorm.DB, params FunctionSetParams) error { var newFunctions []function.Function if len(params.Functions) > 0 { newFunctions = *function.Get(db, params.Functions) } return FunctionSet{ Functions: newFunctions, }.update(db) } func Delete(db *gorm.DB, inputFunctionSets []uint) error { var functionSets []FunctionSet // if len(inputFunctionSets) < 1 { // result := db.Model(&FunctionSet{}).Select("id").Find(&inputFunctionSets) // if result.Error != nil { // return result.Error // } // } for _, inputFunctionSet := range inputFunctionSets { var functionSet FunctionSet functionSet.get(db, inputFunctionSet) functionSets = append(functionSets, functionSet) } for _, function := range functionSets { err := function.delete(db) if err != nil { return err } } return nil } func HandleRequest(method string, db *gorm.DB, IDUintArray *[]uint, body *[]byte) (*[]FunctionSet, error) { var err error var params FunctionSetParams err = params.parse(IDUintArray, body) if err != nil { return nil, err } var result *[]FunctionSet switch method { case "GET": result = Get(db, params.IDArray) case "POST": err = Create(db, params) case "PUT": err = Update(db, params) case "DELETE": err = Delete(db, params.IDArray) } return result, err }