From dc5f6cc0736a4e9ce11bf76912bb325a889c810a Mon Sep 17 00:00:00 2001 From: kumfo Date: Wed, 12 Oct 2022 16:09:20 +0800 Subject: [PATCH] fix: search page --- internal/repo/search_repo.go | 6 +++--- internal/service/search_service.go | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/repo/search_repo.go b/internal/repo/search_repo.go index 52bf43ac..f6277c19 100644 --- a/internal/repo/search_repo.go +++ b/internal/repo/search_repo.go @@ -105,7 +105,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID, return } - res, err := sr.data.DB.Query(builder.MySQL().Select("*").From(b, "t").OrderBy(sr.parseOrder(ctx, order)).Limit(size, page)) + res, err := sr.data.DB.Query(builder.MySQL().Select("*").From(b, "t").OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1)) tr, err := sr.data.DB.Query(builder.MySQL().Select("count(*) total").From(b, "c")) if len(tr) != 0 { @@ -153,7 +153,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limit } else if answers > 0 { b.And(builder.Gte{"answer_count": answers}) } - res, err := sr.data.DB.Query(b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page)) + res, err := sr.data.DB.Query(b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1)) tr, err := sr.data.DB.Query(builder.MySQL().Select("count(*) total").From(b, "c")) @@ -203,7 +203,7 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAc b.Where(builder.Eq{"question_id": questionID}) } - res, err := sr.data.DB.Query(b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page)) + res, err := sr.data.DB.Query(b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1)) tr, err := sr.data.DB.Query(builder.MySQL().Select("count(*) total").From(b, "c")) total = converter.StringToInt64(string(tr[0]["total"])) diff --git a/internal/service/search_service.go b/internal/service/search_service.go index b292a99c..817c2318 100644 --- a/internal/service/search_service.go +++ b/internal/service/search_service.go @@ -57,6 +57,10 @@ func NewSearchService( func (ss *SearchService) Search(ctx context.Context, dto *schema.SearchDTO) (resp []schema.SearchResp, total int64, extra interface{}, err error) { extra = nil + if dto.Page < 1 { + dto.Page = 1 + } + switch { case ss.tagSearch.Parse(dto): resp, total, err = ss.tagSearch.Search(ctx)