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 }
func (this *MainController) DeleteMessage() { res := &ResEntity{} id, err := strconv.Atoi(this.Ctx.Input.Param(":id")) if err != nil { beego.Error(err) } message := models.Message{Id: int64(id)} err = message.Delete() if err != nil { beego.Error("delete post error:", err.Error()) res.Success = false res.Msg = "删除失败" this.Data["json"] = res this.ServeJson() return } res.Success = true res.Msg = "删除成功" this.Data["json"] = res this.ServeJson() return }