45 lines
1.1 KiB
Cheetah
45 lines
1.1 KiB
Cheetah
package models
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
{{formatComment (printf "Model for %s entity\nGenerated with custom JavaScript functions" .Entity.Name)}}
|
|
type {{.Entity.Name | title}} struct {
|
|
{{- range .Entity.Fields}}
|
|
{{.Name | title}} {{goType .Type}} {{dbFieldMapping .Name .Type}}
|
|
{{- end}}
|
|
}
|
|
|
|
{{formatComment "Table name for GORM"}}
|
|
func ({{.Entity.Name | title}}) TableName() string {
|
|
return "{{pluralize (.Entity.Name | lower)}}"
|
|
}
|
|
|
|
{{formatComment "Validation function using custom JavaScript validation rules"}}
|
|
func (m *{{.Entity.Name | title}}) Validate() error {
|
|
{{- range .Entity.Fields}}
|
|
{{- if .Required}}
|
|
{{generateValidation .Type .Name}}
|
|
{{- end}}
|
|
{{- end}}
|
|
return nil
|
|
}
|
|
|
|
{{formatComment "Create a new instance with validation"}}
|
|
func New{{.Entity.Name | title}}({{range $i, $field := .Entity.Fields}}{{if $i}}, {{end}}{{$field.Name | lower}} {{goType $field.Type}}{{end}}) (*{{.Entity.Name | title}}, error) {
|
|
model := &{{.Entity.Name | title}}{
|
|
{{- range .Entity.Fields}}
|
|
{{.Name | title}}: {{.Name | lower}},
|
|
{{- end}}
|
|
}
|
|
|
|
if err := model.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return model, nil
|
|
}
|