Skip to content

Commit

Permalink
给列表添加总数
Browse files Browse the repository at this point in the history
  • Loading branch information
juniaoshaonian committed Jan 5, 2025
1 parent bb59c25 commit 79e15a0
Show file tree
Hide file tree
Showing 29 changed files with 504 additions and 334 deletions.
2 changes: 2 additions & 0 deletions internal/cases/internal/integration/case_set_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ func (s *CaseSetTestSuite) TestCaseSet_ListAllCaseSets() {
wantCode: 200,
wantResp: test.Result[web.CaseSetList]{
Data: web.CaseSetList{
Total: 100,
CaseSets: []web.CaseSet{
{
Id: 100,
Expand Down Expand Up @@ -636,6 +637,7 @@ func (s *CaseSetTestSuite) TestCaseSet_ListAllCaseSets() {
wantCode: 200,
wantResp: test.Result[web.CaseSetList]{
Data: web.CaseSetList{
Total: 100,
CaseSets: []web.CaseSet{
{
Id: 1,
Expand Down
2 changes: 2 additions & 0 deletions internal/cases/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (s *HandlerTestSuite) TestPubList() {
wantCode: 200,
wantResp: test.Result[web.CasesList]{
Data: web.CasesList{
Total: 100,
Cases: []web.Case{
{
Id: 100,
Expand Down Expand Up @@ -191,6 +192,7 @@ func (s *HandlerTestSuite) TestPubList() {
wantCode: 200,
wantResp: test.Result[web.CasesList]{
Data: web.CasesList{
Total: 100,
Cases: []web.Case{
{
Id: 1,
Expand Down
6 changes: 5 additions & 1 deletion internal/cases/internal/repository/case_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type CaseSetRepository interface {
Total(ctx context.Context) (int64, error)
List(ctx context.Context, offset int, limit int) ([]domain.CaseSet, error)
UpdateNonZero(ctx context.Context, set domain.CaseSet) error

CountByBiz(ctx context.Context, biz string) (int64, error)
GetByIDs(ctx context.Context, ids []int64) ([]domain.CaseSet, error)
// GetByIDsWithCases 会同步把关联的 Case 也找出来,但是只是找 id,具体内容没有找
GetByIDsWithCases(ctx context.Context, ids []int64) ([]domain.CaseSet, error)
Expand All @@ -32,6 +32,10 @@ type caseSetRepo struct {
dao dao.CaseSetDAO
}

func (c *caseSetRepo) CountByBiz(ctx context.Context, biz string) (int64, error) {
return c.dao.CountByBiz(ctx, biz)
}

func NewCaseSetRepo(caseSetDao dao.CaseSetDAO) CaseSetRepository {
return &caseSetRepo{
dao: caseSetDao,
Expand Down
5 changes: 5 additions & 0 deletions internal/cases/internal/repository/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type CaseRepo interface {
PubList(ctx context.Context, offset int, limit int) ([]domain.Case, error)
GetPubByID(ctx context.Context, caseId int64) (domain.Case, error)
GetPubByIDs(ctx context.Context, ids []int64) ([]domain.Case, error)
PubCount(ctx context.Context) (int64, error)
// Sync 保存到制作库,而后同步到线上库
Sync(ctx context.Context, ca domain.Case) (int64, error)
// 管理端接口
Expand All @@ -35,6 +36,10 @@ type caseRepo struct {
caseDao dao.CaseDAO
}

func (c *caseRepo) PubCount(ctx context.Context) (int64, error) {
return c.caseDao.PublishCaseCount(ctx)
}

func (c *caseRepo) Ids(ctx context.Context) ([]int64, error) {
return c.caseDao.Ids(ctx)
}
Expand Down
10 changes: 10 additions & 0 deletions internal/cases/internal/repository/dao/cases_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type CaseSetDAO interface {
UpdateCasesByID(ctx context.Context, id int64, cids []int64) error

Count(ctx context.Context) (int64, error)
CountByBiz(ctx context.Context, biz string) (int64, error)
List(ctx context.Context, offset, limit int) ([]CaseSet, error)
UpdateNonZero(ctx context.Context, set CaseSet) error
GetByIDs(ctx context.Context, ids []int64) ([]CaseSet, error)
Expand All @@ -29,6 +30,15 @@ type caseSetDAO struct {
db *egorm.Component
}

func (c *caseSetDAO) CountByBiz(ctx context.Context, biz string) (int64, error) {
var count int64
db := c.db.WithContext(ctx)
err := db.
Model(&CaseSet{}).
Where("biz = ?", biz).Count(&count).Error
return count, err
}

func (c *caseSetDAO) GetRefCasesByIDs(ctx context.Context, ids []int64) ([]CaseSetCase, error) {
var res []CaseSetCase
err := c.db.WithContext(ctx).Where("cs_id IN ?", ids).Find(&res).Error
Expand Down
21 changes: 18 additions & 3 deletions internal/cases/internal/service/case_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type CaseSetService interface {
GetByIdsWithCases(ctx context.Context, ids []int64) ([]domain.CaseSet, error)

ListByBiz(ctx context.Context, offset, limit int, biz string) ([]domain.CaseSet, error)
ListDefault(ctx context.Context, offset, limit int) ([]domain.CaseSet, error)
ListDefault(ctx context.Context, offset, limit int) (int64, []domain.CaseSet, error)
GetByBiz(ctx context.Context, biz string, bizId int64) (domain.CaseSet, error)
GetCandidates(ctx context.Context, id int64, offset int, limit int) ([]domain.Case, int64, error)
}
Expand Down Expand Up @@ -64,8 +64,23 @@ func (c *caseSetSvc) GetCandidates(ctx context.Context, id int64, offset int, li
return c.caRepo.Exclude(ctx, cids, offset, limit)
}

func (c *caseSetSvc) ListDefault(ctx context.Context, offset, limit int) ([]domain.CaseSet, error) {
return c.repo.ListByBiz(ctx, offset, limit, domain.DefaultBiz)
func (c *caseSetSvc) ListDefault(ctx context.Context, offset, limit int) (int64, []domain.CaseSet, error) {
var (
eg errgroup.Group
total int64
css []domain.CaseSet
)
eg.Go(func() error {
var err error
css, err = c.repo.ListByBiz(ctx, offset, limit, domain.DefaultBiz)
return err
})
eg.Go(func() error {
var err error
total, err = c.repo.CountByBiz(ctx, domain.DefaultBiz)
return err
})
return total, css, eg.Wait()
}

func (c *caseSetSvc) ListByBiz(ctx context.Context, offset, limit int, biz string) ([]domain.CaseSet, error) {
Expand Down
22 changes: 19 additions & 3 deletions internal/cases/internal/service/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Service interface {
Publish(ctx context.Context, ca domain.Case) (int64, error)
List(ctx context.Context, offset int, limit int) ([]domain.Case, int64, error)

PubList(ctx context.Context, offset int, limit int) ([]domain.Case, error)
PubList(ctx context.Context, offset int, limit int) (int64, []domain.Case, error)
GetPubByIDs(ctx context.Context, ids []int64) ([]domain.Case, error)
Detail(ctx context.Context, caseId int64) (domain.Case, error)
PubDetail(ctx context.Context, caseId int64) (domain.Case, error)
Expand Down Expand Up @@ -84,8 +84,24 @@ func (s *service) List(ctx context.Context, offset int, limit int) ([]domain.Cas
return caseList, total, nil
}

func (s *service) PubList(ctx context.Context, offset int, limit int) ([]domain.Case, error) {
return s.repo.PubList(ctx, offset, limit)
func (s *service) PubList(ctx context.Context, offset int, limit int) (int64, []domain.Case, error) {
var (
eg errgroup.Group
cas []domain.Case
total int64
)
eg.Go(func() error {
var err error
cas, err = s.repo.PubList(ctx, offset, limit)
return err
})
eg.Go(func() error {
var err error
total, err = s.repo.PubCount(ctx)
return err
})
return total, cas, eg.Wait()

}

func (s *service) Detail(ctx context.Context, caseId int64) (domain.Case, error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/cases/internal/web/case_set_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (h *CaseSetHandler) PrivateRoutes(server *gin.Engine) {

// ListCaseSets 展示个人案例集
func (h *CaseSetHandler) ListCaseSets(ctx *ginx.Context, req Page) (ginx.Result, error) {
data, err := h.svc.ListDefault(ctx, req.Offset, req.Limit)
count, data, err := h.svc.ListDefault(ctx, req.Offset, req.Limit)
if err != nil {
return systemErrorResult, err
}
Expand All @@ -67,6 +67,7 @@ func (h *CaseSetHandler) ListCaseSets(ctx *ginx.Context, req Page) (ginx.Result,
}
return ginx.Result{
Data: CaseSetList{
Total: count,
CaseSets: slice.Map(data, func(idx int, src domain.CaseSet) CaseSet {
qs := newCaseSet(src)
qs.Interactive = newInteractive(intrs[src.ID])
Expand Down
3 changes: 2 additions & 1 deletion internal/cases/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *Handler) MemberRoutes(server *gin.Engine) {
}

func (h *Handler) PubList(ctx *ginx.Context, req Page) (ginx.Result, error) {
data, err := h.svc.PubList(ctx, req.Offset, req.Limit)
count, data, err := h.svc.PubList(ctx, req.Offset, req.Limit)
if err != nil {
return systemErrorResult, err
}
Expand All @@ -78,6 +78,7 @@ func (h *Handler) PubList(ctx *ginx.Context, req Page) (ginx.Result, error) {
}
return ginx.Result{
Data: CasesList{
Total: count,
Cases: slice.Map(data, func(idx int, ca domain.Case) Case {
return Case{
Id: ca.Id,
Expand Down
Loading

0 comments on commit 79e15a0

Please sign in to comment.