コード例 #1
0
ファイル: method.go プロジェクト: elvizlai/MGBlog
//通过id获取文章,static为是否统计的标识
func GetArticleById(id bson.ObjectId, static ...bool) *Article {
	art := new(Article)
	model.ArticleC.Do(func(c *mgo.Collection) {
		if c.FindId(id).One(art) != nil {
			art = nil
		} else {
			//previous
			previous := new(Article)
			if c.Find(bson.M{"createtime": bson.M{"$gt": art.CreateTime}}).Sort("createtime").One(previous) == nil {
				art.Previous = previous
			}
			//next
			next := new(Article)
			if c.Find(bson.M{"createtime": bson.M{"$lt": art.CreateTime}}).Sort("-createtime").One(next) == nil {
				art.Next = next
			}

			if u := user.GetUserById(art.UId); u != nil {
				art.AuthStr = u.NickName
			}

			if static != nil {
				c.UpdateId(id, bson.M{"$inc": bson.M{"pv": 1}})
			}
		}
	})
	return art
}
コード例 #2
0
ファイル: Base.go プロジェクト: elvizlai/MGBlog
//是否登录
func (this *BaseController) Prepare() {
	if this.Ctx.Input.Method() == "GET" {
		this.Layout = "Layout.html"
		this.Data["visitCount"] = static.GetVisitCount() //todo easy
	}

	if uId := this.GetSession("uId"); uId != nil {
		if this.CurrentUser = user.GetUserById(bson.ObjectIdHex(uId.(string))); this.CurrentUser.Token == this.StartSession().SessionID() {
			this.Data["isLogin"] = true
		} else {
			this.CurrentUser = nil
		}
	}
}