Jelajahi Sumber

feat: 删除分类数据

runningwater 6 bulan lalu
induk
melakukan
2bb1c5830e
3 mengubah file dengan 20 tambahan dan 28 penghapusan
  1. 16 21
      app/http/controllers/api/v1/categories_controller.go
  2. 3 7
      gohub.http
  3. 1 0
      routes/api.go

+ 16 - 21
app/http/controllers/api/v1/categories_controller.go

@@ -71,24 +71,19 @@ func (ctrl *CategoriesController) Update(c *gin.Context) {
 	}
 }
 
-// func (ctrl *CategoriesController) Delete(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
-//     }
-//
-//     rowsAffected := categoryModel.Delete()
-//     if rowsAffected > 0 {
-//         response.Success(c)
-//         return
-//     }
-//
-//     response.Abort500(c, "删除失败,请稍后尝试~")
-// }
+func (ctrl *CategoriesController) Delete(c *gin.Context) {
+
+	categoryModel := category.Get(c.Param("id"))
+	if categoryModel.ID == 0 {
+		response.Abort404(c)
+		return
+	}
+
+	rowsAffected := categoryModel.Delete()
+	if rowsAffected > 0 {
+		response.Success(c)
+		return
+	}
+
+	response.Abort500(c, "删除失败,请稍后尝试~")
+}

+ 3 - 7
gohub.http

@@ -93,7 +93,7 @@ GET {{base_url}}/v1/auth/user HTTP/1.1
 Authorization: Bearer {{access_token}}
 
 
-### users
+### 用户列表
 GET {{base_url}}/v1/users HTTP/1.1
 
 ### 创建分类
@@ -121,10 +121,6 @@ GET {{base_url}}/v1/categories?page=2&sort=id&order=asc&per_page=10 HTTP/1.1
 Content-Type: application/json
 
 ### 删除分类
-DELETE {{base_url}}/v1/categories HTTP/1.1
+DELETE {{base_url}}/v1/categories/1 HTTP/1.1
 Authorization: Bearer {{access_token}}
-Content-Type: application/json
-
-{
-  "id": 1
-}
+Content-Type: application/json

+ 1 - 0
routes/api.go

@@ -65,6 +65,7 @@ func RegisterAPIRoutes(router *gin.Engine) {
 			categoryGroup.GET("", cgc.Index)
 			categoryGroup.POST("", middlewares.AuthJWT(), cgc.Save)
 			categoryGroup.PUT("/:id", middlewares.AuthJWT(), cgc.Update)
+			categoryGroup.DELETE("/:id", middlewares.AuthJWT(), cgc.Delete)
 		}
 	}
 }