删除单词使用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 Delete(ctx context.Context, uid, id uint) (err error) {
db := dao.Words.Ctx(ctx).Where("id", id)
if uid > 0 {
db = db.Where("uid", uid)
}
_, err = db.Delete()
return
}
Controller调用Logic
internal/controller/words/words_v1_delete.go
package words
import (
"context"
"star/internal/logic/users"
"star/internal/logic/words"
"star/api/words/v1"
)
func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
uid, err := users.GetUid(ctx)
if err != nil {
return nil, err
}
err = 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
}