Skip to content

Commit 2391510

Browse files
committed
fix(tag): tag的启用状态为未启用,也就是0时,无法更新成功
Fix #2
1 parent ec0b2ea commit 2391510

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

docs/docs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
22
// This file was generated by swaggo/swag at
3-
// 2021-06-20 16:53:32.405848 +0800 CST m=+0.051690642
3+
// 2021-06-20 17:26:29.286161 +0800 CST m=+0.049265085
44

55
package docs
66

@@ -130,6 +130,9 @@ var doc = `{
130130
},
131131
"/api/v1/tags/{id}": {
132132
"put": {
133+
"consumes": [
134+
"application/json"
135+
],
133136
"produces": [
134137
"application/json"
135138
],

docs/swagger.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@
112112
},
113113
"/api/v1/tags/{id}": {
114114
"put": {
115+
"consumes": [
116+
"application/json"
117+
],
115118
"produces": [
116119
"application/json"
117120
],

docs/swagger.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ paths:
186186
$ref: '#/definitions/errcode.Error'
187187
summary: 删除标签
188188
put:
189+
consumes:
190+
- application/json
189191
parameters:
190192
- description: 标签 ID
191193
in: path

internal/dao/tag.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,22 @@ func (d *Dao) CreateTag(name string, state uint8, createdBy string) error {
2727
return tag.Create(d.engine)
2828
}
2929

30-
func (d *Dao) UpdateTag(id uint32, name string, state uint8, modifiedBy string) error {
30+
func (d *Dao) UpdateTag(id uint32, name string, state *uint8, modifiedBy string) error {
3131
tag := model.Tag{
32-
Name: name,
33-
State: state,
34-
Model: &model.Model{ID: id, ModifiedBy: modifiedBy},
32+
Model: &model.Model{ID: id},
33+
}
34+
35+
values := map[string]interface{}{
36+
"modified_by": modifiedBy,
37+
}
38+
if name != "" {
39+
values["name"] = name
40+
}
41+
if state != nil {
42+
values["state"] = state
3543
}
3644

37-
return tag.Update(d.engine)
45+
return tag.Update(d.engine, values)
3846
}
3947

4048
func (d *Dao) DeleteTag(id uint32) error {

internal/model/tag.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ func (t Tag) Create(db *gorm.DB) error {
5959
return db.Create(&t).Error
6060
}
6161

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

6772
func (t Tag) Delete(db *gorm.DB) error {

internal/service/tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type CreateTagRequest struct {
2626
type UpdateTagRequest struct {
2727
ID uint32 `form:"id" binding:"required,gte=1"` // 标签id
2828
Name string `form:"name" binding:"max=100"` // 标签名称
29-
State uint8 `form:"state" binding:"oneof=0 1"` //状态,是否启用(0 为禁用、1 为启用)
29+
State *uint8 `form:"state" binding:"omitempty,oneof=0 1"` //状态,是否启用(0 为禁用、1 为启用)
3030
ModifiedBy string `form:"modified_by" binding:"required,min=2,max=100"` // 修改者
3131
}
3232

0 commit comments

Comments
 (0)