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
86 lines
3.3 KiB
package model |
|
{{if .Imports}} |
|
import ( |
|
{{range $val:= .Imports}} |
|
"{{$val}}"{{end}} |
|
) |
|
{{end}} |
|
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 { |
|
updates map[string]any |
|
{{range $val := .Columns}}{{$val.Name|UpperType}} {{$val.GoType}} `db:"{{$val.Name}}"`{{if $val.Comment}}//{{$val.Comment}}{{end}} |
|
{{end}} |
|
} |
|
default{{.Name|UpperType}}Model struct { |
|
db *gorm.DB |
|
} |
|
) |
|
func (s *{{.Name|UpperType}}) TableName() string { |
|
return "{{.Name}}" |
|
} |
|
|
|
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 |
|
} |
|
|
|
|
|
|
|
func new{{.Name|UpperType}}DAO(db *gorm.DB) *default{{.Name|UpperType}}Model { |
|
return &default{{.Name|UpperType}}Model{ |
|
db: db, |
|
} |
|
} |
|
|
|
func (s *default{{.Name|UpperType}}Model) Insert(ctx context.Context, data *{{.Name|UpperType}}) error { |
|
return s.db.Create(data).Error |
|
} |
|
{{ if .Primary}} |
|
func (s *default{{.Name|UpperType}}Model) Update(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}, updates map[string]any) error { |
|
return s.db.Model({{.Name|UpperType}}{}).Where("{{.Primary.Name}}=?", {{.Primary.Name|LowerType}}).Updates(updates).Error |
|
} |
|
|
|
|
|
func (s *default{{.Name|UpperType}}Model) Delete(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}) error { |
|
return s.db.Where("{{.Primary.Name}}=?", {{.Primary.Name|LowerType}}).Delete(&{{.Name|UpperType}}{}).Error |
|
} |
|
|
|
|
|
func (s *default{{.Name|UpperType}}Model) FindOne(ctx context.Context, {{.Primary.Name|LowerType}} {{.Primary.GoType}}) (*{{.Name|UpperType}}, error) { |
|
row := {{.Name|UpperType}}{} |
|
err := s.db.Where("{{.Primary.Name}}=?", {{.Primary.Name|LowerType}}).Find(&row).Error |
|
return findResultWithError(&row,err) |
|
} |
|
{{end}} |
|
|
|
{{range $val:= .Indexes}} |
|
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) |
|
} |
|
{{end}}
|
|
|