From de5b6e207c69dbba97b29bfba181bca213fecca5 Mon Sep 17 00:00:00 2001 From: viletyy Date: Fri, 28 Jun 2019 10:37:22 +0800 Subject: [PATCH] =?UTF-8?q?[ADD]=E6=96=87=E7=AB=A0=E5=B8=A6=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/index.go | 6 ++++-- models/note.go | 13 ++++++++----- views/index.html | 4 ++-- views/shares/header.html | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/controllers/index.go b/controllers/index.go index 4a89d3e..a5df817 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -16,14 +16,15 @@ func (c *IndexController) Get() { page = 1 } - notes, err := models.QueryNoteByPage(page, limit) + title := c.GetString("title") + notes, err := models.QueryNoteByPage(title, page, limit) if err != nil { c.Abort500(err) } c.Data["notes"] = notes // 得到文章的总数 - count, err := models.QueryNoteCount() + count, err := models.QueryNoteCount(title) if err != nil { c.Abort500(err) } @@ -35,6 +36,7 @@ func (c *IndexController) Get() { c.Data["totalPage"] = totalPage c.Data["page"] = page + c.Data["title"] = title c.TplName = "index.html" } diff --git a/models/note.go b/models/note.go index 9e5a9be..55352f5 100644 --- a/models/note.go +++ b/models/note.go @@ -1,6 +1,9 @@ package models -import "github.com/jinzhu/gorm" +import ( + "fmt" + "github.com/jinzhu/gorm" +) type Note struct { gorm.Model @@ -19,13 +22,13 @@ func QueryNoteByKeyAndUserId(key string, userId int) (note Note, err error) { return note, err } -func QueryNoteByPage(page, limit int) (notes []*Note, err error) { - err = db.Offset((page - 1) * limit).Limit(limit).Find(¬es).Error +func QueryNoteByPage(title string, page int, limit int) (notes []*Note, err error) { + err = db.Where("title like ?", fmt.Sprintf("%%%s%%", title)).Offset((page - 1) * limit).Limit(limit).Find(¬es).Error return notes, err } -func QueryNoteCount() (count int, err error) { - err = db.Model(&Note{}).Count(&count).Error +func QueryNoteCount(title string) (count int, err error) { + err = db.Model(&Note{}).Where("title like ?", fmt.Sprintf("%%%s%%", title)).Count(&count).Error return count, err } diff --git a/views/index.html b/views/index.html index bffefc3..f61680b 100755 --- a/views/index.html +++ b/views/index.html @@ -31,11 +31,11 @@
{{ if gt .page 1 }} - + {{ end }} {{ if lt .page .totalPage }} - + {{ end }}
diff --git a/views/shares/header.html b/views/shares/header.html index 086d6e7..beb17cb 100644 --- a/views/shares/header.html +++ b/views/shares/header.html @@ -6,11 +6,11 @@ -
+
- +