The function of setting the proficiency level is essentially an edit that only changes one field, ProficiencyLevel
, so using the PATCH
method is more suitable than PUT
.
Add Api
api/words/v1/learn_words.go
...
type SetLevelReq struct {
g.Meta `path:"words/{id}/level" method:"patch"`
Id uint `json:"id" v:"required"`
Level model.ProficiencyLevel `json:"level" v:"required|between:1,5"`
}
type SetLevelRes struct {
}
Both words/{id}/level
and words/level/{id}
route styles are acceptable, but the former is more in line with the resource hierarchy, and it is recommended to use it.
Write Logic
internal/controller/words/words_v1_set_level.go
...
// SetLevel sets the word proficiency level
func SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) error {
if level < 0 || level > 5 {
return gerror.New("Invalid proficiency level value")
}
db := dao.Words.Ctx(ctx)
if uid > 0 {
db = db.Where("uid", uid)
}
_, err := db.Data("proficiency_level", level).Where("id", id).Update()
return err
}
To prevent data anomalies, we need to check if the level is between 1-5
before entering the database.
Controller calls Logic
internal/logic/words/learn_words.go
package words
import (
"context"
"star/api/words/v1"
"star/internal/logic/users"
"star/internal/logic/words"
)
func (c *ControllerV1) SetLevel(ctx context.Context, req *v1.SetLevelReq) (res *v1.SetLevelRes, err error) {
uid, err := users.GetUid(ctx)
if err != nil {
return nil, err
}
err = words.SetLevel(ctx, uid, req.Id, req.Level)
return nil, err
}
Interface Testing
$ curl -X PATCH http://127.0.0.1:8000/v1/words/1/level \
-H "Authorization: eyJhbGci...5U" \
-H "Content-Type: application/json" \
-d '{
"proficiency_level": 5
}'
{
"code": 0,
"message": "",
"data": null
}