Skip to content

Commit

Permalink
fix(tag): tag的启用状态为未启用,也就是0时,无法更新成功
Browse files Browse the repository at this point in the history
Fix #2
  • Loading branch information
minibear2333 committed Jun 20, 2021
1 parent ec0b2ea commit 2391510
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
5 changes: 4 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2021-06-20 16:53:32.405848 +0800 CST m=+0.051690642
// 2021-06-20 17:26:29.286161 +0800 CST m=+0.049265085

package docs

Expand Down Expand Up @@ -130,6 +130,9 @@ var doc = `{
},
"/api/v1/tags/{id}": {
"put": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
Expand Down
3 changes: 3 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
},
"/api/v1/tags/{id}": {
"put": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
Expand Down
2 changes: 2 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ paths:
$ref: '#/definitions/errcode.Error'
summary: 删除标签
put:
consumes:
- application/json
parameters:
- description: 标签 ID
in: path
Expand Down
18 changes: 13 additions & 5 deletions internal/dao/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ func (d *Dao) CreateTag(name string, state uint8, createdBy string) error {
return tag.Create(d.engine)
}

func (d *Dao) UpdateTag(id uint32, name string, state uint8, modifiedBy string) error {
func (d *Dao) UpdateTag(id uint32, name string, state *uint8, modifiedBy string) error {
tag := model.Tag{
Name: name,
State: state,
Model: &model.Model{ID: id, ModifiedBy: modifiedBy},
Model: &model.Model{ID: id},
}

values := map[string]interface{}{
"modified_by": modifiedBy,
}
if name != "" {
values["name"] = name
}
if state != nil {
values["state"] = state
}

return tag.Update(d.engine)
return tag.Update(d.engine, values)
}

func (d *Dao) DeleteTag(id uint32) error {
Expand Down
9 changes: 7 additions & 2 deletions internal/model/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ func (t Tag) Create(db *gorm.DB) error {
return db.Create(&t).Error
}

func (t Tag) Update(db *gorm.DB) error {
func (t Tag) Update(db *gorm.DB, values interface{}) error {
// Updates 更新所选字段
return db.Model(&Tag{}).Where("id = ? AND is_del = ?", t.ID, 0).Update(t).Error
// 不能这样写 db.Model(&Tag{}).Where("id = ? AND is_del = ?", t.ID, 0).Update(t).Error
// https://github.com/golang-minibear2333/gin-blog/issues/2
if err := db.Model(t).Where("id = ? AND is_del = ?", t.ID, 0).Updates(values).Error; err != nil {
return err
}
return nil
}

func (t Tag) Delete(db *gorm.DB) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type CreateTagRequest struct {
type UpdateTagRequest struct {
ID uint32 `form:"id" binding:"required,gte=1"` // 标签id
Name string `form:"name" binding:"max=100"` // 标签名称
State uint8 `form:"state" binding:"oneof=0 1"` //状态,是否启用(0 为禁用、1 为启用)
State *uint8 `form:"state" binding:"omitempty,oneof=0 1"` //状态,是否启用(0 为禁用、1 为启用)
ModifiedBy string `form:"modified_by" binding:"required,min=2,max=100"` // 修改者
}

Expand Down

0 comments on commit 2391510

Please sign in to comment.