From 39e7ce462d43f5903e3b34c439ab04052569f1e5 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Mon, 4 Aug 2014 15:46:46 +0800 Subject: [PATCH 1/3] added get all for models and controllers --- g_models.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/g_models.go b/g_models.go index 30e3a9a..de1db2b 100644 --- a/g_models.go +++ b/g_models.go @@ -624,6 +624,18 @@ func Get{{modelName}}ById(id int) (v *{{modelName}}, err error) { return nil, err } +// GetAll{{modelName}} retrieves all {{modelName}} matches certain condition. Returns empty list if +// no records exist +func GetAll{{modelName}}() (l []{{modelName}}, err error) { + o := orm.NewOrm() + t := new({{modelName}}) + qs := o.QueryTable(t) + if _, err := qs.All(&l); err == nil { + return l, nil + } + return nil, err +} + // Update{{modelName}} updates {{modelName}} by Id and returns error if // the record to be updated doesn't exist func Update{{modelName}}ById(m *{{modelName}}) (err error) { @@ -696,7 +708,23 @@ func (this *{{ctrlName}}Controller) GetOne() { this.ServeJson() } -// @Title update +// @Title Get All +// @Description get {{ctrlName}} +// @Param id path string true "get all records matches certain condition" +// @Success 200 {object} models.{{ctrlName}} +// @Failure 403 +// @router / [get] +func (this *{{ctrlName}}Controller) GetAll() { + l, err := models.GetAll{{ctrlName}}() + if err != nil { + this.Data["json"] = err.Error() + } else { + this.Data["json"] = l + } + this.ServeJson() +} + +// @Title Update // @Description update the {{ctrlName}} // @Param id path string true "The id you want to update" // @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content" @@ -716,7 +744,7 @@ func (this *{{ctrlName}}Controller) Put() { this.ServeJson() } -// @Title delete +// @Title Delete // @Description delete the {{ctrlName}} // @Param id path string true "The id you want to delete" // @Success 200 {string} delete success! From fc62745b4f6c836af0acb1e2bd30ce81d7aac5b0 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Mon, 4 Aug 2014 16:08:46 +0800 Subject: [PATCH 2/3] remove defer close file --- g_models.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/g_models.go b/g_models.go index de1db2b..f33cbcc 100644 --- a/g_models.go +++ b/g_models.go @@ -443,7 +443,6 @@ func writeModelFiles(tables []*Table, mPath string) { ColorLog("[ERRO] %v\n", err) os.Exit(2) } - defer f.Close() template := "" if tb.Pk == "" { template = STRUCT_MODEL_TPL @@ -456,6 +455,7 @@ func writeModelFiles(tables []*Table, mPath string) { ColorLog("[ERRO] Could not write model file to %s\n", fpath) os.Exit(2) } + f.Close() ColorLog("[INFO] model => %s\n", fpath) formatAndFixImports(fpath) } @@ -474,12 +474,12 @@ func writeControllerFiles(tables []*Table, cPath string) { ColorLog("[ERRO] %v\n", err) os.Exit(2) } - defer f.Close() fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1) if _, err := f.WriteString(fileStr); err != nil { ColorLog("[ERRO] Could not write controller file to %s\n", fpath) os.Exit(2) } + f.Close() ColorLog("[INFO] controller => %s\n", fpath) formatAndFixImports(fpath) } @@ -507,11 +507,11 @@ func writeRouterFile(tables []*Table, rPath string) { ColorLog("[ERRO] %v\n", err) os.Exit(2) } - defer f.Close() if _, err := f.WriteString(routerStr); err != nil { ColorLog("[ERRO] Could not write router file to %s\n", fpath) os.Exit(2) } + f.Close() ColorLog("[INFO] router => %s\n", fpath) formatAndFixImports(fpath) } From 7a4d5f27988d2617109bd1bc13d8bab7f2b8fed8 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Mon, 4 Aug 2014 16:33:39 +0800 Subject: [PATCH 3/3] do not exit while encouter a file that's already exist --- g_models.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/g_models.go b/g_models.go index f33cbcc..ee1b04c 100644 --- a/g_models.go +++ b/g_models.go @@ -440,8 +440,8 @@ func writeModelFiles(tables []*Table, mPath string) { fpath := path.Join(mPath, filename+".go") f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) if err != nil { - ColorLog("[ERRO] %v\n", err) - os.Exit(2) + ColorLog("[WARN] %v\n", err) + continue } template := "" if tb.Pk == "" { @@ -471,8 +471,8 @@ func writeControllerFiles(tables []*Table, cPath string) { fpath := path.Join(cPath, filename+".go") f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) if err != nil { - ColorLog("[ERRO] %v\n", err) - os.Exit(2) + ColorLog("[WARN] %v\n", err) + continue } fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1) if _, err := f.WriteString(fileStr); err != nil { @@ -504,8 +504,8 @@ func writeRouterFile(tables []*Table, rPath string) { routerStr = strings.Replace(routerStr, "{{projectName}}", projectName, 1) f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) if err != nil { - ColorLog("[ERRO] %v\n", err) - os.Exit(2) + ColorLog("[WARN] %v\n", err) + return } if _, err := f.WriteString(routerStr); err != nil { ColorLog("[ERRO] Could not write router file to %s\n", fpath)