Skip to content

Commit

Permalink
skill添加进度
Browse files Browse the repository at this point in the history
  • Loading branch information
juniaoshaonian committed Jul 28, 2024
1 parent 6f5d5e2 commit 8eeaa29
Show file tree
Hide file tree
Showing 21 changed files with 898 additions and 111 deletions.
190 changes: 189 additions & 1 deletion internal/question/internal/integration/admin_set_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

"github.com/ecodeclub/webook/internal/question/internal/service"

"github.com/ecodeclub/webook/internal/ai"

"github.com/ecodeclub/webook/internal/permission"
Expand Down Expand Up @@ -58,6 +60,7 @@ type AdminSetHandlerTestSuite struct {
dao dao.QuestionDAO
questionSetDAO dao.QuestionSetDAO
producer *eveMocks.MockSyncEventProducer
setSvc service.QuestionSetService
}

func (s *AdminSetHandlerTestSuite) SetupSuite() {
Expand All @@ -82,7 +85,7 @@ func (s *AdminSetHandlerTestSuite) SetupSuite() {
})
module.AdminSetHdl.PrivateRoutes(server.Engine)
server.Use(middleware.NewCheckMembershipMiddlewareBuilder(nil).Build())

s.setSvc = module.SetSvc
s.server = server
s.db = testioc.InitDB()
err = dao.InitTables(s.db)
Expand Down Expand Up @@ -1065,6 +1068,191 @@ func (s *AdminSetHandlerTestSuite) TestQuestionSetEvent() {
}, ans)
}

func (s *AdminSetHandlerTestSuite) TestGetQuestionSets() {
var now int64 = 123
testCases := []struct {
name string
before func(t *testing.T)
after func(t *testing.T, sets []domain.QuestionSet)
req []int64
}{
{
name: "非空题集",
before: func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// 创建两个空题集
id, err := s.questionSetDAO.Create(ctx, dao.QuestionSet{
Id: 322,
Uid: uid,
Title: "Go",
Description: "Go题集",
Biz: "roadmap",
BizId: 2,
Utime: now,
})
require.NoError(t, err)
require.Equal(t, int64(322), id)
id, err = s.questionSetDAO.Create(ctx, dao.QuestionSet{
Id: 323,
Uid: uid,
Title: "mysql",
Description: "mysql题集",
Biz: "roadmap",
BizId: 3,
Utime: now,
})
require.NoError(t, err)
require.Equal(t, int64(323), id)

// 为322添加问题
questions := []dao.Question{
{
Id: 614,
Uid: uid + 1,
Biz: "project",
BizId: 1,
Title: "Go问题1",
Content: "Go问题1",
Ctime: now,
Utime: now,
},
{
Id: 615,
Uid: uid + 2,
Biz: "project",
BizId: 1,
Title: "Go问题2",
Content: "Go问题2",
Ctime: now,
Utime: now,
},
{
Id: 616,
Uid: uid + 3,
Biz: "project",
BizId: 1,
Title: "Go问题3",
Content: "Go问题3",
Ctime: now,
Utime: now,
},
}
err = s.db.WithContext(ctx).Create(&questions).Error
require.NoError(t, err)
qids := []int64{614, 615, 616}
require.NoError(t, s.questionSetDAO.UpdateQuestionsByID(ctx, 322, qids))
// 为333添加题目
questions = []dao.Question{
{
Id: 618,
Uid: uid + 1,
Biz: "project",
BizId: 1,
Title: "Mysql问题1",
Content: "Mysql问题1",
Ctime: now,
Utime: now,
},
{
Id: 619,
Uid: uid + 2,
Biz: "project",
BizId: 1,
Title: "Mysql问题2",
Content: "Mysql问题2",
Ctime: now,
Utime: now,
},
{
Id: 620,
Uid: uid + 3,
Biz: "project",
BizId: 1,
Title: "Mysql问题4",
Content: "Mysql问题4",
Ctime: now,
Utime: now,
},
}
err = s.db.WithContext(ctx).Create(&questions).Error
require.NoError(t, err)
qids = []int64{618, 619, 620}
require.NoError(t, s.questionSetDAO.UpdateQuestionsByID(ctx, 323, qids))

// 添加用户答题记录,只需要添加一个就可以
err = s.db.WithContext(ctx).Create(&dao.QuestionResult{
Uid: uid,
Qid: 614,
Result: domain.ResultAdvanced.ToUint8(),
Ctime: now,
Utime: now,
}).Error
require.NoError(t, err)

// 题集中题目为1
qs, err := s.questionSetDAO.GetQuestionsByID(ctx, 322)
require.NoError(t, err)
require.Equal(t, 3, len(qs))
qs, err = s.questionSetDAO.GetQuestionsByID(ctx, 323)
require.NoError(t, err)
require.Equal(t, 3, len(qs))
},
after: func(t *testing.T, sets []domain.QuestionSet) {
assert.Equal(t, []domain.QuestionSet{
{
Id: 322,
Title: "Go",
Questions: []domain.Question{
{
Id: 614,

Title: "Go问题1",
},
{
Id: 615,
Title: "Go问题2",
},
{
Id: 616,
Title: "Go问题3",
},
},
},
{
Id: 323,
Title: "mysql",
Questions: []domain.Question{
{
Id: 618,
Title: "Mysql问题1",
},
{
Id: 619,
Title: "Mysql问题2",
},
{
Id: 620,
Title: "Mysql问题4",
},
},
},
}, sets)

},
req: []int64{322, 323},
},
}
for _, tc := range testCases {
s.T().Run(tc.name, func(t *testing.T) {
tc.before(t)
sets, err := s.setSvc.GetByIDsWithQuestion(context.Background(), tc.req)
require.NoError(t, err)
tc.after(t, sets)
})
}
}

