update page

This commit is contained in:
aichy126 2022-11-30 16:18:23 +08:00
parent 7cad11e725
commit a20105321d
8 changed files with 119 additions and 79 deletions

View File

@ -190,7 +190,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
uiRouter := router.NewUIRouter()
authUserMiddleware := middleware.NewAuthUserMiddleware(authService)
avatarMiddleware := middleware.NewAvatarMiddleware(serviceConf, uploaderService)
templateRenderController := templaterender.NewTemplateRenderController(questionService, userService)
templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService)
templateController := controller.NewTemplateController(templateRenderController)
templateRouter := router.NewTemplateRouter(templateController, templateRenderController)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware, templateRouter)

View File

@ -5,9 +5,11 @@ import (
"net/http"
"regexp"
"github.com/answerdev/answer/internal/base/handler"
templaterender "github.com/answerdev/answer/internal/controller/template_render"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/ui"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
)
@ -68,9 +70,26 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
// TagList tags list
func (tc *TemplateController) TagList(ctx *gin.Context) {
req := &schema.GetTagWithPageReq{}
if handler.BindAndCheck(ctx, req) {
return
}
data, err := tc.templateRenderController.TagList(ctx, req)
if err != nil {
ctx.HTML(http.StatusOK, "404.html", gin.H{
"scriptPath": tc.scriptPath,
"cssPath": tc.cssPath,
"err": err.Error(),
})
return
}
page := templaterender.Paginator(req.Page, req.PageSize, data.Count)
spew.Dump(page)
ctx.HTML(http.StatusOK, "tags.html", gin.H{
"scriptPath": tc.scriptPath,
"cssPath": tc.cssPath,
"page": page,
"data": data,
})
}

View File

