Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

给refsid加题集 #250

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions internal/skill/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ func (s *HandlerTestSuite) TestRefsByLevelIDs() {
Ctime: time.Now().UnixMilli(),
Utime: time.Now().UnixMilli(),
},

{
Id: 3,
Slid: 2,
Expand All @@ -967,6 +968,24 @@ func (s *HandlerTestSuite) TestRefsByLevelIDs() {
Ctime: time.Now().UnixMilli(),
Utime: time.Now().UnixMilli(),
},
{
Id: 4,
Slid: 1,
Sid: 2,
Rtype: "question_set",
Rid: 1,
Ctime: time.Now().UnixMilli(),
Utime: time.Now().UnixMilli(),
},
{
Id: 5,
Slid: 2,
Sid: 2,
Rtype: "question_set",
Rid: 6,
Ctime: time.Now().UnixMilli(),
Utime: time.Now().UnixMilli(),
},
}).Error
require.NoError(s.T(), err)
testCases := []struct {
Expand All @@ -987,20 +1006,54 @@ func (s *HandlerTestSuite) TestRefsByLevelIDs() {
{
Id: 1,
Questions: []web.Question{
{Id: 2, Title: "这是问题2"},
{Id: 2, Title: "这是问题2", Result: 2 % 4},
},
Cases: []web.Case{
{Id: 1, Title: "这是案例1"},
},
QuestionSets: []web.QuestionSet{},
QuestionSets: []web.QuestionSet{
{
ID: 1,
Title: "这是题集1",
Questions: []web.Question{
{
Id: 11,
Title: "这是题目11",
Result: 11 % 4,
},
{
Id: 12,
Title: "这是题目12",
Result: 12 % 4,
},
},
},
},
},
{
Id: 2,
Questions: []web.Question{
{Id: 1, Title: "这是问题1"},
{Id: 1, Title: "这是问题1", Result: 1 % 4},
},
Cases: []web.Case{},
QuestionSets: []web.QuestionSet{
{
ID: 6,
Title: "这是题集6",
Questions: []web.Question{
{
Id: 66,
Title: "这是题目66",
Result: 66 % 4,
},
{
Id: 72,
Title: "这是题目72",
Result: 72 % 4,
},
},
},
},
Cases: []web.Case{},
QuestionSets: []web.QuestionSet{},
},
},
},
Expand All @@ -1017,7 +1070,8 @@ func (s *HandlerTestSuite) TestRefsByLevelIDs() {
recorder := test.NewJSONResponseRecorder[[]web.SkillLevel]()
s.server.ServeHTTP(recorder, req)
require.Equal(t, tc.wantCode, recorder.Code)
assert.Equal(t, tc.wantResp, recorder.MustScan())
data := recorder.MustScan()
assert.Equal(t, tc.wantResp, data)
})
}
}
Expand Down
86 changes: 42 additions & 44 deletions internal/skill/internal/web/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"context"
"fmt"
"net/http"

Expand Down Expand Up @@ -47,7 +48,7 @@ func (h *Handler) PrivateRoutes(server *gin.Engine) {
server.POST("/skill/detail", ginx.B[Sid](h.Detail))
server.POST("/skill/detail-refs", ginx.S(h.Permission), ginx.B[Sid](h.DetailRefs))
server.POST("/skill/save-refs", ginx.S(h.Permission), ginx.B(h.SaveRefs))
server.POST("/skill/level-refs", ginx.S(h.Permission), ginx.B(h.RefsByLevelIDs))
server.POST("/skill/level-refs", ginx.S(h.Permission), ginx.BS(h.RefsByLevelIDs))
server.POST("/skill/level/detail", ginx.BS[LevelInfoReq](h.LevelInfo))
}

Expand Down Expand Up @@ -152,41 +153,16 @@ func (h *Handler) DetailRefs(ctx *ginx.Context, req Sid) (ginx.Result, error) {
}, eg.Wait()
}

