Example #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
}