From 56f0ac46638d1b01db492963e627b8add85999b7 Mon Sep 17 00:00:00 2001 From: wenliang zhu <73632785+juniaoshaonian@users.noreply.github.com> Date: Thu, 8 Aug 2024 20:57:16 +0800 Subject: [PATCH] =?UTF-8?q?Dev=E4=B8=BA=E9=A2=98=E9=9B=86=E5=92=8C?= =?UTF-8?q?=E6=A1=88=E4=BE=8B=E9=9B=86=E7=9A=84=E9=A2=98=E7=9B=AE=E5=92=8C?= =?UTF-8?q?=E6=A1=88=E4=BE=8B=E6=B7=BB=E5=8A=A0=20=E7=82=B9=E8=B5=9E?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=20(#251)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 为案例集添加点赞数据 * make check --- .../integration/case_set_handler_test.go | 44 ++++++++++++++- .../cases/internal/web/case_set_handler.go | 23 ++++++-- .../internal/integration/set_handler_test.go | 56 ++++++++++++++++--- .../internal/web/question_set_handler.go | 29 +++++++--- 4 files changed, 130 insertions(+), 22 deletions(-) diff --git a/internal/cases/internal/integration/case_set_handler_test.go b/internal/cases/internal/integration/case_set_handler_test.go index b2a4a6f6..ab4a1d2c 100644 --- a/internal/cases/internal/integration/case_set_handler_test.go +++ b/internal/cases/internal/integration/case_set_handler_test.go @@ -267,6 +267,13 @@ func (s *CaseSetTestSuite) TestCaseSetDetailByBiz() { Content: "Go案例1", ExamineResult: domain.ResultAdvanced.ToUint8(), Utime: now, + Interactive: web.Interactive{ + ViewCnt: 615, + LikeCnt: 616, + CollectCnt: 617, + Liked: false, + Collected: true, + }, }, { Id: 615, @@ -275,6 +282,13 @@ func (s *CaseSetTestSuite) TestCaseSetDetailByBiz() { Title: "Go案例2", Content: "Go案例2", Utime: now, + Interactive: web.Interactive{ + ViewCnt: 616, + LikeCnt: 617, + CollectCnt: 618, + Liked: true, + Collected: false, + }, }, { Id: 616, @@ -283,6 +297,13 @@ func (s *CaseSetTestSuite) TestCaseSetDetailByBiz() { Title: "Go案例3", Content: "Go案例3", Utime: now, + Interactive: web.Interactive{ + ViewCnt: 617, + LikeCnt: 618, + CollectCnt: 619, + Liked: false, + Collected: true, + }, }, }, }, @@ -467,6 +488,13 @@ func (s *CaseSetTestSuite) TestCaseSet_Detail() { Content: "Go案例1", ExamineResult: domain.ResultAdvanced.ToUint8(), Utime: now, + Interactive: web.Interactive{ + ViewCnt: 615, + LikeCnt: 616, + CollectCnt: 617, + Liked: false, + Collected: true, + }, }, { Id: 615, @@ -475,6 +503,13 @@ func (s *CaseSetTestSuite) TestCaseSet_Detail() { Title: "Go案例2", Content: "Go案例2", Utime: now, + Interactive: web.Interactive{ + ViewCnt: 616, + LikeCnt: 617, + CollectCnt: 618, + Liked: true, + Collected: false, + }, }, { Id: 616, @@ -483,6 +518,13 @@ func (s *CaseSetTestSuite) TestCaseSet_Detail() { Title: "Go案例3", Content: "Go案例3", Utime: now, + Interactive: web.Interactive{ + ViewCnt: 617, + LikeCnt: 618, + CollectCnt: 619, + Liked: false, + Collected: true, + }, }, }, }, @@ -510,7 +552,7 @@ func (s *CaseSetTestSuite) TestCaseSet_Detail() { } } -func (s *CaseSetTestSuite) TestCaseSet_ListAllQuestionSets() { +func (s *CaseSetTestSuite) TestCaseSet_ListAllCaseSets() { // 插入一百条 total := 100 data := make([]dao.CaseSet, 0, total) diff --git a/internal/cases/internal/web/case_set_handler.go b/internal/cases/internal/web/case_set_handler.go index aae0acd0..944818f5 100644 --- a/internal/cases/internal/web/case_set_handler.go +++ b/internal/cases/internal/web/case_set_handler.go @@ -101,9 +101,10 @@ func (h *CaseSetHandler) getDetail( uid int64, cs domain.CaseSet) (ginx.Result, error) { var ( - eg errgroup.Group - intr interactive.Interactive - resultMap map[int64]domain.ExamineCaseResult + eg errgroup.Group + intr interactive.Interactive + caseIntrMap map[int64]interactive.Interactive + resultMap map[int64]domain.ExamineCaseResult ) eg.Go(func() error { @@ -111,6 +112,12 @@ func (h *CaseSetHandler) getDetail( intr, err = h.intrSvc.Get(ctx, "caseSet", cs.ID, uid) return err }) + eg.Go(func() error { + var err error + cids := cs.Cids() + caseIntrMap, err = h.intrSvc.GetByIds(ctx, "case", cids) + return err + }) eg.Go(func() error { var err error @@ -124,24 +131,28 @@ func (h *CaseSetHandler) getDetail( } return ginx.Result{ - Data: h.toCaseSetVO(cs, intr, resultMap), + Data: h.toCaseSetVO(cs, intr, caseIntrMap, resultMap), }, nil } func (h *CaseSetHandler) toCaseSetVO( set domain.CaseSet, intr interactive.Interactive, + caseIntrMap map[int64]interactive.Interactive, results map[int64]domain.ExamineCaseResult) CaseSet { cs := newCaseSet(set) - cs.Cases = h.toCaseVO(set.Cases, results) + cs.Cases = h.toCaseVO(set.Cases, results, caseIntrMap) cs.Interactive = newInteractive(intr) return cs } -func (h *CaseSetHandler) toCaseVO(cases []domain.Case, results map[int64]domain.ExamineCaseResult) []Case { +func (h *CaseSetHandler) toCaseVO(cases []domain.Case, + results map[int64]domain.ExamineCaseResult, + caseIntrMap map[int64]interactive.Interactive) []Case { return slice.Map(cases, func(idx int, src domain.Case) Case { ca := newCase(src) res := results[ca.Id] + ca.Interactive = newInteractive(caseIntrMap[ca.Id]) ca.ExamineResult = res.Result.ToUint8() return ca }) diff --git a/internal/question/internal/integration/set_handler_test.go b/internal/question/internal/integration/set_handler_test.go index 412cb7e7..cca4a427 100644 --- a/internal/question/internal/integration/set_handler_test.go +++ b/internal/question/internal/integration/set_handler_test.go @@ -275,6 +275,13 @@ func (s *SetHandlerTestSuite) TestQuestionSetDetailByBiz() { Content: "Go问题1", ExamineResult: domain.ResultAdvanced.ToUint8(), Utime: now, + Interactive: web.Interactive{ + ViewCnt: 615, + LikeCnt: 616, + CollectCnt: 617, + Liked: false, + Collected: true, + }, }, { Id: 615, @@ -283,6 +290,13 @@ func (s *SetHandlerTestSuite) TestQuestionSetDetailByBiz() { Title: "Go问题2", Content: "Go问题2", Utime: now, + Interactive: web.Interactive{ + ViewCnt: 616, + LikeCnt: 617, + CollectCnt: 618, + Liked: true, + Collected: false, + }, }, { Id: 616, @@ -291,6 +305,13 @@ func (s *SetHandlerTestSuite) TestQuestionSetDetailByBiz() { Title: "Go问题3", Content: "Go问题3", Utime: now, + Interactive: web.Interactive{ + ViewCnt: 617, + LikeCnt: 618, + CollectCnt: 619, + Liked: false, + Collected: true, + }, }, }, Utime: now, @@ -470,11 +491,18 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() { }, Questions: []web.Question{ { - Id: 614, - Biz: "project", - BizId: 1, - Title: "Go问题1", - Content: "Go问题1", + Id: 614, + Biz: "project", + BizId: 1, + Title: "Go问题1", + Content: "Go问题1", + Interactive: web.Interactive{ + ViewCnt: 615, + LikeCnt: 616, + CollectCnt: 617, + Liked: false, + Collected: true, + }, ExamineResult: domain.ResultAdvanced.ToUint8(), Utime: now, }, @@ -484,7 +512,14 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() { BizId: 1, Title: "Go问题2", Content: "Go问题2", - Utime: now, + Interactive: web.Interactive{ + ViewCnt: 616, + LikeCnt: 617, + CollectCnt: 618, + Liked: true, + Collected: false, + }, + Utime: now, }, { Id: 616, @@ -492,7 +527,14 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() { BizId: 1, Title: "Go问题3", Content: "Go问题3", - Utime: now, + Interactive: web.Interactive{ + ViewCnt: 617, + LikeCnt: 618, + CollectCnt: 619, + Liked: false, + Collected: true, + }, + Utime: now, }, }, Utime: now, diff --git a/internal/question/internal/web/question_set_handler.go b/internal/question/internal/web/question_set_handler.go index 5d0b6ebc..680756ef 100644 --- a/internal/question/internal/web/question_set_handler.go +++ b/internal/question/internal/web/question_set_handler.go @@ -118,9 +118,10 @@ func (h *QuestionSetHandler) getDetail( uid int64, qs domain.QuestionSet) (ginx.Result, error) { var ( - eg errgroup.Group - intr interactive.Interactive - resultMap map[int64]domain.ExamineResult + eg errgroup.Group + intr interactive.Interactive + queIntrMap map[int64]interactive.Interactive + resultMap map[int64]domain.ExamineResult ) eg.Go(func() error { @@ -129,6 +130,12 @@ func (h *QuestionSetHandler) getDetail( return err }) + eg.Go(func() error { + var eerr error + queIntrMap, eerr = h.intrSvc.GetByIds(ctx, "question", qs.Qids()) + return eerr + }) + eg.Go(func() error { var err error resultMap, err = h.examineSvc.GetResults(ctx, uid, qs.Qids()) @@ -141,23 +148,29 @@ func (h *QuestionSetHandler) getDetail( } return ginx.Result{ - Data: h.toQuestionSetVO(qs, intr, resultMap), + Data: h.toQuestionSetVO(qs, intr, resultMap, queIntrMap), }, nil } func (h *QuestionSetHandler) toQuestionSetVO( set domain.QuestionSet, intr interactive.Interactive, - results map[int64]domain.ExamineResult) QuestionSet { + results map[int64]domain.ExamineResult, + queIntrMap map[int64]interactive.Interactive, +) QuestionSet { qs := newQuestionSet(set) - qs.Questions = h.toQuestionVO(set.Questions, results) + qs.Questions = h.toQuestionVO(set.Questions, results, queIntrMap) qs.Interactive = newInteractive(intr) return qs } -func (h *QuestionSetHandler) toQuestionVO(questions []domain.Question, results map[int64]domain.ExamineResult) []Question { +func (h *QuestionSetHandler) toQuestionVO( + questions []domain.Question, + results map[int64]domain.ExamineResult, + queIntrMap map[int64]interactive.Interactive) []Question { return slice.Map(questions, func(idx int, src domain.Question) Question { - que := newQuestion(src, interactive.Interactive{}) + intr := queIntrMap[src.Id] + que := newQuestion(src, intr) res := results[que.Id] que.ExamineResult = res.Result.ToUint8() return que