Browse Source

feat: 更新分类

runningwater 6 months ago
parent
commit
e827194a7e
4 changed files with 52 additions and 38 deletions
  1. 27 33
      app/http/controllers/api/v1/categories_controller.go
  2. 19 0
      gohub.http
  3. 4 4
      pkg/captcha/store_redis.go
  4. 2 1
      routes/api.go

+ 27 - 33
app/http/controllers/api/v1/categories_controller.go

@@ -12,7 +12,7 @@ type CategoriesController struct {
 	BaseApiController
 }
 
-func (ctrl *CategoriesController) Store(c *gin.Context) {
+func (ctrl *CategoriesController) Save(c *gin.Context) {
 
 	request := requests.CategoryRequest{}
 	if ok := requests.Validate(c, &request, requests.CategorySave); !ok {
@@ -31,38 +31,32 @@ func (ctrl *CategoriesController) Store(c *gin.Context) {
 	}
 }
 
-// func (ctrl *CategoriesController) Update(c *gin.Context) {
-//
-//     categoryModel := category.Get(c.Param("id"))
-//     if categoryModel.ID == 0 {
-//         response.Abort404(c)
-//         return
-//     }
-//
-//     if ok := policies.CanModifyCategory(c, categoryModel); !ok {
-//         response.Abort403(c)
-//         return
-//     }
-//
-//     request := requests.CategoryRequest{}
-//     bindOk, errs := requests.Validate(c, &request, requests.CategorySave)
-//     if !bindOk {
-//         return
-//     }
-//     if len(errs) > 0 {
-//         response.ValidationError(c, errs)
-//         return
-//     }
-//
-//     categoryModel.FieldName = request.FieldName
-//     rowsAffected := categoryModel.Save()
-//     if rowsAffected > 0 {
-//         response.Data(c, categoryModel)
-//     } else {
-//         response.Abort500(c, "更新失败,请稍后尝试~")
-//     }
-// }
-//
+func (ctrl *CategoriesController) Update(c *gin.Context) {
+
+	// 1. 验证 url 参数 id 是否正确
+	categoryModel := category.Get(c.Param("id"))
+	if categoryModel.ID == 0 {
+		response.Abort404(c)
+		return
+	}
+
+	// 2. 验证请求参数
+	request := requests.CategoryRequest{}
+	if ok := requests.Validate(c, &request, requests.CategorySave); !ok {
+		return
+	}
+
+	// 3. 更新数据
+	categoryModel.Name = request.Name
+	categoryModel.Description = request.Description
+	rowsAffected := categoryModel.Save()
+	if rowsAffected > 0 {
+		response.Data(c, categoryModel)
+	} else {
+		response.Abort500(c, "更新失败,请稍后尝试~")
+	}
+}
+
 // func (ctrl *CategoriesController) Delete(c *gin.Context) {
 //
 //     categoryModel := category.Get(c.Param("id"))

+ 19 - 0
gohub.http

@@ -105,3 +105,22 @@ Content-Type: application/json
     "name": "分类1",
     "description": "分类1描述"
 }
+
+### 更新分类
+PUT {{base_url}}/v1/categories/1 HTTP/1.1
+Authorization: Bearer {{access_token}}
+Content-Type: application/json
+
+{
+    "name": "新分类名称",
+    "description": "分类1描述"
+}
+
+### 删除分类
+DELETE {{base_url}}/v1/categories HTTP/1.1
+Authorization: Bearer {{access_token}}
+Content-Type: application/json
+
+{
+    "id": 1
+}

+ 4 - 4
pkg/captcha/store_redis.go

@@ -10,13 +10,13 @@ import (
 )
 
 // RedisStore is a store for captcha using Redis.
-// It implements the Store interface. base64Captcha.Store interface.
+// It implements the Save interface. base64Captcha.Store interface.
 type RedisStore struct {
 	RedisClient *redis.RedisClient
 	KeyPrefix   string
 }
 
-// Set 实现 base64Captcha.Store.Set 方法
+// Set 实现 base64Captcha.Save.Set 方法
 func (s *RedisStore) Set(key string, value string) error {
 	expiretime := time.Minute * time.Duration(config.GetInt64("captcha.expire_time"))
 	// 方便本地开发调试
@@ -30,7 +30,7 @@ func (s *RedisStore) Set(key string, value string) error {
 	return nil
 }
 
-// Get 实现 base64Captcha.Store.Get 方法
+// Get 实现 base64Captcha.Save.Get 方法
 func (s *RedisStore) Get(key string, clear bool) string {
 	key = s.KeyPrefix + key
 	val := s.RedisClient.Get(key)
@@ -40,7 +40,7 @@ func (s *RedisStore) Get(key string, clear bool) string {
 	return val
 }
 
-// Verify 实现 base64Captcha.Store.Verify 方法
+// Verify 实现 base64Captcha.Save.Verify 方法
 func (s *RedisStore) Verify(key, answer string, clear bool) bool {
 	v := s.Get(key, clear)
 	return v == answer

+ 2 - 1
routes/api.go

@@ -62,7 +62,8 @@ func RegisterAPIRoutes(router *gin.Engine) {
 		cgc := new(controllers.CategoriesController)
 		categoryGroup := v1.Group("/categories")
 		{
-			categoryGroup.POST("", middlewares.AuthJWT(), cgc.Store)
+			categoryGroup.POST("", middlewares.AuthJWT(), cgc.Save)
+			categoryGroup.PUT("/:id", middlewares.AuthJWT(), cgc.Update)
 		}
 	}
 }