跳到主要内容
版本:2.8.x(Latest)

删除单词使用DELETE方式。

添加Api


api/words/v1/words.go

...
type DeleteReq struct {
g.Meta `path:"words/{id}" method:"delete" sm:"删除" tags:"单词"`
Id uint `json:"id" v:"required"`
}

type DeleteRes struct {
}

编写Logic


internal/logic/words/words.go

...

func (w *Words) Delete(ctx context.Context, uid, id uint) (err error) {
var (
cls = dao.Words.Columns()
orm = dao.Words.Ctx(ctx)
)
orm = orm.Where(cls.Id, id)
if uid > 0 {
orm = orm.Where(cls.Uid, uid)
}
_, err = orm.Delete()
return
}

Controller调用Logic


internal/controller/words/words_v1_delete.go

package words  

import (
"context"

"star/api/words/v1"
)

func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
uid, err := c.users.GetUid(ctx)
if err != nil {
return nil, err
}

err = c.words.Delete(ctx, uid, req.Id)
return
}

接口测试


$ curl -X DELETE http://127.0.0.1:8000/v1/words/1 \
-H "Authorization: eyJhbGci...5U" \
-H "Content-Type: application/json" \

{
    "code": 0,
    "message": "",
    "data": null
}