1 Star 0 Fork 0

mchong/gin-admin-cli

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bll_impl.go 3.43 KB
一键复制 编辑 原始数据 按行查看 历史
Mchong 提交于 2019-09-09 22:34 +08:00 . bugfix
package generate
import (
"bytes"
"context"
"fmt"
"gitee.com/mchongjs/gin-admin-cli/util"
)
func getBllImplFileName(dir, name string) string {
fullname := fmt.Sprintf("%s/internal/app/bll/impl/internal/b_%s.go", dir, util.ToLowerUnderlinedNamer(name))
return fullname
}
// 生成bll实现文件
func genBllImpl(ctx context.Context, pkgName, dir string, item TplItem) error {
name := item.StructName
comment := item.Comment
hasselect := item.HasSelect
data := map[string]interface{}{
"PkgName": pkgName,
"Name": name,
"Comment": comment,
}
finalbuf := new(bytes.Buffer)
buf, err := execParseTpl(bllImplTpl, data)
if err != nil {
return err
}
finalbuf.Write(buf.Bytes())
buf.Reset()
if hasselect {
buf, err = execParseTpl(bllImplshortTpl, data)
if err != nil {
return err
}
finalbuf.Write(buf.Bytes())
buf.Reset()
}
buf, err = execParseTpl(bllImplTpl2, data)
if err != nil {
return err
}
finalbuf.Write(buf.Bytes())
buf.Reset()
fullname := getBllImplFileName(dir, name)
err = createFile(ctx, fullname, finalbuf)
if err != nil {
return err
}
fmt.Printf("文件[%s]写入成功\n", fullname)
return execGoFmt(fullname)
}
const bllImplTpl = `
package internal
import (
"context"
"{{.PkgName}}/internal/app/errors"
"{{.PkgName}}/internal/app/model"
"{{.PkgName}}/internal/app/schema"
)
// New{{.Name}} 创建{{.Comment}}
func New{{.Name}}(m{{.Name}} model.I{{.Name}}) *{{.Name}} {
return &{{.Name}}{
{{.Name}}Model: m{{.Name}},
}
}
// {{.Name}} {{.Comment}}业务逻辑
type {{.Name}} struct {
{{.Name}}Model model.I{{.Name}}
}
// Query 查询数据
func (a *{{.Name}}) Query(ctx context.Context, params schema.{{.Name}}QueryParam, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}QueryResult, error) {
return a.{{.Name}}Model.Query(ctx, params, opts...)
}
`
const bllImplshortTpl = `
// Select 选择数据(下拉用)
func (a *{{.Name}}) Select(ctx context.Context, params schema.{{.Name}}QueryParam) (*schema.{{.Name}}_shortQueryResult, error) {
return a.{{.Name}}Model.Select(ctx, params)
}
`
const bllImplTpl2 = `
// Get 查询指定数据
func (a *{{.Name}}) Get(ctx context.Context, recordID int, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}, error) {
item, err := a.{{.Name}}Model.Get(ctx, recordID, opts...)
if err != nil {
return nil, err
} else if item == nil {
return nil, errors.ErrNotFound
}
return item, nil
}
func (a *{{.Name}}) getUpdate(ctx context.Context, recordID int) (*schema.{{.Name}}, error) {
return a.Get(ctx, recordID)
}
// Create 创建数据
func (a *{{.Name}}) Create(ctx context.Context, item schema.{{.Name}}) (*schema.{{.Name}}, error) {
id, err := a.{{.Name}}Model.Create(ctx, item)
if err != nil {
return nil, err
}
return a.getUpdate(ctx, id)
}
// Update 更新数据
func (a *{{.Name}}) Update(ctx context.Context, recordID int, item schema.{{.Name}}) (*schema.{{.Name}}, error) {
oldItem, err := a.{{.Name}}Model.Get(ctx, recordID)
if err != nil {
return nil, err
} else if oldItem == nil {
return nil, errors.ErrNotFound
}
err = a.{{.Name}}Model.Update(ctx, recordID, item)
if err != nil {
return nil, err
}
return a.getUpdate(ctx, recordID)
}
// Delete 删除数据
func (a *{{.Name}}) Delete(ctx context.Context, recordID int) error {
oldItem, err := a.{{.Name}}Model.Get(ctx, recordID)
if err != nil {
return err
} else if oldItem == nil {
return errors.ErrNotFound
}
return a.{{.Name}}Model.Delete(ctx, recordID)
}
`
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mchongjs/gin-admin-cli.git
git@gitee.com:mchongjs/gin-admin-cli.git
mchongjs
gin-admin-cli
gin-admin-cli
a13bc20d66c4

搜索帮助