3 Commit-ok 7c51b9b432 ... d8ad850fc6

Szerző SHA1 Üzenet Dátum
  runningwater d8ad850fc6 feat(command): make policy 命令 3 hónapja
  runningwater 1342673076 feat: 授权策略 3 hónapja
  runningwater cd06cecfa0 feat: 话题更新接口 3 hónapja

+ 11 - 0
README.md

@@ -52,6 +52,8 @@ UNIQUE KEY `migration` (`migration`)
 
 #### 🐛 Bug 修复
 
+- Fix typing error
+- Fix automigrate charset for mysql
 - Golangci-lint run fix
 - Readme.md
 - 删除测试的 testCommand
@@ -74,6 +76,15 @@ UNIQUE KEY `migration` (`migration`)
 
 #### 🚀 新功能
 
+- 话题更新接口
+- 创建话题接口
+- 话题模型和迁移
+- 删除分类数据
+- 分类列表
+- *(Command)* Seed 命令
+- 更新分类
+- 创建分类
+- 模式模板添加分页功能
 - 分页请求参数校验
 - 用户列表分页
 - Paginator 分页功能

+ 4 - 2
app/cmd/make/make.go

@@ -4,10 +4,11 @@ import (
 	"embed"
 	"fmt"
 
+	"github.com/spf13/cobra"
+
 	"github.com/runningwater/gohub/pkg/console"
 	"github.com/runningwater/gohub/pkg/file"
 	"github.com/runningwater/gohub/pkg/str"
-	"github.com/spf13/cobra"
 )
 
 // Model 参数解释
@@ -54,7 +55,7 @@ type Model struct {
 	PackageName        string
 }
 
-// tplsFS 方便我们后面打包的 .stub 为后缀的文件
+// tplsFS 方便我们后面打包的 .tpl 为后缀的文件
 //
 //go:embed tpls
 var tplsFS embed.FS
@@ -74,6 +75,7 @@ func init() {
 		CmdMakeMigration,
 		CmdMakeFactory,
 		CmdMakeSeeder,
+		CmdMakePolicy,
 	)
 }
 

+ 30 - 0
app/cmd/make/make_policy.go

@@ -0,0 +1,30 @@
+package make
+
+import (
+	"fmt"
+	"os"
+
+	"github.com/spf13/cobra"
+
+	"github.com/runningwater/gohub/pkg/console"
+)
+
+var CmdMakePolicy = &cobra.Command{
+	Use:   "policy",
+	Short: "Create policy file, example: make policy user",
+	Run:   runMakePolicy,
+	Args:  cobra.ExactArgs(1), // 只允许且必须传 1 个参数
+}
+
+func runMakePolicy(cmd *cobra.Command, args []string) {
+	model := makeModelFromString(args[0])
+
+	err := os.MkdirAll("app/policies", os.ModePerm)
+	if err != nil {
+		console.Error(err.Error())
+		return
+	}
+	filePath := fmt.Sprintf("app/policies/%s_policy.go", model.PackageName)
+
+	createFileFromTpl(filePath, "policy", model)
+}

+ 17 - 0
app/cmd/make/tpls/policy.tpl

@@ -0,0 +1,17 @@
+package policies
+
+import (
+    "github.com/runningwater/gohub/app/models/{{PackageName}}"
+    "github.com/runningwater/gohub/pkg/auth"
+
+    "github.com/gin-gonic/gin"
+)
+
+func CanModify{{StructName}}(c *gin.Context, {{VariableName}}Model {{PackageName}}.{{StructName}}) bool {
+    return auth.CurrentUID(c) == {{VariableName}}Model.UserID
+}
+
+// func CanView{{StructName}}(c *gin.Context, {{VariableName}}Model {{PackageName}}.{{StructName}}) bool {}
+// func CanCreate{{StructName}}(c *gin.Context, {{VariableName}}Model {{PackageName}}.{{StructName}}) bool {}
+// func CanUpdate{{StructName}}(c *gin.Context, {{VariableName}}Model {{PackageName}}.{{StructName}}) bool {}
+// func CanDelete{{StructName}}(c *gin.Context, {{VariableName}}Model {{PackageName}}.{{StructName}}) bool {}

+ 32 - 0
app/http/controllers/api/v1/topics_controller.go

@@ -2,6 +2,7 @@ package v1
 
 import (
 	"github.com/runningwater/gohub/app/models/topic"
+	"github.com/runningwater/gohub/app/policies"
 	"github.com/runningwater/gohub/app/requests"
 	"github.com/runningwater/gohub/pkg/auth"
 	"github.com/runningwater/gohub/pkg/response"
@@ -38,3 +39,34 @@ func (ctrl *TopicsController) Store(c *gin.Context) {
 		response.Abort500(c, "创建失败,请稍后尝试~")
 	}
 }
+
+func (ctrl *TopicsController) Update(c *gin.Context) {
+	topicModel := topic.Get(c.Param("id"))
+	if topicModel.ID == 0 {
+		response.Abort404(c)
+		return
+	}
+
+	request := requests.TopicRequest{}
+	if ok := requests.Validate(c, &request, requests.TopicSave); !ok {
+		return
+	}
+
+	if !policies.CanModifyTopic(c, topicModel) {
+		response.Abort403(c)
+		return
+	}
+
+	topicModel.Title = request.Title
+	topicModel.Body = request.Body
+	topicModel.CategoryID = request.CategoryID
+	rowsAffected := topicModel.Save()
+	if rowsAffected > 0 {
+		response.Data(c, topicModel)
+	} else {
+		response.Abort500(c, "更新失败,请稍后尝试~")
+	}
+}
+
+func (ctrl *TopicsController) Delete(c *gin.Context) {
+}

+ 14 - 0
app/policies/topic_policy.go

@@ -0,0 +1,14 @@
+// Package policies 用户授权
+package policies
+
+import (
+	"github.com/gin-gonic/gin"
+
+	"github.com/runningwater/gohub/app/models/topic"
+	"github.com/runningwater/gohub/pkg/auth"
+)
+
+// CanModifyTopic 登陆用户是否可以修改此 topic
+func CanModifyTopic(c *gin.Context, _topic topic.Topic) bool {
+	return _topic.UserID == auth.CurrentUID(c)
+}

+ 11 - 0
gohub.http

@@ -135,3 +135,14 @@ Content-Type: application/json
   "body": "话题1内容, 这里是帖子描述内容帖子描述内容",
   "category_id": "3"
 }
+
+### 更新话题
+PUT {{base_url}}/v1/topics/1 HTTP/1.1
+Authorization: Bearer {{access_token}}
+Content-Type: application/json
+
+{
+  "title": "我的的帖子3",
+  "body": "话题1内容, 这里是帖子描述内容帖子描述内容",
+  "category_id": "3"
+}

+ 3 - 1
routes/api.go

@@ -1,4 +1,4 @@
-// Path: routes 注册路由
+// Package routes Path: routes 注册路由
 package routes
 
 import (
@@ -70,6 +70,8 @@ func RegisterAPIRoutes(router *gin.Engine) {
 		tpcGroup := v1.Group("/topics")
 		{
 			tpcGroup.POST("", middlewares.AuthJWT(), tpc.Store)
+			tpcGroup.PUT("/:id", middlewares.AuthJWT(), tpc.Update)
+			tpcGroup.DELETE("/:id", middlewares.AuthJWT(), tpc.Delete)
 		}
 	}
 }