Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa864ed

Browse files
committed
图书排序方式和分页修改
1 parent 2c06df3 commit aa864ed

File tree

3 files changed

+45
-54
lines changed

3 files changed

+45
-54
lines changed

src/http/controller/book.go

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
package controller
88

99
import (
10+
"html/template"
1011
"logic"
1112
"net/http"
1213

1314
"github.com/labstack/echo"
1415
"github.com/polaris1119/goutils"
15-
"github.com/polaris1119/logger"
1616

1717
. "http"
1818
"model"
@@ -36,55 +36,20 @@ func (self BookController) RegisterRoute(g *echo.Group) {
3636

3737
// ReadList 图书列表页
3838
func (BookController) ReadList(ctx echo.Context) error {
39-
limit := 20
39+
curPage := goutils.MustInt(ctx.QueryParam("p"), 1)
40+
paginator := logic.NewPaginator(curPage)
4041

41-
lastId := goutils.MustInt(ctx.QueryParam("lastid"))
42-
books := logic.DefaultGoBook.FindBy(ctx, limit+1, lastId)
43-
if books == nil {
44-
logger.Errorln("book controller: find book error")
45-
return ctx.Redirect(http.StatusSeeOther, "/books")
46-
}
47-
48-
num := len(books)
49-
if num == 0 {
50-
if lastId == 0 {
51-
return ctx.Redirect(http.StatusSeeOther, "/")
52-
}
53-
return ctx.Redirect(http.StatusSeeOther, "/books")
54-
}
55-
56-
var (
57-
hasPrev, hasNext bool
58-
prevId, nextId int
59-
)
60-
61-
if lastId > 0 {
62-
prevId = lastId
63-
64-
if prevId-books[0].Id > 1 {
65-
hasPrev = false
66-
} else {
67-
prevId += limit
68-
hasPrev = true
69-
}
70-
}
71-
72-
if num > limit {
73-
hasNext = true
74-
books = books[:limit]
75-
nextId = books[limit-1].Id
76-
} else {
77-
nextId = books[num-1].Id
78-
}
42+
books := logic.DefaultGoBook.FindAll(ctx, paginator, "likenum DESC,id DESC")
43+
total := logic.DefaultGoBook.Count(ctx)
44+
pageHtml := paginator.SetTotal(total).GetPageHtml(ctx.Request().URL().Path())
7945

80-
pageInfo := map[string]interface{}{
81-
"has_prev": hasPrev,
82-
"prev_id": prevId,
83-
"has_next": hasNext,
84-
"next_id": nextId,
46+
data := map[string]interface{}{
47+
"books": books,
48+
"activeBooks": "active",
49+
"page": template.HTML(pageHtml),
8550
}
8651

87-
return render(ctx, "books/list.html", map[string]interface{}{"books": books, "activeBooks": "active", "page": pageInfo})
52+
return render(ctx, "books/list.html", data)
8853
}
8954

9055
// Detail 图书详细页

src/logic/gobook.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ func (GoBookLogic) FindBy(ctx context.Context, limit int, lastIds ...int) []*mod
3939
return books
4040
}
4141

42+
// FindAll 支持多页翻看
43+
func (GoBookLogic) FindAll(ctx context.Context, paginator *Paginator, orderBy string) []*model.Book {
44+
objLog := GetLogger(ctx)
45+
46+
bookList := make([]*model.Book, 0)
47+
err := MasterDB.OrderBy(orderBy).Limit(paginator.PerPage(), paginator.Offset()).Find(&bookList)
48+
if err != nil {
49+
objLog.Errorln("GoBookLogic FindAll error:", err)
50+
return nil
51+
}
52+
53+
return bookList
54+
}
55+
56+
func (GoBookLogic) Count(ctx context.Context) int64 {
57+
objLog := GetLogger(ctx)
58+
59+
var (
60+
total int64
61+
err error
62+
)
63+
total, err = MasterDB.Count(new(model.Book))
64+
65+
if err != nil {
66+
objLog.Errorln("GoBookLogic Count error:", err)
67+
}
68+
69+
return total
70+
}
71+
4272
// FindByIds 获取多个图书详细信息
4373
func (GoBookLogic) FindByIds(ids []int) []*model.Book {
4474
if len(ids) == 0 {

template/books/list.html

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,11 @@ <h4><a href="/book/{{.Id}}" target="_blank" title="{{.Name}}">{{.Name}}</a></h4>
4141
</div>
4242
<hr class="dashed">
4343
{{end}}
44-
</div>
45-
<ul class="pager">
46-
{{if .page.has_prev}}
47-
<li class="previous"><a href="/books?lastid={{.page.prev_id}}">&larr; 上一页</a></li>
48-
{{end}}
49-
{{if .page.has_next}}
50-
<li class="next"><a href="/books?lastid={{.page.next_id}}">下一页 &rarr;</a></li>
44+
45+
{{if .page}}
46+
<nav class="text-center"><ul class="pagination pagination-sm">{{.page}}</ul></nav>
5147
{{end}}
52-
</ul>
48+
</div>
5349
</div>
5450
<div class="col-lg-3 col-md-4 col-sm-5">
5551
<div class="row box_white sidebar">

0 commit comments

Comments
 (0)