2025-04-28 12:38:15 -05:00
|
|
|
package functionset
|
|
|
|
|
|
|
|
|
|
import (
|
2025-05-15 10:13:35 -05:00
|
|
|
"encoding/json"
|
2025-04-28 12:38:15 -05:00
|
|
|
|
|
|
|
|
function "example.com/database/function"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type FunctionSet struct {
|
|
|
|
|
gorm.Model
|
|
|
|
|
Functions []function.Function `gorm:"many2many:functionset_function_associations" json:"functions"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
type FunctionSetParams struct {
|
|
|
|
|
// ID(s) of the object being modified
|
2025-05-16 22:51:54 -05:00
|
|
|
IDArray []uint
|
2025-05-15 10:13:35 -05:00
|
|
|
// 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 {
|
2025-04-28 12:38:15 -05:00
|
|
|
result := db.Create(&functionSet)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
return result.Error
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 00:42:57 -05:00
|
|
|
func (functionSet *FunctionSet) getAssociations(db *gorm.DB) {
|
|
|
|
|
db.Model(&functionSet).Association("Functions").Find(&functionSet.Functions)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 21:05:53 -05:00
|
|
|
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
|
|
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
functionSet.getAssociations(db)
|
2025-05-26 21:05:53 -05:00
|
|
|
return nil
|
2025-04-29 00:42:57 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
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)
|
2025-04-28 12:38:15 -05:00
|
|
|
if result.Error != nil {
|
2025-04-29 00:42:57 -05:00
|
|
|
return result.Error
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
// Set the associated values by grabbing them from the database
|
2025-05-26 21:05:53 -05:00
|
|
|
functionsError := db.Model(&originalFunctionSet).Association("Functions").Replace(&functionSet.Functions)
|
|
|
|
|
if functionsError != nil {
|
|
|
|
|
return functionsError
|
2025-05-15 10:13:35 -05:00
|
|
|
}
|
|
|
|
|
db.Save(&originalFunctionSet)
|
2025-04-29 00:42:57 -05:00
|
|
|
return nil
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func (functionSet FunctionSet) delete(db *gorm.DB) error {
|
|
|
|
|
result := db.Unscoped().Delete(&functionSet)
|
2025-04-29 17:54:32 -05:00
|
|
|
if result.Error != nil {
|
|
|
|
|
return result.Error
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func Create(db *gorm.DB, params FunctionSetParams) error {
|
|
|
|
|
var newFunctions []function.Function
|
2025-05-26 21:05:53 -05:00
|
|
|
var err error
|
2025-05-15 10:13:35 -05:00
|
|
|
if len(params.Functions) > 0 {
|
2025-05-26 21:05:53 -05:00
|
|
|
newFunctions, err = function.Get(db, params.Functions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
return FunctionSet{
|
2025-05-15 10:13:35 -05:00
|
|
|
Functions: newFunctions,
|
|
|
|
|
}.create(db)
|
2025-04-29 00:42:57 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-26 21:05:53 -05:00
|
|
|
func Get(db *gorm.DB, inputFunctionSets []uint) ([]FunctionSet, error) {
|
2025-04-28 12:38:15 -05:00
|
|
|
var outputFunctionSets []FunctionSet
|
2025-05-15 10:13:35 -05:00
|
|
|
if len(inputFunctionSets) < 1 {
|
|
|
|
|
db.Model(&FunctionSet{}).Select("id").Find(&inputFunctionSets)
|
|
|
|
|
}
|
2025-04-28 12:38:15 -05:00
|
|
|
for _, inputFunctionSet := range inputFunctionSets {
|
2025-04-29 00:42:57 -05:00
|
|
|
var outputFunctionSet FunctionSet
|
2025-05-26 21:05:53 -05:00
|
|
|
err := outputFunctionSet.get(db, inputFunctionSet)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
outputFunctionSets = append(outputFunctionSets, outputFunctionSet)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
return outputFunctionSets, nil
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func Update(db *gorm.DB, params FunctionSetParams) error {
|
|
|
|
|
var newFunctions []function.Function
|
2025-05-26 21:05:53 -05:00
|
|
|
var err error
|
2025-05-15 10:13:35 -05:00
|
|
|
if len(params.Functions) > 0 {
|
2025-05-26 21:05:53 -05:00
|
|
|
newFunctions, err = function.Get(db, params.Functions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
return FunctionSet{
|
2025-05-26 21:05:53 -05:00
|
|
|
Model: gorm.Model{ID: params.IDArray[0]},
|
2025-05-15 10:13:35 -05:00
|
|
|
Functions: newFunctions,
|
|
|
|
|
}.update(db)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
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)
|
|
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
for _, functionSet := range functionSets {
|
|
|
|
|
err := functionSet.delete(db)
|
2025-05-15 10:13:35 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
return nil
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
|
2025-05-26 21:05:53 -05:00
|
|
|
func HandleRequest(method string, db *gorm.DB, IDUintArray *[]uint, body *[]byte) ([]FunctionSet, error) {
|
2025-05-15 10:13:35 -05:00
|
|
|
var err error
|
|
|
|
|
var params FunctionSetParams
|
|
|
|
|
err = params.parse(IDUintArray, body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
var result []FunctionSet
|
2025-05-15 10:13:35 -05:00
|
|
|
switch method {
|
|
|
|
|
case "GET":
|
2025-05-26 21:05:53 -05:00
|
|
|
result, err = Get(db, params.IDArray)
|
2025-05-15 10:13:35 -05:00
|
|
|
case "POST":
|
|
|
|
|
err = Create(db, params)
|
|
|
|
|
case "PUT":
|
|
|
|
|
err = Update(db, params)
|
|
|
|
|
case "DELETE":
|
|
|
|
|
err = Delete(db, params.IDArray)
|
2025-04-29 17:54:32 -05:00
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
return result, err
|
2025-04-29 17:54:32 -05:00
|
|
|
}
|