Exemplo n.º 1
0
func (this *MainController) DeletePost() {
	res := &ResEntity{}
	id, err := strconv.Atoi(this.Ctx.Input.Param(":id"))
	if err != nil {
		beego.Error(err)
	}
	post := models.Post{Id: int64(id)}
	err = post.Delete()
	if err != nil {
		beego.Error("delete post error:", err.Error())
		res.Success = false
		res.Msg = "删除失败"
		this.Data["json"] = res
		this.ServeJson()
		return
	}

	o := orm.NewOrm()
	o.Raw("DELETE FROM message WHERE post_id=?", id).Exec()

	res.Success = true
	res.Msg = "删除成功"
	this.Data["json"] = res
	this.ServeJson()
	return
}
Exemplo n.º 2
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
}
Exemplo n.º 3
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
}