Delete word using DELETE
method.
Add Api
api/words/v1/words.go
...
type DeleteReq struct {
g.Meta `path:"words/{id}" method:"delete" sm:"Delete" tags:"Word"`
Id uint `json:"id" v:"required"`
}
type DeleteRes struct {
}
Write 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 Invokes 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
}
Interface Testing
$ 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
}