From 669c0ece92232e6e925e710aa31336d5d2e06d74 Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Fri, 5 Jul 2024 15:26:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20question=20=E6=9B=B4=E6=96=B0=E9=81=97?= =?UTF-8?q?=E6=BC=8F=20labels=20=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/integration/handler_test.go | 50 ++++++++++++++----- .../internal/repository/dao/question.go | 14 +----- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/internal/question/internal/integration/handler_test.go b/internal/question/internal/integration/handler_test.go index ac58b8ad..f9281a8c 100644 --- a/internal/question/internal/integration/handler_test.go +++ b/internal/question/internal/integration/handler_test.go @@ -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, @@ -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] @@ -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), @@ -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)) @@ -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), @@ -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 @@ -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) @@ -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)) @@ -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), diff --git a/internal/question/internal/repository/dao/question.go b/internal/question/internal/repository/dao/question.go index f8cdf7c9..e4a7a04c 100644 --- a/internal/question/internal/repository/dao/question.go +++ b/internal/question/internal/repository/dao/question.go @@ -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) }) } @@ -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, })