2025-04-27 23:14:37 -05:00
|
|
|
package function
|
|
|
|
|
|
|
|
import (
|
2025-04-30 16:18:27 -05:00
|
|
|
"errors"
|
2025-04-27 23:14:37 -05:00
|
|
|
"log"
|
|
|
|
|
|
|
|
functiontag "example.com/database/functiontag"
|
2025-04-30 16:18:27 -05:00
|
|
|
"github.com/gin-gonic/gin"
|
2025-04-27 23:14:37 -05:00
|
|
|
|
|
|
|
"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"`
|
|
|
|
}
|
|
|
|
|
2025-04-30 16:18:27 -05:00
|
|
|
type functionParams struct {
|
|
|
|
Id string
|
|
|
|
Tags []string
|
|
|
|
Requirements []string
|
|
|
|
}
|
|
|
|
|
2025-04-29 00:42:57 -05:00
|
|
|
func (function Function) Create(db *gorm.DB) error {
|
|
|
|
result := db.Create(&function)
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-27 23:14:37 -05:00
|
|
|
func (function *Function) getAssociations(db *gorm.DB) {
|
|
|
|
db.Model(&function).Association("Tags").Find(&function.Tags)
|
|
|
|
db.Model(&function).Association("Requirements").Find(&function.Requirements)
|
|
|
|
}
|
|
|
|
|
2025-04-30 16:18:27 -05:00
|
|
|
func (params *functionParams) validate(context *gin.Context) error {
|
|
|
|
Id, idOk := context.GetQuery("id")
|
|
|
|
Tags, tagsOK := context.GetQueryArray("tags")
|
|
|
|
Requirements, requirementsOK := context.GetQueryArray("requirements")
|
|
|
|
if idOk && tagsOK && requirementsOK {
|
|
|
|
params.Id = Id
|
|
|
|
if len(Tags) > 0 {
|
|
|
|
params.Tags = Tags
|
|
|
|
}
|
|
|
|
if len(Requirements) > 0 {
|
|
|
|
params.Requirements = Requirements
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("One or more parameters not included in request")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-27 23:14:37 -05:00
|
|
|
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 {
|
2025-04-29 17:54:32 -05:00
|
|
|
var originalFunction Function
|
|
|
|
originalFunction.Get(db, function.Name)
|
|
|
|
tagsError := db.Model(&originalFunction).Association("Tags").Replace(&function.Tags)
|
|
|
|
if tagsError != nil {
|
|
|
|
return tagsError
|
|
|
|
}
|
|
|
|
requirementsError := db.Model(&originalFunction).Association("Requirements").Replace(&function.Requirements)
|
|
|
|
if requirementsError != nil {
|
|
|
|
return requirementsError
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (function Function) Delete(db *gorm.DB) error {
|
|
|
|
result := db.Delete(&function)
|
2025-04-27 23:14:37 -05:00
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-30 16:18:27 -05:00
|
|
|
func Create(db *gorm.DB, context *gin.Context) error {
|
|
|
|
var params functionParams
|
|
|
|
err := params.validate(context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
return Function{
|
2025-04-30 16:18:27 -05:00
|
|
|
Name: params.Id,
|
|
|
|
Tags: *functiontag.Get(db, params.Tags),
|
|
|
|
Requirements: *Get(db, params.Requirements),
|
2025-04-29 00:42:57 -05:00
|
|
|
}.Create(db)
|
2025-04-27 23:14:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 outputFunctionNames []string
|
|
|
|
result := db.Model(&Function{}).Select("name").Find(&outputFunctionNames)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(result.Error)
|
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
return Get(db, outputFunctionNames)
|
|
|
|
}
|
|
|
|
|
2025-04-30 16:18:27 -05:00
|
|
|
func Update(db *gorm.DB, context *gin.Context) error {
|
|
|
|
var params functionParams
|
|
|
|
err := params.validate(context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
return Function{
|
2025-04-30 16:18:27 -05:00
|
|
|
Name: params.Id,
|
|
|
|
Tags: *functiontag.Get(db, params.Tags),
|
|
|
|
Requirements: *Get(db, params.Requirements),
|
2025-04-29 00:42:57 -05:00
|
|
|
}.Update(db)
|
2025-04-27 23:14:37 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
|
2025-04-30 16:18:27 -05:00
|
|
|
func Delete(db *gorm.DB, inputFunctions []string) error {
|
2025-04-29 17:54:32 -05:00
|
|
|
functions := Get(db, inputFunctions)
|
|
|
|
for _, function := range *functions {
|
2025-04-30 16:18:27 -05:00
|
|
|
err := function.Delete(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
}
|
2025-04-30 16:18:27 -05:00
|
|
|
return nil
|
2025-04-29 17:54:32 -05:00
|
|
|
}
|