func (h *Handler) RefsByLevelIDs(ctx *ginx.Context, req IDs) (ginx.Result, error) {
func (h *Handler) RefsByLevelIDs(ctx *ginx.Context, req IDs, sess session.Session) (ginx.Result, error) {
uid := sess.Claims().Uid
if len(req.IDs) == 0 {
return ginx.Result{}, nil
}
res, err := h.svc.RefsByLevelIDs(ctx, req.IDs)
if err != nil {
return systemErrorResult, err
}
var (
eg errgroup.Group
csm map[int64]cases.Case
qsm map[int64]baguwen.Question
)
qids := make([]int64, 0, 32)
cids := make([]int64, 0, 16)
for _, sl := range res {
qids = append(qids, sl.Questions...)
cids = append(cids, sl.Cases...)
}
eg.Go(func() error {
cs, err1 := h.caseSvc.GetPubByIDs(ctx, cids)
csm = slice.ToMap(cs, func(element cases.Case) int64 {
return element.Id
})
return err1
})

eg.Go(func() error {
qs, err1 := h.queSvc.GetPubByIDs(ctx, qids)
qsm = slice.ToMap(qs, func(element baguwen.Question) int64 {
return element.Id
})
return err1
})
err = eg.Wait()
csm, qsm, qssmap, examResMap, err := h.getmap(ctx, uid, res)
if err != nil {
return systemErrorResult, err
}
Expand All @@ -195,7 +171,8 @@ func (h *Handler) RefsByLevelIDs(ctx *ginx.Context, req IDs) (ginx.Result, error
Data: slice.Map(res, func(idx int, src domain.SkillLevel) SkillLevel {
sl := newSkillLevel(src)
sl.setCases(csm)
sl.setQuestions(qsm)
sl.setQuestionsWithExam(qsm, examResMap)
sl.setQuestionSet(qssmap, examResMap)
return sl
}),
}, nil
Expand All @@ -207,33 +184,60 @@ func (h *Handler) LevelInfo(ctx *ginx.Context, req LevelInfoReq, sess session.Se
if err != nil {
return systemErrorResult, err
}
csm, qsm, qssmap, examResMap, err := h.getmap(ctx, uid, []domain.SkillLevel{res})
if err != nil {
return systemErrorResult, err
}
sl := newSkillLevel(res)
sl.setCases(csm)
sl.setQuestionsWithExam(qsm, examResMap)
sl.setQuestionSet(qssmap, examResMap)
return ginx.Result{
Data: sl,
}, nil
}

func (h *Handler) getmap(ctx context.Context, uid int64, levels []domain.SkillLevel) (map[int64]cases.Case,
map[int64]baguwen.Question,
map[int64]baguwen.QuestionSet,
map[int64]baguwen.ExamResult,
error,
) {
var (
err error
eg errgroup.Group
csm map[int64]cases.Case
qsm map[int64]baguwen.Question
qssmap map[int64]baguwen.QuestionSet
examResMap map[int64]baguwen.ExamResult
)
qids := make([]int64, 0, 32)
cids := make([]int64, 0, 16)
qsids := make([]int64, 0, 16)
for _, sl := range levels {
qids = append(qids, sl.Questions...)
cids = append(cids, sl.Cases...)
qsids = append(qsids, sl.QuestionSets...)
}
var qid2s []int64
qids := res.Questions
cids := res.Cases
qsids := res.QuestionSets
qid2s = append(qid2s, qids...)
// 获取case
eg.Go(func() error {
cs, err1 := h.caseSvc.GetPubByIDs(ctx, cids)
csm = slice.ToMap(cs, func(element cases.Case) int64 {
return element.Id
})
return err1
})

// 获取问题
eg.Go(func() error {
qs, err1 := h.queSvc.GetPubByIDs(ctx, qids)
qsm = slice.ToMap(qs, func(element baguwen.Question) int64 {
return element.Id
})
return err1
})
// 获取问题集
eg.Go(func() error {
qsets, qerr := h.queSetSvc.GetByIDsWithQuestion(ctx, qsids)
if qerr != nil {
Expand All @@ -250,19 +254,13 @@ func (h *Handler) LevelInfo(ctx *ginx.Context, req LevelInfoReq, sess session.Se
return nil
})
if err = eg.Wait(); err != nil {
return systemErrorResult, err
return nil, nil, nil, nil, err
}
// 获取进度
examResMap, err = h.examSvc.GetResults(ctx, uid, qid2s)
if err != nil {
return systemErrorResult, err
return nil, nil, nil, nil, err
}
// 组装 title
sl := newSkillLevel(res)
sl.setCases(csm)
sl.setQuestionsWithExam(qsm, examResMap)
sl.setQuestionSet(qssmap, examResMap)
return ginx.Result{
Data: sl,
}, nil
return csm, qsm, qssmap, examResMap, nil

}
Loading