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 @@