64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
![]() |
package function
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
|
||
|
functiontag "example.com/database/functiontag"
|
||
|
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type Function struct {
|
||
|
gorm.Model
|
||
|
Name string `gorm:"primaryKey uniqueIndex index" json:"name"`
|
||
|
Tags []functiontag.FunctionTag `gorm:"many2many:function_tag_associations" json:"tags"`
|
||
|
Requirements []Function `gorm:"many2many:function_requirement_associations" json:"requirements"`
|
||
|
}
|
||
|
|
||
|
func (function *Function) getAssociations(db *gorm.DB) {
|
||
|
db.Model(&function).Association("Tags").Find(&function.Tags)
|
||
|
db.Model(&function).Association("Requirements").Find(&function.Requirements)
|
||
|
}
|
||
|
|
||
|
func (function *Function) Get(db *gorm.DB, inputFunction string) {
|
||
|
db.Where("name = ?", inputFunction).Take(&function)
|
||
|
function.getAssociations(db)
|
||
|
}
|
||
|
|
||
|
func (function Function) Update(db *gorm.DB) error {
|
||
|
result := db.Save(&function)
|
||
|
if result.Error != nil {
|
||
|
return result.Error
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (function Function) Create(db *gorm.DB) error {
|
||
|
result := db.Create(&function)
|
||
|
if result.Error != nil {
|
||
|
return result.Error
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Get(db *gorm.DB, inputFunctions []string) *[]Function {
|
||
|
var outputFunctions []Function
|
||
|
for _, inputFunction := range inputFunctions {
|
||
|
var outputFunction Function
|
||
|
outputFunction.Get(db, inputFunction)
|
||
|
outputFunctions = append(outputFunctions, outputFunction)
|
||
|
}
|
||
|
return &outputFunctions
|
||
|
}
|
||
|
|
||
|
func GetAll(db *gorm.DB) *[]Function {
|
||
|
var outputFunctions []Function
|
||
|
var outputFunctionNames []string
|
||
|
result := db.Model(&Function{}).Select("name").Find(&outputFunctionNames)
|
||
|
if result.Error != nil {
|
||
|
log.Println(result.Error)
|
||
|
}
|
||
|
outputFunctions = *Get(db, outputFunctionNames)
|
||
|
return &outputFunctions
|
||
|
}
|