Skip to content

Commit

Permalink
fix: question 更新遗漏 labels 字段
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Jul 5, 2024
1 parent 3547b48 commit 97ba87d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
50 changes: 38 additions & 12 deletions internal/question/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,13 @@ func (s *HandlerTestSuite) TestSave() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := s.db.WithContext(ctx).Create(&dao.Question{
Id: 2,
Uid: uid,
Title: "老的标题",
Id: 2,
Uid: uid,
Title: "老的标题",
Labels: sqlx.JsonColumn[[]string]{
Valid: true,
Val: []string{"MySQL"},
},
Content: "老的内容",
Status: domain.UnPublishedStatus.ToUint8(),
Ctime: 123,
Expand Down Expand Up @@ -277,6 +281,10 @@ func (s *HandlerTestSuite) TestSave() {
Status: domain.UnPublishedStatus.ToUint8(),
Title: "面试题1",
Content: "新的内容",
Labels: sqlx.JsonColumn[[]string]{
Valid: true,
Val: []string{"sqlite"},
},
}, q)
assert.Equal(t, 4, len(eles))
analysis := eles[0]
Expand Down Expand Up @@ -304,6 +312,7 @@ func (s *HandlerTestSuite) TestSave() {
Id: 2,
Title: "面试题1",
Content: "新的内容",
Labels: []string{"sqlite"},
Analysis: analysis,
Basic: s.buildAnswerEle(1),
Intermediate: s.buildAnswerEle(2),
Expand Down Expand Up @@ -470,9 +479,13 @@ func (s *HandlerTestSuite) TestSync() {
q, eles, err := s.dao.GetPubByID(ctx, 1)
require.NoError(t, err)
s.assertQuestion(t, dao.Question{
Uid: uid,
Title: "面试题1",
Status: domain.PublishedStatus.ToUint8(),
Uid: uid,
Title: "面试题1",
Status: domain.PublishedStatus.ToUint8(),
Labels: sqlx.JsonColumn[[]string]{
Valid: true,
Val: []string{"MySQL"},
},
Content: "面试题内容",
}, dao.Question(q))
assert.Equal(t, 4, len(eles))
Expand All @@ -481,6 +494,7 @@ func (s *HandlerTestSuite) TestSync() {
Question: web.Question{
Title: "面试题1",
Content: "面试题内容",
Labels: []string{"MySQL"},
Analysis: s.buildAnswerEle(0),
Basic: s.buildAnswerEle(1),
Intermediate: s.buildAnswerEle(2),
Expand All @@ -504,7 +518,10 @@ func (s *HandlerTestSuite) TestSync() {
Uid: uid,
Title: "老的标题",
Content: "老的内容",

Labels: sqlx.JsonColumn[[]string]{
Valid: true,
Val: []string{"MySQL"},
},
Ctime: 123,
Utime: 234,
}).Error
Expand All @@ -529,8 +546,12 @@ func (s *HandlerTestSuite) TestSync() {
q, eles, err := s.dao.GetByID(ctx, 2)
require.NoError(t, err)
s.assertQuestion(t, dao.Question{
Uid: uid,
Status: domain.PublishedStatus.ToUint8(),
Uid: uid,
Status: domain.PublishedStatus.ToUint8(),
Labels: sqlx.JsonColumn[[]string]{
Valid: true,
Val: []string{"sqlite"},
},
Title: "面试题1",
Content: "新的内容",
}, q)
Expand All @@ -549,9 +570,13 @@ func (s *HandlerTestSuite) TestSync() {
pq, pEles, err := s.dao.GetPubByID(ctx, 2)

s.assertQuestion(t, dao.Question{
Uid: uid,
Status: domain.PublishedStatus.ToUint8(),
Title: "面试题1",
Uid: uid,
Status: domain.PublishedStatus.ToUint8(),
Title: "面试题1",
Labels: sqlx.JsonColumn[[]string]{
Valid: true,
Val: []string{"sqlite"},
},
Content: "新的内容",
}, dao.Question(pq))
assert.Equal(t, 4, len(pEles))
Expand Down Expand Up @@ -580,6 +605,7 @@ func (s *HandlerTestSuite) TestSync() {
Id: 2,
Title: "面试题1",
Content: "新的内容",
Labels: []string{"sqlite"},
Analysis: analysis,
Basic: s.buildAnswerEle(1),
Intermediate: s.buildAnswerEle(2),
Expand Down
14 changes: 2 additions & 12 deletions internal/question/internal/repository/dao/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,8 @@ func (g *GORMQuestionDAO) Delete(ctx context.Context, qid int64) error {
}

func (g *GORMQuestionDAO) Update(ctx context.Context, q Question, eles []AnswerElement) error {
now := time.Now().UnixMilli()
return g.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
res := tx.Model(&Question{}).WithContext(ctx).Where("id = ?", q.Id).Updates(map[string]any{
"title": q.Title,
"content": q.Content,
"utime": now,
"status": q.Status,
})
if res.Error != nil {
return res.Error
}

return g.saveEles(tx, eles)
return g.update(tx, q, eles)
})
}

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

0 comments on commit 97ba87d

Please sign in to comment.