فهرست منبع

feat(command): make apicontroller 命令

runningwater 7 ماه پیش
والد
کامیت
d4a68c6562
4فایلهای تغییر یافته به همراه138 افزوده شده و 3 حذف شده
  1. 1 0
      app/cmd/make/make.go
  2. 36 0
      app/cmd/make/make_apicontroller.go
  3. 100 0
      app/cmd/make/tpls/apicontroller.tpl
  4. 1 3
      cliff.toml

+ 1 - 0
app/cmd/make/make.go

@@ -69,6 +69,7 @@ func init() {
 	CmdMake.AddCommand(
 		CmdMakeCMD,
 		CmdMakeModel,
+		CmdMakeAPIController,
 	)
 }
 

+ 36 - 0
app/cmd/make/make_apicontroller.go

@@ -0,0 +1,36 @@
+package make
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/runningwater/gohub/pkg/console"
+	"github.com/spf13/cobra"
+)
+
+var CmdMakeAPIController = &cobra.Command{
+	Use:   "apicontroller",
+	Short: "Create api controller file, example: make apicontroller v1/user",
+	Run:   runMakeAPIController,
+	Args:  cobra.ExactArgs(1), // 只允许且必须传 1 个参数
+}
+
+func runMakeAPIController(cmd *cobra.Command, args []string) {
+
+	// 1. 获取参数,处理参数,要求附带版本号(v1 或 v2)
+	array := strings.Split(args[0], "/")
+	if len(array) != 2 {
+		console.Exit("api controller file format: v1/user")
+	}
+
+	// 2. 拼接目标文件路径
+	// 如:app/http/controllers/api/v1/user_controller.go
+	apiVersion, name := array[0], array[1]
+	model := makeModelFromString(name)
+
+	// 3. 拼接目标文件路径
+	// 如:app/http/controllers/api/v1/user_controller.go
+	filePath := fmt.Sprintf("app/http/controllers/api/%s/%s_controller.go", apiVersion, model.TableName)
+	// 4. 基于模板生成文件
+	createFileFromStub(filePath, "apicontroller", model)
+}

+ 100 - 0
app/cmd/make/tpls/apicontroller.tpl

@@ -0,0 +1,100 @@
+package v1
+
+import (
+    "github.com/runningwater/gohub/app/models/{{PackageName}}"
+    "github.com/runningwater/gohub/app/policies"
+    "github.com/runningwater/gohub/app/requests"
+    "github.com/runningwater/gohub/pkg/response"
+
+    "github.com/gin-gonic/gin"
+)
+
+type {{StructNamePlural}}Controller struct {
+    BaseApiController
+}
+
+func (ctrl *{{StructNamePlural}}Controller) Index(c *gin.Context) {
+    {{VariableNamePlural}} := {{PackageName}}.All()
+    response.Data(c, {{VariableNamePlural}})
+}
+
+func (ctrl *{{StructNamePlural}}Controller) Show(c *gin.Context) {
+    {{VariableName}}Model := {{PackageName}}.Get(c.Param("id"))
+    if {{VariableName}}Model.ID == 0 {
+        response.Abort404(c)
+        return
+    }
+    response.Data(c, {{VariableName}}Model)
+}
+
+func (ctrl *{{StructNamePlural}}Controller) Store(c *gin.Context) {
+
+    request := requests.{{StructName}}Request{}
+    if ok := requests.Validate(c, &request, requests.{{StructName}}Save); !ok {
+        return
+    }
+
+    {{VariableName}}Model := {{PackageName}}.{{StructName}}{
+        FieldName:      request.FieldName,
+    }
+    {{VariableName}}Model.Create()
+    if {{VariableName}}Model.ID > 0 {
+        response.Created(c, {{VariableName}}Model)
+    } else {
+        response.Abort500(c, "创建失败,请稍后尝试~")
+    }
+}
+
+func (ctrl *{{StructNamePlural}}Controller) Update(c *gin.Context) {
+
+    {{VariableName}}Model := {{PackageName}}.Get(c.Param("id"))
+    if {{VariableName}}Model.ID == 0 {
+        response.Abort404(c)
+        return
+    }
+
+    if ok := policies.CanModify{{StructName}}(c, {{VariableName}}Model); !ok {
+        response.Abort403(c)
+        return
+    }
+
+    request := requests.{{StructName}}Request{}
+    bindOk, errs := requests.Validate(c, &request, requests.{{StructName}}Save)
+    if !bindOk {
+        return
+    }
+    if len(errs) > 0 {
+        response.ValidationError(c, errs)
+        return
+    }
+
+    {{VariableName}}Model.FieldName = request.FieldName
+    rowsAffected := {{VariableName}}Model.Save()
+    if rowsAffected > 0 {
+        response.Data(c, {{VariableName}}Model)
+    } else {
+        response.Abort500(c, "更新失败,请稍后尝试~")
+    }
+}
+
+func (ctrl *{{StructNamePlural}}Controller) Delete(c *gin.Context) {
+
+    {{VariableName}}Model := {{PackageName}}.Get(c.Param("id"))
+    if {{VariableName}}Model.ID == 0 {
+        response.Abort404(c)
+        return
+    }
+
+    if ok := policies.CanModify{{StructName}}(c, {{VariableName}}Model); !ok {
+        response.Abort403(c)
+        return
+    }
+
+    rowsAffected := {{VariableName}}Model.Delete()
+    if rowsAffected > 0 {
+        response.Success(c)
+        return
+    }
+
+    response.Abort500(c, "删除失败,请稍后尝试~")
+}

+ 1 - 3
cliff.toml

@@ -29,8 +29,6 @@ Web 服务功能会封装到子命令 serve 中,命令行功能会封装到子
 body = """
 {% if version %}\
     ### [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }}
-{% else %}\
-    ### [未发布]
 {% endif %}\
 {% for group, commits in commits | group_by(attribute="group") %}
     #### {{ group | striptags | trim | upper_first }}
@@ -56,7 +54,7 @@ postprocessors = [
 ]
 
 # 即使没有发布版本也渲染内容
-# render_always = true
+render_always = true
 
 # 输出文件路径
 output = "CHANGELOG.md"