2025-04-28 12:38:15 -05:00
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 00:42:57 -05:00
|
|
|
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-07 16:53:00 -05:00
|
|
|
func (functionSet *FunctionSet) Get(db *gorm.DB, inputFunctionSet uint) {
|
2025-04-29 00:42:57 -05:00
|
|
|
db.Where("id = ?", inputFunctionSet).Take(&functionSet)
|
|
|
|
|
functionSet.getAssociations(db)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (functionSet FunctionSet) Update(db *gorm.DB) error {
|
|
|
|
|
result := db.Save(&functionSet)
|
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-04-29 00:42:57 -05:00
|
|
|
return nil
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-29 17:54:32 -05:00
|
|
|
func (functionSet FunctionSet) Delete(db *gorm.DB) error {
|
|
|
|
|
result := db.Delete(&functionSet)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
return result.Error
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func Create(db *gorm.DB, functions []uint) error {
|
2025-04-29 00:42:57 -05:00
|
|
|
return FunctionSet{
|
|
|
|
|
Functions: *function.Get(db, functions),
|
|
|
|
|
}.Create(db)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func Get(db *gorm.DB, inputFunctionSets []uint) *[]FunctionSet {
|
2025-04-28 12:38:15 -05:00
|
|
|
var outputFunctionSets []FunctionSet
|
|
|
|
|
for _, inputFunctionSet := range inputFunctionSets {
|
2025-04-29 00:42:57 -05:00
|
|
|
var outputFunctionSet FunctionSet
|
|
|
|
|
outputFunctionSet.Get(db, inputFunctionSet)
|
|
|
|
|
outputFunctionSets = append(outputFunctionSets, outputFunctionSet)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
return &outputFunctionSets
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-29 00:42:57 -05:00
|
|
|
func GetAll(db *gorm.DB) *[]FunctionSet {
|
2025-05-07 16:53:00 -05:00
|
|
|
var outputFunctionSetIDs []uint
|
2025-04-29 00:42:57 -05:00
|
|
|
result := db.Model(&FunctionSet{}).Select("id").Find(&outputFunctionSetIDs)
|
2025-04-28 12:38:15 -05:00
|
|
|
if result.Error != nil {
|
|
|
|
|
log.Println(result.Error)
|
|
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
return Get(db, outputFunctionSetIDs)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func Update(db *gorm.DB, id int, functions []uint) error {
|
2025-04-29 00:42:57 -05:00
|
|
|
outputFunctionSet := FunctionSet{
|
|
|
|
|
Functions: *function.Get(db, functions),
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
outputFunctionSet.ID = uint(id)
|
|
|
|
|
return outputFunctionSet.Update(db)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func Delete(db *gorm.DB, inputFunctionSets []uint) {
|
2025-04-29 17:54:32 -05:00
|
|
|
functionSets := Get(db, inputFunctionSets)
|
|
|
|
|
for _, functionSet := range *functionSets {
|
|
|
|
|
functionSet.Delete(db)
|
|
|
|
|
}
|
|
|
|
|
}
|