You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.3 KiB

1 year ago
package model
{{if .Imports}}
import (
{{range $val:= .Imports}}
"{{$val}}"{{end}}
)
{{end}}
1 year ago
type (
{{.Name|LowerType}}Model interface {
Insert(ctx context.Context, data *{{.Name|UpperType}}) error
{{ if .Primary}}Update(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}, updates map[string]any) error
Delete(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}) error
FindOne(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}) (*{{.Name|UpperType}}, error){{end}}
{{range $val:= .Indexes}}Find{{$val.Name|UpperType}}(ctx context.Context {{range $column:=$val.Columns}},{{$column.Name|LowerType}} {{$column.GoType}}{{end}}) (*{{$.Name|UpperType}}, error){{end}}
}
{{.Name|UpperType}} struct {
1 year ago
updates map[string]any
{{range $val := .Columns}}{{$val.Name|UpperType}} {{$val.GoType}} `db:"{{$val.Name}}"`{{if $val.Comment}}//{{$val.Comment}}{{end}}
{{end}}
1 year ago
}
default{{.Name|UpperType}}Model struct {
db *gorm.DB
}
)
1 year ago
func (s *{{.Name|UpperType}}) TableName() string {
return "{{.Name}}"
}
1 year ago
1 year ago
func New{{.Name|UpperType}}() *{{.Name|UpperType}} {
return &{{.Name|UpperType}}{
updates: make(map[string]any),
{{range $val := .Columns}}{{if $val.Default}}{{$val.Name|UpperType}}:{{if (eq $val.GoType "string")}}"{{$val.Default}}"{{else}}{{$val.Default}}{{end}},
{{end}}{{end}}
}
}
{{range $val := .Columns}}
func (s *{{$.Name|UpperType}}) Set{{$val.Name|UpperType}}({{$val.Name|LowerType}} {{$val.GoType}}) {
s.{{$val.Name|UpperType}} = {{$val.Name|LowerType}}
s.set("{{$val.Name}}", {{$val.Name|LowerType}})
}
{{end}}
func (s *{{.Name|UpperType}}) set(key string, val any) {
s.updates[key] = val
}
func (s *{{.Name|UpperType}}) UpdateColumn() map[string]any {
return s.updates
}
1 year ago
func new{{.Name|UpperType}}DAO(db *gorm.DB) *default{{.Name|UpperType}}Model {
return &default{{.Name|UpperType}}Model{
1 year ago
db: db,
}
}
1 year ago
func (s *default{{.Name|UpperType}}Model) Insert(ctx context.Context, data *{{.Name|UpperType}}) error {
1 year ago
return s.db.Create(data).Error
}
{{ if .Primary}}
1 year ago
func (s *default{{.Name|UpperType}}Model) Update(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}, updates map[string]any) error {
1 year ago
return s.db.Model({{.Name|UpperType}}{}).Where("{{.Primary.Name}}=?", {{.Primary.Name|LowerType}}).Updates(updates).Error
}
1 year ago
func (s *default{{.Name|UpperType}}Model) Delete(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}) error {
1 year ago
return s.db.Where("{{.Primary.Name}}=?", {{.Primary.Name|LowerType}}).Delete(&{{.Name|UpperType}}{}).Error
}
1 year ago
func (s *default{{.Name|UpperType}}Model) FindOne(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}) (*{{.Name|UpperType}}, error) {
1 year ago
row := {{.Name|UpperType}}{}
err := s.db.Where("{{.Primary.Name}}=?", {{.Primary.Name|LowerType}}).Find(&row).Error
1 year ago
return findResultWithError(&row,err)
1 year ago
}
{{end}}
{{range $val:= .Indexes}}
1 year ago
func (s *default{{$.Name|UpperType}}Model) Find{{$val.Name|UpperType}}(ctx context.Context {{range $column:=$val.Columns}},{{$column.Name|LowerType}} {{$column.GoType}}{{end}}) (*{{$.Name|UpperType}}, error) {
row := {{$.Name|UpperType}}{}
err := s.db.Where("{{$val.Columns|UpdateColumn}}" {{range $column:=$val.Columns}},{{$column.Name|LowerType}}{{end}}).Find(&row).Error
return findResultWithError(&row,err)
1 year ago
}
{{end}}