func TestAdminSetHandler(t *testing.T) {
suite.Run(t, new(AdminSetHandlerTestSuite))
}
43 changes: 43 additions & 0 deletions internal/question/internal/repository/dao/question_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"time"

"github.com/ecodeclub/ekit/slice"

"github.com/ego-component/egorm"
"gorm.io/gorm"
)
Expand All @@ -35,12 +37,53 @@ type QuestionSetDAO interface {
GetByIDs(ctx context.Context, ids []int64) ([]QuestionSet, error)
ListByBiz(ctx context.Context, offset int, limit int, biz string) ([]QuestionSet, error)
GetByBiz(ctx context.Context, biz string, bizId int64) (QuestionSet, error)
// GetByIDsWithQuestions 返回题集和题集对应的题目id
GetByIDsWithQuestions(ctx context.Context, ids []int64) ([]QuestionSet, map[int64][]Question, error)
}

type GORMQuestionSetDAO struct {
db *egorm.Component
}

func (g *GORMQuestionSetDAO) GetByIDsWithQuestions(ctx context.Context, ids []int64) ([]QuestionSet, map[int64][]Question, error) {
var sets []QuestionSet
db := g.db.WithContext(ctx)
err := db.Where("id IN ?", ids).Find(&sets).Error
if err != nil {
return nil, nil, err
}
var questions []QuestionSetQuestion
err = db.Model(&QuestionSetQuestion{}).Where("qs_id IN ?", ids).Find(&questions).Error
if err != nil {
return nil, nil, err
}
qids := slice.Map(questions, func(idx int, src QuestionSetQuestion) int64 {
return src.QID
})
var qs []Question
err = db.Model(&Question{}).Where("id IN ?", qids).Find(&qs).Error
if err != nil {
return nil, nil, err
}
qmap := slice.ToMap(qs, func(element Question) int64 {
return element.Id
})
questionMap := make(map[int64][]Question, len(ids))
for _, q := range questions {
questionList, ok := questionMap[q.QSID]
question := qmap[q.QID]
if ok {
questionList = append(questionList, question)
questionMap[q.QSID] = questionList
} else {
questionMap[q.QSID] = []Question{
question,
}
}
}
return sets, questionMap, nil
}

func (g *GORMQuestionSetDAO) GetByBiz(ctx context.Context, biz string, bizId int64) (QuestionSet, error) {
var res QuestionSet
db := g.db.WithContext(ctx)
Expand Down
24 changes: 24 additions & 0 deletions internal/question/internal/repository/question_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type QuestionSetRepository interface {
List(ctx context.Context, offset int, limit int) ([]domain.QuestionSet, error)
UpdateNonZero(ctx context.Context, set domain.QuestionSet) error
GetByIDs(ctx context.Context, ids []int64) ([]domain.QuestionSet, error)
GetByIDsWithQuestion(ctx context.Context, ids []int64) ([]domain.QuestionSet, error)
ListByBiz(ctx context.Context, offset, limit int, biz string) ([]domain.QuestionSet, error)
GetByBiz(ctx context.Context, biz string, bizId int64) (domain.QuestionSet, error)
}
Expand All @@ -43,6 +44,29 @@ type questionSetRepository struct {
logger *elog.Component
}

func (q *questionSetRepository) GetByIDsWithQuestion(ctx context.Context, ids []int64) ([]domain.QuestionSet, error) {
qsets, questionMap, err := q.dao.GetByIDsWithQuestions(ctx, ids)
if err != nil {
return nil, err
}
res := slice.Map(qsets, func(idx int, src dao.QuestionSet) domain.QuestionSet {
qids := questionMap[src.Id]
set := domain.QuestionSet{
Id: src.Id,
Title: src.Title,
}
questions := slice.Map(qids, func(idx int, src dao.Question) domain.Question {
return domain.Question{
Id: src.Id,
Title: src.Title,
}
})
set.Questions = questions
return set
})
return res, nil
}

func (q *questionSetRepository) GetByBiz(ctx context.Context, biz string, bizId int64) (domain.QuestionSet, error) {
set, err := q.dao.GetByBiz(ctx, biz, bizId)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/question/internal/service/examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
var ErrInsufficientCredit = ai.ErrInsufficientCredit

// ExamineService 测试服务
//
//go:generate mockgen -source=./examine.go -destination=../../mocks/examine.mock.go -package=quemocks -typed=true ExamineService
type ExamineService interface {
// Examine 测试服务
// input 是用户输入的内容
Expand Down
5 changes: 5 additions & 0 deletions internal/question/internal/service/question_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type QuestionSetService interface {
GetByIds(ctx context.Context, ids []int64) ([]domain.QuestionSet, error)
DetailByBiz(ctx context.Context, biz string, bizId int64) (domain.QuestionSet, error)
GetCandidates(ctx context.Context, id int64, offset int, limit int) ([]domain.Question, int64, error)
GetByIDsWithQuestion(ctx context.Context, ids []int64) ([]domain.QuestionSet, error)
}

type questionSetService struct {
Expand All @@ -51,6 +52,10 @@ type questionSetService struct {
syncTimeout time.Duration
}

func (q *questionSetService) GetByIDsWithQuestion(ctx context.Context, ids []int64) ([]domain.QuestionSet, error) {
return q.repo.GetByIDsWithQuestion(ctx, ids)
}

func (q *questionSetService) GetCandidates(ctx context.Context, id int64, offset int, limit int) ([]domain.Question, int64, error) {
qs, err := q.repo.GetByID(ctx, id)
if err != nil {
Expand Down
Loading

0 comments on commit 8eeaa29

Please sign in to comment.