2014-08-08 17:37:55 +00:00
|
|
|
// Copyright 2013 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-07-31 21:30:35 +00:00
|
|
|
"fmt"
|
2014-08-08 17:37:55 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// article
|
|
|
|
// cms/article
|
|
|
|
//
|
2016-07-31 21:30:35 +00:00
|
|
|
func generateController(cname, currpath string) {
|
|
|
|
w := NewColorWriter(os.Stdout)
|
|
|
|
|
2014-08-08 17:37:55 +00:00
|
|
|
p, f := path.Split(cname)
|
|
|
|
controllerName := strings.Title(f)
|
|
|
|
packageName := "controllers"
|
2016-07-31 21:30:35 +00:00
|
|
|
|
2014-08-08 17:37:55 +00:00
|
|
|
if p != "" {
|
|
|
|
i := strings.LastIndex(p[:len(p)-1], "/")
|
|
|
|
packageName = p[i+1 : len(p)-1]
|
|
|
|
}
|
2016-07-31 21:30:35 +00:00
|
|
|
|
2014-08-08 17:48:40 +00:00
|
|
|
ColorLog("[INFO] Using '%s' as controller name\n", controllerName)
|
|
|
|
ColorLog("[INFO] Using '%s' as package name\n", packageName)
|
2016-07-31 21:30:35 +00:00
|
|
|
|
|
|
|
fp := path.Join(currpath, "controllers", p)
|
2014-08-08 17:37:55 +00:00
|
|
|
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
2016-07-31 21:30:35 +00:00
|
|
|
// Create the controller's directory
|
2014-08-08 17:37:55 +00:00
|
|
|
if err := os.MkdirAll(fp, 0777); err != nil {
|
|
|
|
ColorLog("[ERRO] Could not create controllers directory: %s\n", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|
2016-07-31 21:30:35 +00:00
|
|
|
|
2014-08-08 17:37:55 +00:00
|
|
|
fpath := path.Join(fp, strings.ToLower(controllerName)+".go")
|
|
|
|
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
|
2016-07-31 21:30:35 +00:00
|
|
|
defer CloseFile(f)
|
|
|
|
|
|
|
|
modelPath := path.Join(currpath, "models", strings.ToLower(controllerName)+".go")
|
|
|
|
|
2015-07-16 17:36:02 +00:00
|
|
|
var content string
|
|
|
|
if _, err := os.Stat(modelPath); err == nil {
|
|
|
|
ColorLog("[INFO] Using matching model '%s'\n", controllerName)
|
|
|
|
content = strings.Replace(controllerModelTpl, "{{packageName}}", packageName, -1)
|
2016-07-31 21:30:35 +00:00
|
|
|
pkgPath := getPackagePath(currpath)
|
2015-07-16 17:36:02 +00:00
|
|
|
content = strings.Replace(content, "{{pkgPath}}", pkgPath, -1)
|
|
|
|
} else {
|
|
|
|
content = strings.Replace(controllerTpl, "{{packageName}}", packageName, -1)
|
|
|
|
}
|
2016-07-31 21:30:35 +00:00
|
|
|
|
2014-08-08 17:37:55 +00:00
|
|
|
content = strings.Replace(content, "{{controllerName}}", controllerName, -1)
|
|
|
|
f.WriteString(content)
|
2016-07-31 21:30:35 +00:00
|
|
|
|
|
|
|
// Run 'gofmt' on the generated source code
|
2014-08-08 17:37:55 +00:00
|
|
|
formatSourceCode(fpath)
|
2016-07-31 21:30:35 +00:00
|
|
|
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", fpath, "\x1b[0m")
|
2014-08-08 17:37:55 +00:00
|
|
|
} else {
|
|
|
|
ColorLog("[ERRO] Could not create controller file: %s\n", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var controllerTpl = `package {{packageName}}
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
)
|
|
|
|
|
2015-04-26 02:26:26 +00:00
|
|
|
// operations for {{controllerName}}
|
2014-08-08 17:37:55 +00:00
|
|
|
type {{controllerName}}Controller struct {
|
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
2014-11-05 14:48:09 +00:00
|
|
|
func (c *{{controllerName}}Controller) URLMapping() {
|
|
|
|
c.Mapping("Post", c.Post)
|
|
|
|
c.Mapping("GetOne", c.GetOne)
|
|
|
|
c.Mapping("GetAll", c.GetAll)
|
|
|
|
c.Mapping("Put", c.Put)
|
|
|
|
c.Mapping("Delete", c.Delete)
|
2014-08-18 04:21:21 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title Create
|
2014-08-08 17:37:55 +00:00
|
|
|
// @Description create {{controllerName}}
|
|
|
|
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
|
2015-10-16 15:44:11 +00:00
|
|
|
// @Success 201 {object} models.{{controllerName}}
|
2014-08-08 17:37:55 +00:00
|
|
|
// @Failure 403 body is empty
|
|
|
|
// @router / [post]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (c *{{controllerName}}Controller) Post() {
|
2014-08-08 17:37:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title GetOne
|
2014-08-08 17:37:55 +00:00
|
|
|
// @Description get {{controllerName}} by id
|
|
|
|
// @Param id path string true "The key for staticblock"
|
|
|
|
// @Success 200 {object} models.{{controllerName}}
|
|
|
|
// @Failure 403 :id is empty
|
|
|
|
// @router /:id [get]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (c *{{controllerName}}Controller) GetOne() {
|
2014-08-08 17:37:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title GetAll
|
2014-08-08 17:37:55 +00:00
|
|
|
// @Description get {{controllerName}}
|
|
|
|
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
|
|
|
|
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
|
|
|
|
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
|
|
|
|
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
|
|
|
|
// @Param limit query string false "Limit the size of result set. Must be an integer"
|
|
|
|
// @Param offset query string false "Start position of result set. Must be an integer"
|
|
|
|
// @Success 200 {object} models.{{controllerName}}
|
|
|
|
// @Failure 403
|
|
|
|
// @router / [get]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (c *{{controllerName}}Controller) GetAll() {
|
2014-08-08 17:37:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Update
|
|
|
|
// @Description update the {{controllerName}}
|
|
|
|
// @Param id path string true "The id you want to update"
|
|
|
|
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
|
|
|
|
// @Success 200 {object} models.{{controllerName}}
|
|
|
|
// @Failure 403 :id is not int
|
|
|
|
// @router /:id [put]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (c *{{controllerName}}Controller) Put() {
|
2015-10-16 15:44:11 +00:00
|
|
|
|
2014-08-08 17:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Delete
|
|
|
|
// @Description delete the {{controllerName}}
|
|
|
|
// @Param id path string true "The id you want to delete"
|
|
|
|
// @Success 200 {string} delete success!
|
|
|
|
// @Failure 403 id is empty
|
|
|
|
// @router /:id [delete]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (c *{{controllerName}}Controller) Delete() {
|
2015-10-16 15:44:11 +00:00
|
|
|
|
2014-08-08 17:37:55 +00:00
|
|
|
}
|
|
|
|
`
|
2015-07-16 17:36:02 +00:00
|
|
|
|
|
|
|
var controllerModelTpl = `package {{packageName}}
|
|
|
|
|
|
|
|
import (
|
|
|
|
"{{pkgPath}}/models"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
)
|
|
|
|
|
|
|
|
// oprations for {{controllerName}}
|
|
|
|
type {{controllerName}}Controller struct {
|
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *{{controllerName}}Controller) URLMapping() {
|
|
|
|
c.Mapping("Post", c.Post)
|
|
|
|
c.Mapping("GetOne", c.GetOne)
|
|
|
|
c.Mapping("GetAll", c.GetAll)
|
|
|
|
c.Mapping("Put", c.Put)
|
|
|
|
c.Mapping("Delete", c.Delete)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Post
|
|
|
|
// @Description create {{controllerName}}
|
|
|
|
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
|
2015-10-16 15:44:11 +00:00
|
|
|
// @Success 201 {int} models.{{controllerName}}
|
2015-07-16 17:36:02 +00:00
|
|
|
// @Failure 403 body is empty
|
|
|
|
// @router / [post]
|
|
|
|
func (c *{{controllerName}}Controller) Post() {
|
|
|
|
var v models.{{controllerName}}
|
|
|
|
json.Unmarshal(c.Ctx.Input.RequestBody, &v)
|
2015-10-16 15:44:11 +00:00
|
|
|
if _, err := models.Add{{controllerName}}(&v); err == nil {
|
|
|
|
c.Ctx.Output.SetStatus(201)
|
|
|
|
c.Data["json"] = v
|
2015-07-16 17:36:02 +00:00
|
|
|
} else {
|
|
|
|
c.Data["json"] = err.Error()
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
c.ServeJSON()
|
2015-07-16 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Get
|
|
|
|
// @Description get {{controllerName}} by id
|
|
|
|
// @Param id path string true "The key for staticblock"
|
|
|
|
// @Success 200 {object} models.{{controllerName}}
|
|
|
|
// @Failure 403 :id is empty
|
|
|
|
// @router /:id [get]
|
|
|
|
func (c *{{controllerName}}Controller) GetOne() {
|
2016-01-06 03:55:56 +00:00
|
|
|
idStr := c.Ctx.Input.Param(":id")
|
2015-07-16 17:36:02 +00:00
|
|
|
id, _ := strconv.ParseInt(idStr, 0, 64)
|
|
|
|
v, err := models.Get{{controllerName}}ById(id)
|
|
|
|
if err != nil {
|
|
|
|
c.Data["json"] = err.Error()
|
|
|
|
} else {
|
|
|
|
c.Data["json"] = v
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
c.ServeJSON()
|
2015-07-16 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Get All
|
|
|
|
// @Description get {{controllerName}}
|
|
|
|
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
|
|
|
|
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
|
|
|
|
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
|
|
|
|
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
|
|
|
|
// @Param limit query string false "Limit the size of result set. Must be an integer"
|
|
|
|
// @Param offset query string false "Start position of result set. Must be an integer"
|
|
|
|
// @Success 200 {object} models.{{controllerName}}
|
2015-10-16 15:44:11 +00:00
|
|
|
// @Failure 403
|
2015-07-16 17:36:02 +00:00
|
|
|
// @router / [get]
|
|
|
|
func (c *{{controllerName}}Controller) GetAll() {
|
|
|
|
var fields []string
|
|
|
|
var sortby []string
|
|
|
|
var order []string
|
|
|
|
var query map[string]string = make(map[string]string)
|
|
|
|
var limit int64 = 10
|
|
|
|
var offset int64 = 0
|
|
|
|
|
|
|
|
// fields: col1,col2,entity.col3
|
|
|
|
if v := c.GetString("fields"); v != "" {
|
|
|
|
fields = strings.Split(v, ",")
|
|
|
|
}
|
|
|
|
// limit: 10 (default is 10)
|
|
|
|
if v, err := c.GetInt64("limit"); err == nil {
|
|
|
|
limit = v
|
|
|
|
}
|
|
|
|
// offset: 0 (default is 0)
|
|
|
|
if v, err := c.GetInt64("offset"); err == nil {
|
|
|
|
offset = v
|
|
|
|
}
|
|
|
|
// sortby: col1,col2
|
|
|
|
if v := c.GetString("sortby"); v != "" {
|
|
|
|
sortby = strings.Split(v, ",")
|
|
|
|
}
|
|
|
|
// order: desc,asc
|
|
|
|
if v := c.GetString("order"); v != "" {
|
|
|
|
order = strings.Split(v, ",")
|
|
|
|
}
|
|
|
|
// query: k:v,k:v
|
|
|
|
if v := c.GetString("query"); v != "" {
|
|
|
|
for _, cond := range strings.Split(v, ",") {
|
2016-08-18 14:46:11 +00:00
|
|
|
kv := strings.SplitN(cond, ":", 2)
|
2015-07-16 17:36:02 +00:00
|
|
|
if len(kv) != 2 {
|
|
|
|
c.Data["json"] = errors.New("Error: invalid query key/value pair")
|
2015-10-22 03:32:21 +00:00
|
|
|
c.ServeJSON()
|
2015-07-16 17:36:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
k, v := kv[0], kv[1]
|
|
|
|
query[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := models.GetAll{{controllerName}}(query, fields, sortby, order, offset, limit)
|
|
|
|
if err != nil {
|
|
|
|
c.Data["json"] = err.Error()
|
|
|
|
} else {
|
|
|
|
c.Data["json"] = l
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
c.ServeJSON()
|
2015-07-16 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Update
|
|
|
|
// @Description update the {{controllerName}}
|
|
|
|
// @Param id path string true "The id you want to update"
|
|
|
|
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
|
|
|
|
// @Success 200 {object} models.{{controllerName}}
|
|
|
|
// @Failure 403 :id is not int
|
|
|
|
// @router /:id [put]
|
|
|
|
func (c *{{controllerName}}Controller) Put() {
|
2016-01-06 03:55:56 +00:00
|
|
|
idStr := c.Ctx.Input.Param(":id")
|
2015-07-16 17:36:02 +00:00
|
|
|
id, _ := strconv.ParseInt(idStr, 0, 64)
|
|
|
|
v := models.{{controllerName}}{Id: id}
|
|
|
|
json.Unmarshal(c.Ctx.Input.RequestBody, &v)
|
|
|
|
if err := models.Update{{controllerName}}ById(&v); err == nil {
|
|
|
|
c.Data["json"] = "OK"
|
|
|
|
} else {
|
|
|
|
c.Data["json"] = err.Error()
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
c.ServeJSON()
|
2015-07-16 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Delete
|
|
|
|
// @Description delete the {{controllerName}}
|
|
|
|
// @Param id path string true "The id you want to delete"
|
|
|
|
// @Success 200 {string} delete success!
|
|
|
|
// @Failure 403 id is empty
|
|
|
|
// @router /:id [delete]
|
|
|
|
func (c *{{controllerName}}Controller) Delete() {
|
2016-01-06 03:55:56 +00:00
|
|
|
idStr := c.Ctx.Input.Param(":id")
|
2015-07-16 17:36:02 +00:00
|
|
|
id, _ := strconv.ParseInt(idStr, 0, 64)
|
|
|
|
if err := models.Delete{{controllerName}}(id); err == nil {
|
|
|
|
c.Data["json"] = "OK"
|
|
|
|
} else {
|
|
|
|
c.Data["json"] = err.Error()
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
c.ServeJSON()
|
2015-07-16 17:36:02 +00:00
|
|
|
}
|
|
|
|
`
|