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

在Question PubList 调整为只返回八股文内容 #241

Merged
merged 1 commit into from
Jul 24, 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
21 changes: 17 additions & 4 deletions internal/question/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,27 @@ func (s *HandlerTestSuite) TestPubList() {
data = append(data, dao.PublishQuestion{
Id: id,
Uid: uid,
Biz: "project",
Biz: domain.DefaultBiz,
BizId: id,
Status: domain.UnPublishedStatus.ToUint8(),
Title: fmt.Sprintf("这是标题 %d", idx),
Content: fmt.Sprintf("这是解析 %d", idx),
Utime: 123,
})
}

// project 的不会被搜索到
data = append(data, dao.PublishQuestion{
Id: 101,
Uid: uid,
Biz: "project",
BizId: 101,
Status: domain.UnPublishedStatus.ToUint8(),
Title: fmt.Sprintf("这是标题 %d", 101),
Content: fmt.Sprintf("这是解析 %d", 101),
Utime: 123,
})

err := s.db.Create(&data).Error
require.NoError(s.T(), err)
testCases := []struct {
Expand All @@ -171,7 +184,7 @@ func (s *HandlerTestSuite) TestPubList() {
Content: "这是解析 99",
Status: domain.UnPublishedStatus.ToUint8(),
Utime: 123,
Biz: "project",
Biz: domain.DefaultBiz,
BizId: 100,
Interactive: web.Interactive{
ViewCnt: 101,
Expand All @@ -187,7 +200,7 @@ func (s *HandlerTestSuite) TestPubList() {
Content: "这是解析 98",
Status: domain.UnPublishedStatus.ToUint8(),
Utime: 123,
Biz: "project",
Biz: domain.DefaultBiz,
BizId: 99,
Interactive: web.Interactive{
ViewCnt: 100,
Expand All @@ -213,7 +226,7 @@ func (s *HandlerTestSuite) TestPubList() {
Id: 1,
Title: "这是标题 0",
Content: "这是解析 0",
Biz: "project",
Biz: domain.DefaultBiz,
BizId: 1,
Status: domain.UnPublishedStatus.ToUint8(),
Utime: 123,
Expand Down
5 changes: 3 additions & 2 deletions internal/question/internal/repository/dao/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type QuestionDAO interface {
Sync(ctx context.Context, que Question, eles []AnswerElement) (int64, error)

// 线上库 API
PubList(ctx context.Context, offset int, limit int) ([]PublishQuestion, error)
PubList(ctx context.Context, offset int, limit int, biz string) ([]PublishQuestion, error)
PubCount(ctx context.Context) (int64, error)
GetPubByID(ctx context.Context, qid int64) (PublishQuestion, []PublishAnswerElement, error)
GetPubByIDs(ctx context.Context, qids []int64) ([]PublishQuestion, error)
Expand Down Expand Up @@ -152,9 +152,10 @@ func (g *GORMQuestionDAO) Count(ctx context.Context) (int64, error) {
return res, err
}

func (g *GORMQuestionDAO) PubList(ctx context.Context, offset int, limit int) ([]PublishQuestion, error) {
func (g *GORMQuestionDAO) PubList(ctx context.Context, offset int, limit int, biz string) ([]PublishQuestion, error) {
var res []PublishQuestion
err := g.db.WithContext(ctx).Offset(offset).
Where("biz = ?", biz).
Limit(limit).Order("id DESC").
Find(&res).Error
return res, err
Expand Down
23 changes: 3 additions & 20 deletions internal/question/internal/repository/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import (
)

type Repository interface {
PubList(ctx context.Context, offset int, limit int) ([]domain.Question, error)
PubTotal(ctx context.Context) (int64, error)
PubList(ctx context.Context, offset int, limit int, biz string) ([]domain.Question, error)
// Sync 保存到制作库,而后同步到线上库
Sync(ctx context.Context, que *domain.Question) (int64, error)
List(ctx context.Context, offset int, limit int) ([]domain.Question, error)
Expand Down Expand Up @@ -111,30 +110,14 @@ func (c *CachedRepository) Total(ctx context.Context) (int64, error) {
return c.dao.Count(ctx)
}

func (c *CachedRepository) PubList(ctx context.Context, offset int, limit int) ([]domain.Question, error) {
func (c *CachedRepository) PubList(ctx context.Context, offset int, limit int, biz string) ([]domain.Question, error) {
// TODO 缓存第一页
qs, err := c.dao.PubList(ctx, offset, limit)
qs, err := c.dao.PubList(ctx, offset, limit, biz)
return slice.Map(qs, func(idx int, src dao.PublishQuestion) domain.Question {
return c.toDomain(dao.Question(src))
}), err
}

func (c *CachedRepository) PubTotal(ctx context.Context) (int64, error) {
res, err := c.cache.GetTotal(ctx)
if err == nil {
return res, err
}
res, err = c.dao.PubCount(ctx)
if err != nil {
return 0, err
}
err = c.cache.SetTotal(ctx, res)
if err != nil {
c.logger.Error("更新缓存中的总数失败", elog.FieldErr(err))
}
return res, nil
}

func (c *CachedRepository) toDomainWithAnswer(que dao.Question, eles []dao.AnswerElement) domain.Question {
res := c.toDomain(que)
for _, ele := range eles {
Expand Down
3 changes: 2 additions & 1 deletion internal/question/internal/service/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Service interface {
// Delete 会直接删除制作库和线上库的数据
Delete(ctx context.Context, qid int64) error

// PubList 只会返回八股文的数据
PubList(ctx context.Context, offset int, limit int) ([]domain.Question, error)
// GetPubByIDs 目前只会获取基础信息,也就是不包括答案在内的信息
GetPubByIDs(ctx context.Context, ids []int64) ([]domain.Question, error)
Expand Down Expand Up @@ -102,7 +103,7 @@ func (s *service) List(ctx context.Context, offset int, limit int) ([]domain.Que
}

func (s *service) PubList(ctx context.Context, offset int, limit int) ([]domain.Question, error) {
return s.repo.PubList(ctx, offset, limit)
return s.repo.PubList(ctx, offset, limit, domain.DefaultBiz)
}

func (s *service) Save(ctx context.Context, question *domain.Question) (int64, error) {
Expand Down
Loading