-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from chenqinghe/master
feature: support custom model templates
- Loading branch information
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ server: | |
host: 127.0.0.1 | ||
port: 8000 | ||
|
||
customTmpl: example.tpl | ||
|
||
genFilePath: ./ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package {{ .PackageName }} | ||
|
||
type {{ .ModelName }} struct { | ||
{{ .Fields }} | ||
} | ||
|
||
{{ if .Option.WithGormAnnotation }} | ||
// TableName sets the insert table name for this struct type | ||
func (model *{{ .ModelName }})TableName() string { | ||
return "{{ .Table.Name }}" | ||
} | ||
{{ end }} | ||
|
||
{{ if .Option.WithGormAnnotation }} | ||
func Add{{.ModelName}}(m *{{.ModelName}}) error { | ||
return db.Save(m).Error | ||
} | ||
|
||
func Delete{{.ModelName}}ByID(id int) (bool, error) { | ||
if err := db.Delete(&{{.ModelName}}{}, id).Error; err != nil { | ||
return false, err | ||
} | ||
return db.RowsAffected > 0, nil | ||
} | ||
|
||
func Delete{{.ModelName}}(condition string, args ...interface{}) (int64, error) { | ||
if err := db.Where(condition, args...).Delete(&{{.ModelName}}{}).Error; err != nil { | ||
return 0, err | ||
} | ||
return db.RowsAffected, nil | ||
} | ||
|
||
func Update{{.ModelName}}(m *{{.ModelName}}) error { | ||
return db.Save(m).Error | ||
} | ||
|
||
func Get{{.ModelName}}ByID(id int) (*{{.ModelName}}, error) { | ||
var m {{.ModelName}} | ||
if err := db.First(&m, id).Error; err != nil { | ||
return nil, err | ||
} | ||
return &m, nil | ||
} | ||
|
||
func Get{{.ModelName}}s(condition string, args ...interface{}) ([]*{{.ModelName}}, error) { | ||
res := make([]*{{.ModelName}}, 0) | ||
if err := db.Where(condition, args...).Find(&res).Error; err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
{{ end }} | ||
|
||
|
||
|