Esempio n. 1
0
func (this *PostController) OnePost() {
	res := &ResEntity{}
	id, err := strconv.Atoi(this.Ctx.Input.Param(":id"))
	if err != nil {
		beego.Error(err)
	}
	qsPost := new(models.Post)
	post := models.Post{Id: int64(id)}
	if id == 0 {
		// last post
		qsPost.Query().OrderBy("-PublishAt").Limit(1).One(&post)
	} else {
		qsPost.Query().RelatedSel().Filter("id", id).One(&post)
	}

	/*if post == nil {
	    res.Success = false
	    res.Msg = "还没有内容"
	    this.Data["json"] = res
	    this.ServeJson()
	    return
	}*/
	messages := []models.Message{}
	qsMessages := new(models.Message)
	qsMessages.Query().Filter("PostId", post.Id).OrderBy("-CreatedAt").All(&messages)
	resPost := &ResPost{
		Post:     &post,
		Messages: &messages,
	}
	res.Success = true
	res.Data = resPost
	this.Data["json"] = res
	this.ServeJson()
	return
}
Esempio n. 2
0
func (this *PostController) Posts() {
	posts := []models.Post{}
	qsPost := new(models.Post)
	qsPost.Query().OrderBy("-PublishAt").All(&posts)
	this.Data["json"] = posts
	this.ServeJson()
	return
}