分享
  1. 首页
  2. 文章

【Go web开发之revel+mgo】第7章 实现归档

joveth · · 3559 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

1.设计归档页面

首先在开始之前,我要说一下,有的同学在做的时候可能发现import的时候,一会是"MyTest/app/models",一会是"GBlog/app/models" 这是我的错,没有说明,我自己完成的是GBlog项目,现在做的教程是拿MyTest这个给大家做的,所以把前面改成你的项目名称就行,不用跟我这一样。为什么要特别说一下呢,我怕有些同学,做的时候出了错,而找不到原因,自己明明跟着教程做的为什么是错的呢。这样可能打击他的兴趣。所以这里说明一下。本来我想回去都改一下的,毕竟也花不了多久时间,但是毕竟有些问题还是需要大家自己解决的,所以才在这里说明。

好下面我们来做归档页面,在views/App下新建History.html,内容:

{{set . "title" "History blogs - GBlog"}}
{{set . "history" "active" }}
{{template "header.html" .}}
<div class="content">
 <div class="history-nav" id="his">
 <div class="history-title">
 博客归档
 </div>
 <div class="history-cell">
 <div class="panel-heading" style="padding:0;border-bottom: 1px dashed #ccc;">
 <a data-toggle="collapse" data-toggle="collapse" data-parent="#his" href="#collapseOne">2014</a>
 </div>
 <div id="collapseOne" class="panel-collapse collapse in">
 <div class="panel-body" style="padding:0 20px;">
 <ul style="padding:10px 10px;list-style:none;">
 <li><time>2014年04月01日</time><a href="#">Abourt the blog</a><span class="history-auth">By jov</span></li>
 </ul>
 </div>
 </div>
 </div>
 </div>
</div>
{{template "footer.html" .}}

在controllers/app.go添加方法:
func (c App) History() revel.Result {
	
	return c.Render()
}

好,下面添加我们的路由,conf/routes:
GET /history App.History

看一下效果:


下面我们来实现它,在models里面新建history.go 内容:

package models
import (
	"github.com/revel/revel"
	"labix.org/v2/mgo/bson"
	"time"
)
type History struct {
	Year int
	Blogs []Blog
}
func (dao *Dao) InsertHistory(history *History) error {
	historyCollection := dao.session.DB(DbName).C(HistoryCollection)
	err := historyCollection.Insert(history)
	if err != nil {
		revel.WARN.Printf("Unable to save History: %v error %v", history, err)
	}
	return err
}
func (dao *Dao) FindHistory() []History{
	historyCollection := dao.session.DB(DbName).C(HistoryCollection)
	his := []History{}
	query := historyCollection.Find(bson.M{}).Sort("-year")
	query.All(&his)
	return his
}
func (dao *Dao) RemoveAll() error{
	historyCollection := dao.session.DB(DbName).C(HistoryCollection)
	_,err := historyCollection.RemoveAll(bson.M{})
	if err != nil {
		revel.WARN.Printf("Unable to RemoveAll: error %v", err)
	}
	return err
}
func (dao *Dao) CreateAllHistory() {
	dao.RemoveAll();
	var end int = time.Now().Year();
	for i:=BaseYear;i<=end;i++{
		history := new(History);
		history.Year = i;
		dao.InsertHistory(history);
	}
}

这个model只有两个属性,但是保存到db中的只有year属性,看一下最后的方法,我们以BaseYear为基础,判断当前年与baseyear之间的差,然后做归档。

好,在controllers/app.go的History方法中,做如下修改:

func (c App) History() revel.Result {
	dao, err := models.NewDao()
	if err != nil {
		c.Response.Status = 500
		return c.RenderError(err)
	}
	defer dao.Close()
	dao.CreateAllHistory();
	historys := dao.FindHistory();
	for i,_ := range historys{
		historys[i].Blogs =dao.FindBlogsByYear(historys[i].Year);	
	}
	return c.Render(historys)
}

这里又多了一个方法,按年查询blog,下面我们在models/blog.go中添加方法:
func (dao *Dao) FindBlogsByYear(year int) []Blog{
	blogCollection := dao.session.DB(DbName).C(BlogCollection)
	blogs := []Blog{}
	query := blogCollection.Find(bson.M{"year":year}).Sort("-cdate")
	query.All(&blogs)
	return blogs
}

好的,修改我们的页面,views/App/History.html:
{{set . "title" "History blogs - GBlog"}}
{{set . "history" "active" }}
{{template "header.html" .}}
<div class="content">
 <div class="history-nav" id="his">
 <div class="history-title">
 博客归档
 </div>
 {{if .historys}}
 {{range $index,$history :=.historys}}
 <div class="history-cell">
 <div class="panel-heading" style="padding:0;border-bottom: 1px dashed #ccc;">
 <a data-toggle="collapse" data-toggle="collapse" data-parent="#his" href="#collapseOne{{$index}}">{{$history.Year}}</a>
 </div>
 <div id="collapseOne{{$index}}" class="panel-collapse collapse in">
 <div class="panel-body" style="padding:0 20px;">
 <ul style="padding:10px 10px;list-style:none;">
 {{if $history.Blogs }}
 {{range $blog :=$history.Blogs}}
 <li><time>{{$blog.CDate.Format "2006年01月02日"}}</time><a href="#">{{$blog.GetShortTitle }}</a><span class="history-auth">By {{$blog.Email}}</span></li>
 {{end}}
 {{end}}
 </ul>
 </div>
 </div>
 </div>
 {{end}}
 {{end}}
 </div>
</div>
{{template "footer.html" .}}



好了,ok,来看看结果:


你是否已经ok了呢?


交流QQ:158325682


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:joveth

查看原文:【Go web开发之revel+mgo】第7章 实现归档

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
3559 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