"Go Web Development Revel+mgo" The 9th chapter other and??

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In fact, it can be said that the tutorial has basically ended.

But, do you remember the reminder in the top right corner of our home page? In this chapter, we'll do this thing.

It reminds you that there are updates within the last 1 hours. Well, first of all, there should be a way to query the Blog.go in time.

OK, open the Blog.go Add method:

Func (DAO *dao) findblogsbydate (start time. Time) Int{blogcollection: = Dao.session.DB (DbName). C (blogcollection) Query: = Blogcollection.find (Bson. m{"CDate": Bson. m{"$gte": Start}}) Cnt,err: = Query. Count (); if Err!=nil{revel. WARN. Printf ("Unable to Count blog:error%v", err)}return CNT}

Note that in the Find method of MgO, we have nested two bson. M, the first one is well understood, what is the $gte in the second one? Greater than or equal to (>=) Well, yes, MgO there are other operations, if you need to find a find (say, bloggers in order to find this, but took a time), and finally this method just return a statistic only, yes, we only need this is enough.

In the Controllers/app.go index method, modify:

Func (c App) Index () Revel. Result {DAO, err: = models. Newdao () if err! = Nil {c.response.status = 500return c.rendererror (err)}defer DAO. Close ()//dao: = models. Newdao (c.mongosession) Blogs: = DAO. Findblogs () Now: = time. Now (). ADD ( -1 * time. Hour) recentcnt: =dao. Findblogsbydate (now); return C.render (BLOGS,RECENTCNT)}

The last thing we add a little bit of time to deal with, the current time minus 1 hours, is the result we want. don't forget to add our time package to the import

OK, open the views under the header.html will be inside of:

<li class= "{. Home}" >                <a href= "/" >Home</a> <span class=                "Navbar-news" title= " 1 updates in the last 1 hours ">1</span>              </li>

Modified to:

<li class= "{{. Home}" >                <a href= "/" >Home</a>                {{if. recentcnt}}                {{if GT. recentcnt 0}}                <span class= "Navbar-news" title= "the last 1 hours have {{. recentcnt}} Updates" >{{.recentCnt}}</span>                {{End}}                {{End}}              </li>

OK, give it a try.



Finally, here to say, the last RSS, temporarily do not do, I still do not know how to do, to see the GOLANG.USR subscription, just an XML page, this is not what I want, first of all.

Source Address:Https://github.com/joveth/GBlog

AC qq:158325682


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.