Skip to content

Commit 97ba87d

Browse files
committed
fix: question 更新遗漏 labels 字段
1 parent 3547b48 commit 97ba87d

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

internal/question/internal/integration/handler_test.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,13 @@ func (s *HandlerTestSuite) TestSave() {
244244
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
245245
defer cancel()
246246
err := s.db.WithContext(ctx).Create(&dao.Question{
247-
Id: 2,
248-
Uid: uid,
249-
Title: "老的标题",
247+
Id: 2,
248+
Uid: uid,
249+
Title: "老的标题",
250+
Labels: sqlx.JsonColumn[[]string]{
251+
Valid: true,
252+
Val: []string{"MySQL"},
253+
},
250254
Content: "老的内容",
251255
Status: domain.UnPublishedStatus.ToUint8(),
252256
Ctime: 123,
@@ -277,6 +281,10 @@ func (s *HandlerTestSuite) TestSave() {
277281
Status: domain.UnPublishedStatus.ToUint8(),
278282
Title: "面试题1",
279283
Content: "新的内容",
284+
Labels: sqlx.JsonColumn[[]string]{
285+
Valid: true,
286+
Val: []string{"sqlite"},
287+
},
280288
}, q)
281289
assert.Equal(t, 4, len(eles))
282290
analysis := eles[0]
@@ -304,6 +312,7 @@ func (s *HandlerTestSuite) TestSave() {
304312
Id: 2,
305313
Title: "面试题1",
306314
Content: "新的内容",
315+
Labels: []string{"sqlite"},
307316
Analysis: analysis,
308317
Basic: s.buildAnswerEle(1),
309318
Intermediate: s.buildAnswerEle(2),
@@ -470,9 +479,13 @@ func (s *HandlerTestSuite) TestSync() {
470479
q, eles, err := s.dao.GetPubByID(ctx, 1)
471480
require.NoError(t, err)
472481
s.assertQuestion(t, dao.Question{
473-
Uid: uid,
474-
Title: "面试题1",
475-
Status: domain.PublishedStatus.ToUint8(),
482+
Uid: uid,
483+
Title: "面试题1",
484+
Status: domain.PublishedStatus.ToUint8(),
485+
Labels: sqlx.JsonColumn[[]string]{
486+
Valid: true,
487+
Val: []string{"MySQL"},
488+
},
476489
Content: "面试题内容",
477490
}, dao.Question(q))
478491
assert.Equal(t, 4, len(eles))
@@ -481,6 +494,7 @@ func (s *HandlerTestSuite) TestSync() {
481494
Question: web.Question{
482495
Title: "面试题1",
483496
Content: "面试题内容",
497+
Labels: []string{"MySQL"},
484498
Analysis: s.buildAnswerEle(0),
485499
Basic: s.buildAnswerEle(1),
486500
Intermediate: s.buildAnswerEle(2),
@@ -504,7 +518,10 @@ func (s *HandlerTestSuite) TestSync() {
504518
Uid: uid,
505519
Title: "老的标题",
506520
Content: "老的内容",
507-
521+
Labels: sqlx.JsonColumn[[]string]{
522+
Valid: true,
523+
Val: []string{"MySQL"},
524+
},
508525
Ctime: 123,
509526
Utime: 234,
510527
}).Error
@@ -529,8 +546,12 @@ func (s *HandlerTestSuite) TestSync() {
529546
q, eles, err := s.dao.GetByID(ctx, 2)
530547
require.NoError(t, err)
531548
s.assertQuestion(t, dao.Question{
532-
Uid: uid,
533-
Status: domain.PublishedStatus.ToUint8(),
549+
Uid: uid,
550+
Status: domain.PublishedStatus.ToUint8(),
551+
Labels: sqlx.JsonColumn[[]string]{
552+
Valid: true,
553+
Val: []string{"sqlite"},
554+
},
534555
Title: "面试题1",
535556
Content: "新的内容",
536557
}, q)
@@ -549,9 +570,13 @@ func (s *HandlerTestSuite) TestSync() {
549570
pq, pEles, err := s.dao.GetPubByID(ctx, 2)
550571

551572
s.assertQuestion(t, dao.Question{
552-
Uid: uid,
553-
Status: domain.PublishedStatus.ToUint8(),
554-
Title: "面试题1",
573+
Uid: uid,
574+
Status: domain.PublishedStatus.ToUint8(),
575+
Title: "面试题1",
576+
Labels: sqlx.JsonColumn[[]string]{
577+
Valid: true,
578+
Val: []string{"sqlite"},
579+
},
555580
Content: "新的内容",
556581
}, dao.Question(pq))
557582
assert.Equal(t, 4, len(pEles))
@@ -580,6 +605,7 @@ func (s *HandlerTestSuite) TestSync() {
580605
Id: 2,
581606
Title: "面试题1",
582607
Content: "新的内容",
608+
Labels: []string{"sqlite"},
583609
Analysis: analysis,
584610
Basic: s.buildAnswerEle(1),
585611
Intermediate: s.buildAnswerEle(2),

internal/question/internal/repository/dao/question.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,8 @@ func (g *GORMQuestionDAO) Delete(ctx context.Context, qid int64) error {
9393
}
9494

9595
func (g *GORMQuestionDAO) Update(ctx context.Context, q Question, eles []AnswerElement) error {
96-
now := time.Now().UnixMilli()
9796
return g.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
98-
res := tx.Model(&Question{}).WithContext(ctx).Where("id = ?", q.Id).Updates(map[string]any{
99-
"title": q.Title,
100-
"content": q.Content,
101-
"utime": now,
102-
"status": q.Status,
103-
})
104-
if res.Error != nil {
105-
return res.Error
106-
}
107-
108-
return g.saveEles(tx, eles)
97+
return g.update(tx, q, eles)
10998
})
11099
}
111100

@@ -114,6 +103,7 @@ func (g *GORMQuestionDAO) update(tx *gorm.DB, q Question, eles []AnswerElement)
114103
res := tx.Model(&q).Where("id = ?", q.Id).Updates(map[string]any{
115104
"title": q.Title,
116105
"content": q.Content,
106+
"labels": q.Labels,
117107
"status": q.Status,
118108
"utime": now,
119109
})

0 commit comments

Comments
 (0)