Skip to content

Commit

Permalink
完善菜品分类删除功能,但未测试
Browse files Browse the repository at this point in the history
  • Loading branch information
onenewcode committed Feb 26, 2024
1 parent 5183ba5 commit 537e942
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 1 deletion.
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,117 @@ return &cat, count
登陆,然后点击分类管理,效果如下。
![image](images/img_48.png)

## 根据id删除分类
![image](images/img_38.png)


### 代码开发
#### 根据id删除
**1). 添加路由**
在我们的根路由其中添加新增菜品的路由
>internal/router/router.go
```go
// 添加按照id删除
category.DELETE("", admin.DeleteCat)
```
**2). router逻辑添加**
在 category_router.go 中创建 PageCat 方法:
>internal/router/admin/category_router.go
```go
// 按照id删除菜品分类
// @Summary 新增菜品
// @Accept application/json
// @Produce application/json
// @router /admin/category [delete]
func DeleteCat(ctx context.Context, c *app.RequestContext) {
id := c.Param("id")
log.Printf("查询员工账号:{%s}", id)
id_r, _ := strconv.ParseInt(id, 10, 64)
if err:=service.DeleteCat(&id_r);err != nil {
log.Println(err)
c.JSON(http.StatusOK,common.Result{0,"",nil})
}else {
c.JSON(http.StatusOK,common.Result{1,"",nil})
}

}

```
**3). 添加service逻辑**
我们需要在我们的category_service.go添加相应功能。
>internal/router/service/category_service.go
```go
func DeleteCat(id *int64) *error {
err:=db.CatDao.Delete(id)
if err!=nil {
return err
}
return nil
}
```
**4). dao层**

在 category_dao.go文件中实现 PageQuery 方法:
>internal/db/category_dao.go
```go
func (*CategoryDao) Delete(id *int64) *error {
err := DBEngine.Delete(&model.Category{}, id).Error
if err != nil {
return &err
}
return nil
}
```

### 功能测试

#### 接口文档测试

测试**添加菜品分类分页查询功能**

我们再api工具在 访问 http://localhost:8080/admin/category/page/?page=1&pageSize=10&type=2 添加jwt令牌。
![image](images/img_47.png)
运行程序进行测试。
调试结果
```shell
{
"code": 1,
"msg": "",
"data": {
"total": 2,
"records": [
{
"id": 15,
"type": 2,
"name": "商务套餐",
"sort": 13,
"status": 1,
"create_time": "2022-06-09T22:14:10+08:00",
"update_time": "2022-06-10T11:04:48+08:00",
"create_user": 1,
"update_user": 1
},
{
"id": 13,
"type": 2,
"name": "人气套餐",
"sort": 12,
"status": 1,
"create_time": "2022-06-09T22:11:38+08:00",
"update_time": "2022-06-10T11:04:40+08:00",
"create_user": 1,
"update_user": 1
}
]
}
}
```



#### 前后端联调测试
登陆,然后点击分类管理,效果如下。
![image](images/img_48.png)
<a name="代码存在的问题"></a>
# 代码存在的问题
## 问题一 日期无法正常显示
Expand Down
7 changes: 7 additions & 0 deletions internal/db/category_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ func (*CategoryDao) PageQuery(page *dto.CategoryPageQueryDTO) (*[]model.Category
origin_sql.Limit(page.PageSize).Offset((page.Page - 1) * page.PageSize).Order("create_time desc").Find(&cat)
return &cat, count
}
func (*CategoryDao) Delete(id *int64) *error {
err := DBEngine.Delete(&model.Category{}, id).Error
if err != nil {
return &err
}
return nil
}
18 changes: 18 additions & 0 deletions internal/router/admin/category_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reggie/internal/models/dto"
"reggie/internal/models/model"
"reggie/internal/router/service"
"strconv"
"time"
)

Expand Down Expand Up @@ -41,5 +42,22 @@ func PageCat(ctx context.Context, c *app.RequestContext) {
log.Println("菜品分类查询", categoryPage)
cat := service.PageQueryDat(&categoryPage)
c.JSON(http.StatusOK, common.Result{1, "", cat})
}

// 按照id删除菜品分类
// @Summary 新增菜品
// @Accept application/json
// @Produce application/json
// @router /admin/category [delete]
func DeleteCat(ctx context.Context, c *app.RequestContext) {
id := c.Param("id")
log.Printf("查询员工账号:{%s}", id)
id_r, _ := strconv.ParseInt(id, 10, 64)
if err := service.DeleteCat(&id_r); err != nil {
log.Println(err)
c.JSON(http.StatusOK, common.Result{0, "", nil})
} else {
c.JSON(http.StatusOK, common.Result{1, "", nil})
}

}
6 changes: 6 additions & 0 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func InitRouter(r *server.Hertz) {
category.POST("", admin.SaveCategory)
// 菜品分类分页
category.GET("/page", admin.PageCat)
// 添加按照id删除
category.DELETE("", admin.DeleteCat)
// 添加修改分类
//category.PUT("",admin.UpdateCat)
//// 启用禁用分类
//emp.POST("/status/:status", admin.StartOrStopCat)
}

}
7 changes: 7 additions & 0 deletions internal/router/service/category_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ func PageQueryDat(categoryPage *dto.CategoryPageQueryDTO) *common.PageResult {
pageResult.Records, pageResult.Total = db.CatDao.PageQuery(categoryPage)
return &pageResult
}
func DeleteCat(id *int64) *error {
err := db.CatDao.Delete(id)
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 537e942

Please sign in to comment.