@ -3,7 +3,9 @@ package templaterender
import (
"math"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service"
"github.com/answerdev/answer/internal/service/tag"
"github.com/google/wire"
)
@ -15,16 +17,19 @@ var ProviderSetTemplateRenderController = wire.NewSet(
type TemplateRenderController struct {
questionService *service.QuestionService
userService *service.UserService
tagService *tag.TagService
}
func NewTemplateRenderController(
questionService *service.QuestionService,
userService *service.UserService,
tagService *tag.TagService,
) *TemplateRenderController {
return &TemplateRenderController{
questionService: questionService,
userService: userService,
tagService: tagService,
}
}
@ -33,7 +38,10 @@ func NewTemplateRenderController(
// prepage : Number per page
// nums : Total
// Returns the contents of the page in the format of 1, 2, 3, 4, and 5. If the contents are less than 5 pages, the page number is returned
func Paginator(page, prepage int, nums int64) map[string]interface{} {
func Paginator(page, prepage int, nums int64) *schema.Paginator {
if prepage == 0 {
prepage = 10
}
var firstpage int //Previous page address
var lastpage int //Address on the last page
@ -72,11 +80,11 @@ func Paginator(page, prepage int, nums int64) map[string]interface{} {
firstpage = int(math.Max(float64(1), float64(page-1)))
lastpage = page + 1
}
paginatorMap := make(map[string]interface{})
paginatorMap["pages"] = pages
paginatorMap["totalpages"] = totalpages
paginatorMap["firstpage"] = firstpage
paginatorMap["lastpage"] = lastpage
paginatorMap["currpage"] = page
return paginatorMap
paginator := &schema.Paginator{}
paginator.Pages = pages
paginator.Totalpages = totalpages
paginator.Firstpage = firstpage
paginator.Lastpage = lastpage
paginator.Currpage = page
return paginator
}

View File

@ -1,12 +0,0 @@
package templaterender
import (
"testing"
"github.com/davecgh/go-spew/spew"
)
func TestPaginator(t *testing.T) {
list := Paginator(5, 20, 300)
spew.Dump(list)
}

View File

@ -0,0 +1,12 @@
package templaterender
import (
"github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/schema"
"golang.org/x/net/context"
)
func (q *TemplateRenderController) TagList(ctx context.Context, req *schema.GetTagWithPageReq) (resp *pager.PageModel, err error) {
resp, err = q.tagService.GetTagWithPage(ctx, req)
return resp, err
}

View File

@ -0,0 +1,9 @@
package schema
type Paginator struct {
Pages []int
Totalpages int
Firstpage int
Lastpage int
Currpage int
}

33
ui/template/page.html Normal file
View File

@ -0,0 +1,33 @@
{{define "page"}}
<ul class="d-inline-flex mb-0 pagination pagination-sm">
{{$currpage := .page.Currpage}}
{{$totalpages := .page.Totalpages}}
{{$firstpage := .page.Firstpage}}
{{$lastpage := .page.Lastpage}}
{{ if ne $currpage 1 }}
<li class="page-item">
<a class="page-link" href="?page={{$firstpage}}"><span aria-hidden="true">Prev</span><span
class="visually-hidden">Prev</span></a>
</li>
{{ end }}
{{ range $value := .page.Pages }}
{{ if eq $currpage $value }}
<li class="page-item active">
<span class="page-link" href="?page={{$value}}">{{$value}}
<span class="visually-hidden">(current)</span>
</span>
</li>
{{ else }}
<li class="page-item">
<a class="page-link" href="?page={{$value}}">{{$value}}</a>
</li>
{{ end }}
{{ end }}
{{ if lt $currpage $totalpages }}
<li class="page-item">
<a class="page-link" href="?page={{$lastpage}}"><span aria-hidden="true">Next</span><span
class="visually-hidden">Next</span></a>
</li>
{{ end }}
</ul>
{{end}}

View File

@ -1,5 +1,5 @@
{{template "header" . }}
<div style="
<div style="
position: fixed;
top: 90px;
left: 0px;
@ -7,69 +7,40 @@
margin: auto;
z-index: 5;
">
<div class="d-flex justify-content-center"></div>
</div>
<div class="py-3 my-3 container">
<div class="mb-4 d-flex justify-content-center row">
<div class="col-xxl-10 col-sm-12">
<h3 class="mb-4">Tags</h3>
</div>
<div class="mt-4 col-xxl-10 col-sm-12">
<div class="row">
<div class="mb-4 col-lg-3 col-md-4 col-sm-6 col-12">
<div class="h-100 card">
<div class="d-flex flex-column align-items-start card-body">
<a href="/tags/%E7%AE%97%E6%B3%95" class="badge-tag rounded-1 mb-3"><span
class="">算法</span></a>
<p class="fs-14 flex-fill text-break text-wrap text-truncate-4">212121212121221
</p>
<div class="d-flex align-items-center">
<span class="text-secondary fs-14 text-nowrap">0
questions</span>
</div>
</div>
</div>
<div class="d-flex justify-content-center"></div>
</div>
<div class="py-3 my-3 container">
<div class="mb-4 d-flex justify-content-center row">
<div class="col-xxl-10 col-sm-12">
<h3 class="mb-4">Tags</h3>
</div>
<div class="mt-4 col-xxl-10 col-sm-12">
<div class="row">
{{ range .data.List }}
<div class="mb-4 col-lg-3 col-md-4 col-sm-6 col-12">
<div class="h-100 card">
<div class="d-flex flex-column align-items-start card-body">
<a href="/tags/{{.SlugName}}" class="badge-tag rounded-1 mb-3"><span
class="">{{.SlugName}}</span></a>
<p class="fs-14 flex-fill text-break text-wrap text-truncate-4">{{.ParsedText}}
</p>
<div class="d-flex align-items-center">
<span class="text-secondary fs-14 text-nowrap">{{.QuestionCount}}
questions</span>
</div>
<div class="mb-4 col-lg-3 col-md-4 col-sm-6 col-12">
<div class="h-100 card">
<div class="d-flex flex-column align-items-start card-body">
<a href="/tags/%E7%AE%97%E6%B3%95" class="badge-tag rounded-1 mb-3"><span
class="">算法</span></a>
<p class="fs-14 flex-fill text-break text-wrap text-truncate-4">212121212121221
</p>
<div class="d-flex align-items-center">
<span class="text-secondary fs-14 text-nowrap">0
questions</span>
</div>
</div>
</div>
</div>
</div>
<div class="d-flex justify-content-center">
<ul class="d-inline-flex mb-0 pagination pagination-sm">
<li class="page-item active">
<span class="page-link" href="/tags?page=1">1<span
class="visually-hidden">(current)</span></span>
</li>
<li class="page-item">
<a class="page-link" href="/tags?page=2">2</a>
</li>
<li class="page-item">
<a class="page-link" href="/tags?page=2"><span aria-hidden="true">Next</span><span
class="visually-hidden">Next</span></a>
</li>
</ul>
</div>
</div>
</div>
{{ end }}
</div>
<div class="d-flex justify-content-center">
{{template "page" .}}
</div>
</div>
</div>
</div>
{{template "footer" .}}