93 lines
1.7 KiB
Go
93 lines
1.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"liteblog/models"
|
|
"liteblog/syserrors"
|
|
)
|
|
|
|
type IndexController struct {
|
|
BaseController
|
|
}
|
|
|
|
//首页
|
|
// @router / [get]
|
|
func (c *IndexController) Get() {
|
|
limit := 10
|
|
|
|
page, err := c.GetInt("page", 1)
|
|
if err != nil && page <= 0 {
|
|
page = 1
|
|
}
|
|
|
|
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(title)
|
|
if err != nil {
|
|
c.Abort500(err)
|
|
}
|
|
// 计算总页数
|
|
totalPage := count / limit
|
|
if count % limit != 0 {
|
|
totalPage += 1
|
|
}
|
|
|
|
c.Data["totalPage"] = totalPage
|
|
c.Data["page"] = page
|
|
c.Data["title"] = title
|
|
|
|
c.TplName = "index.html"
|
|
}
|
|
|
|
// 显示文章
|
|
// @router /details/:key [get]
|
|
func (c *IndexController) GetDetail() {
|
|
// 得到页面传过来的key
|
|
key := c.Ctx.Input.Param(":key")
|
|
// 到数据查询对应key的文章
|
|
note, err := models.QueryNoteByKey(key)
|
|
|
|
if err != nil {
|
|
c.Abort500(syserrors.NewError("文章不存在", err))
|
|
}
|
|
|
|
comments, err := models.QueryCommentByNoteKey(key)
|
|
if err != nil {
|
|
c.Abort500(syserrors.NewError("文章不存在", err))
|
|
}
|
|
|
|
c.Data["note"] = note
|
|
c.Data["comments"] = comments
|
|
c.TplName = "details.html"
|
|
|
|
}
|
|
|
|
// 显示文章评论
|
|
// @router /comment/:key [get]
|
|
func (c *IndexController) GetComment() {
|
|
key := c.Ctx.Input.Param(":key")
|
|
note, err := models.QueryNoteByKey(key)
|
|
|
|
if err != nil {
|
|
c.Abort500(syserrors.NewError("文章不存在", err))
|
|
}
|
|
|
|
c.Data["note"] = note
|
|
c.TplName = "comment.html"
|
|
}
|
|
|
|
//留言
|
|
// @router /message [get]
|
|
func (c *IndexController) GetMessage() {
|
|
c.TplName = "message.html"
|
|
}
|
|
//关于
|
|
// @router /about [get]
|
|
func (c *IndexController) GetAbout() {
|
|
c.TplName = "about.html"
|
|
} |