2025-04-27 23:14:37 -05:00
|
|
|
package function
|
|
|
|
|
|
|
|
import (
|
2025-05-07 16:53:00 -05:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2025-04-27 23:14:37 -05:00
|
|
|
"log"
|
2025-05-07 16:53:00 -05:00
|
|
|
"strconv"
|
2025-04-27 23:14:37 -05:00
|
|
|
|
|
|
|
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
|
2025-05-07 16:53:00 -05:00
|
|
|
Name string `gorm:"uniqueIndex" json:"name"`
|
2025-04-27 23:14:37 -05:00
|
|
|
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 {
|
2025-05-12 11:24:57 -05:00
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tags []uint `json:"tags"`
|
|
|
|
Requirements []uint `json:"requirements"`
|
2025-04-30 16:18:27 -05:00
|
|
|
}
|
|
|
|
|
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 {
|
2025-05-07 16:53:00 -05:00
|
|
|
body, err := io.ReadAll(context.Request.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
2025-04-30 16:18:27 -05:00
|
|
|
}
|
2025-05-07 16:53:00 -05:00
|
|
|
log.Println(string(body))
|
|
|
|
var newParams functionParams
|
|
|
|
err = json.Unmarshal(body, &newParams)
|
|
|
|
log.Println(err, newParams)
|
|
|
|
params.Id = newParams.Id
|
|
|
|
params.Name = newParams.Name
|
|
|
|
params.Tags = newParams.Tags
|
|
|
|
params.Requirements = newParams.Requirements
|
2025-04-30 16:18:27 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func (function *Function) Get(db *gorm.DB, inputFunction uint) {
|
|
|
|
db.Where("id = ?", inputFunction).Take(&function)
|
2025-04-27 23:14:37 -05:00
|
|
|
function.getAssociations(db)
|
|
|
|
}
|
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func (function Function) update(db *gorm.DB) error {
|
2025-04-29 17:54:32 -05:00
|
|
|
var originalFunction Function
|
2025-05-07 16:53:00 -05:00
|
|
|
result := db.Model(&Function{}).Where("id = ?", function.Model.ID).Take(&originalFunction)
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
originalFunction.Name = function.Name
|
2025-04-29 17:54:32 -05:00
|
|
|
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
|
|
|
|
}
|
2025-05-07 16:53:00 -05:00
|
|
|
db.Save(&originalFunction)
|
2025-04-29 17:54:32 -05:00
|
|
|
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-05-07 16:53:00 -05:00
|
|
|
Name: params.Name,
|
2025-04-30 16:18:27 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func Get(db *gorm.DB, inputFunctions []uint) *[]Function {
|
2025-04-27 23:14:37 -05:00
|
|
|
var outputFunctions []Function
|
|
|
|
for _, inputFunction := range inputFunctions {
|
|
|
|
var outputFunction Function
|
|
|
|
outputFunction.Get(db, inputFunction)
|
|
|
|
outputFunctions = append(outputFunctions, outputFunction)
|
|
|
|
}
|
2025-05-07 16:53:00 -05:00
|
|
|
log.Println(outputFunctions)
|
2025-04-27 23:14:37 -05:00
|
|
|
return &outputFunctions
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAll(db *gorm.DB) *[]Function {
|
2025-05-07 16:53:00 -05:00
|
|
|
var outputFunctionNames []uint
|
|
|
|
result := db.Model(&Function{}).Select("id").Find(&outputFunctionNames)
|
2025-04-27 23:14:37 -05:00
|
|
|
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-05-07 16:53:00 -05:00
|
|
|
uintID, err := strconv.Atoi(params.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-04-29 00:42:57 -05:00
|
|
|
return Function{
|
2025-05-07 16:53:00 -05:00
|
|
|
Model: gorm.Model{ID: uint(uintID)},
|
|
|
|
Name: params.Name,
|
2025-04-30 16:18:27 -05:00
|
|
|
Tags: *functiontag.Get(db, params.Tags),
|
|
|
|
Requirements: *Get(db, params.Requirements),
|
2025-05-07 16:53:00 -05:00
|
|
|
}.update(db)
|
2025-04-27 23:14:37 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func Delete(db *gorm.DB, inputFunctions []uint) error {
|
2025-04-29 17:54:32 -05:00
|
|
|
functions := Get(db, inputFunctions)
|
2025-05-07 16:53:00 -05:00
|
|
|
log.Println(inputFunctions, functions)
|
2025-04-29 17:54:32 -05:00
|
|
|
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
|
|
|
}
|