Parcourir la source

feat(command): make request 命令

runningwater il y a 11 mois
Parent
commit
949876b0a2
4 fichiers modifiés avec 57 ajouts et 0 suppressions
  1. 2 0
      README.md
  2. 1 0
      app/cmd/make/make.go
  3. 21 0
      app/cmd/make/make_request.go
  4. 33 0
      app/cmd/make/tpls/request.tpl

+ 2 - 0
README.md

@@ -44,6 +44,7 @@ Web 服务功能会封装到子命令 serve 中,命令行功能会封装到子
 
 #### 🚀 新功能
 
+- *(command)* Make request 命令
 - *(command)* Make apicontroller 命令
 - 限流中间件
 - 添加 make cmd  命令 \r\n例如: gohub make cmd testCommand
@@ -63,6 +64,7 @@ Web 服务功能会封装到子命令 serve 中,命令行功能会封装到子
 
 #### 🚜 代码重构
 
+- *(doc)* Readme updated
 - 修改生成模板文件后缀名
 - Gofmt -l -w -s package
 

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

@@ -70,6 +70,7 @@ func init() {
 		CmdMakeCMD,
 		CmdMakeModel,
 		CmdMakeAPIController,
+		CmdMakeRequest,
 	)
 }
 

+ 21 - 0
app/cmd/make/make_request.go

@@ -0,0 +1,21 @@
+package make
+
+import (
+	"fmt"
+
+	"github.com/spf13/cobra"
+)
+
+var CmdMakeRequest = &cobra.Command{
+	Use:   "request",
+	Short: "Create request file, example: make request user",
+	Run:   runMakeRequest,
+	Args:  cobra.ExactArgs(1), // 只允许且必须传 1 个参数
+}
+
+func runMakeRequest(cmd *cobra.Command, args []string) {
+
+	model := makeModelFromString(args[0])
+	filePath := fmt.Sprintf("app/requests/%s_request.go", model.PackageName)
+	createFileFromStub(filePath, "request", model)
+}

+ 33 - 0
app/cmd/make/tpls/request.tpl

@@ -0,0 +1,33 @@
+package requests
+
+import (
+    "github.com/gin-gonic/gin"
+    "github.com/thedevsaddam/govalidator"
+)
+
+type {{StructName}}Request struct {
+    // Name        string `valid:"name" json:"name"`
+    // Description string `valid:"description" json:"description,omitempty"`
+    // TODO: FIXME()
+}
+
+func {{StructName}}Save(data any, c *gin.Context) map[string][]string {
+
+    rules := govalidator.MapData{
+        // "name":        []string{"required", "min_cn:2", "max_cn:8", "not_exists:{{TableName}},name"},
+        // "description": []string{"min_cn:3", "max_cn:255"},
+    }
+    messages := govalidator.MapData{
+        // "name": []string{
+        //     "required:名称为必填项",
+        //     "min_cn:名称长度需至少 2 个字",
+        //     "max_cn:名称长度不能超过 8 个字",
+        //     "not_exists:名称已存在",
+        // },
+        // "description": []string{
+        //     "min_cn:描述长度需至少 3 个字",
+        //     "max_cn:描述长度不能超过 255 个字",
+        // },
+    }
+    return validate(data, rules, messages)
+}