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) error { result := db.Model(&FunctionSet{}).Where("id = ?", inputFunctionSet).Take(&functionSet) if result.Error != nil { return result.Error } functionSet.getAssociations(db) return nil } 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 functionsError := db.Model(&originalFunctionSet).Association("Functions").Replace(&functionSet.Functions) if functionsError != nil { return functionsError } 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 var err error if len(params.Functions) > 0 { newFunctions, err = function.Get(db, params.Functions) if err != nil { return err } } return FunctionSet{ Functions: newFunctions, }.create(db) } func Get(db *gorm.DB, inputFunctionSets []uint) ([]FunctionSet, error) { var outputFunctionSets []FunctionSet if len(inputFunctionSets) < 1 { db.Model(&FunctionSet{}).Select("id").Find(&inputFunctionSets) } for _, inputFunctionSet := range inputFunctionSets { var outputFunctionSet FunctionSet err := outputFunctionSet.get(db, inputFunctionSet) if err != nil { return nil, err } outputFunctionSets = append(outputFunctionSets, outputFunctionSet) } return outputFunctionSets, nil } func Update(db *gorm.DB, params FunctionSetParams) error { var newFunctions []function.Function var err error if len(params.Functions) > 0 { newFunctions, err = function.Get(db, params.Functions) if err != nil { return err } } return FunctionSet{ Model: gorm.Model{ID: params.IDArray[0]}, 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 _, functionSet := range functionSets { err := functionSet.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, err = 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 }