diff --git a/internal/question/internal/integration/handler_test.go b/internal/question/internal/integration/handler_test.go index 0bbdaf31..1690432c 100644 --- a/internal/question/internal/integration/handler_test.go +++ b/internal/question/internal/integration/handler_test.go @@ -139,7 +139,7 @@ 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), @@ -147,6 +147,19 @@ func (s *HandlerTestSuite) TestPubList() { 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 { @@ -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, @@ -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, @@ -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, diff --git a/internal/question/internal/repository/dao/question.go b/internal/question/internal/repository/dao/question.go index 3e93d671..119a3107 100644 --- a/internal/question/internal/repository/dao/question.go +++ b/internal/question/internal/repository/dao/question.go @@ -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) @@ -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 diff --git a/internal/question/internal/repository/question.go b/internal/question/internal/repository/question.go index 53580ca4..a41973e5 100644 --- a/internal/question/internal/repository/question.go +++ b/internal/question/internal/repository/question.go @@ -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) @@ -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 { diff --git a/internal/question/internal/service/question.go b/internal/question/internal/service/question.go index ba1bf5e2..65d3a23d 100644 --- a/internal/question/internal/service/question.go +++ b/internal/question/internal/service/question.go @@ -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) @@ -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) {