Skip to content

Commit

Permalink
feat: 后台增加拖拽排序功能 #48
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Dec 8, 2024
1 parent 14252f6 commit ed472c0
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 145 deletions.
37 changes: 37 additions & 0 deletions api-website/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -585,5 +585,42 @@ paths:
schema:
$ref: "#/components/schemas/StandardResponse"

/api/admin/tools/sort:
put:
summary: 批量更新工具排序
security:
- BearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: integer
sort:
type: integer
responses:
"200":
description: 更新成功
content:
application/json:
schema:
$ref: "#/components/schemas/StandardResponse"
"400":
description: 请求参数错误
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
"500":
description: 服务器内部错误
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
security:
- BearerAuth: []
27 changes: 27 additions & 0 deletions handler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,30 @@ func ManifastHanlder(c *gin.Context) {
"background_color": "#ffffff",
})
}

func UpdateToolsSortHandler(c *gin.Context) {
var updates []types.UpdateToolsSortDto
if err := c.ShouldBindJSON(&updates); err != nil {
utils.CheckErr(err)
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"errorMessage": err.Error(),
})
return
}

err := service.UpdateToolsSort(updates)
if err != nil {
utils.CheckErr(err)
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"errorMessage": err.Error(),
})
return
}

c.JSON(200, gin.H{
"success": true,
"message": "更新排序成功",
})
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func main() {
admin.POST("/tool", handler.AddToolHandler)
admin.DELETE("/tool/:id", handler.DeleteToolHandler)
admin.PUT("/tool/:id", handler.UpdateToolHandler)
admin.PUT("/tools/sort", handler.UpdateToolsSortHandler)

admin.POST("/catelog", handler.AddCatelogHandler)
admin.DELETE("/catelog/:id", handler.DeleteCatelogHandler)
Expand Down
24 changes: 24 additions & 0 deletions service/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,27 @@ func UpdateToolIcon(id int64, logo string) {
utils.CheckErr(err)
UpdateImg(logo)
}
func UpdateToolsSort(updates []types.UpdateToolsSortDto) error {
tx, err := database.DB.Begin()
if err != nil {
return err
}

sql := `UPDATE nav_table SET sort = ? WHERE id = ?`
stmt, err := tx.Prepare(sql)
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()

for _, update := range updates {
_, err = stmt.Exec(update.Sort, update.Id)
if err != nil {
tx.Rollback()
return err
}
}

return tx.Commit()
}
4 changes: 4 additions & 0 deletions types/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ type AddToolDto struct {
Sort int `json:"sort"`
Hide bool `json:"hide"`
}
type UpdateToolsSortDto struct {
Id int `json:"id"`
Sort int `json:"sort"`
}
4 changes: 4 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"private": true,
"dependencies": {
"@ant-design/icons": "^5.5.2",
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/modifiers": "^9.0.0",
"@dnd-kit/sortable": "^10.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@radix-ui/react-icons": "^1.3.2",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.1.0",
Expand Down
72 changes: 72 additions & 0 deletions ui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ed472c0

Please sign in to comment.