func (c Topic) EditPost(id int64, topic models.Topic, category int64) revel.Result { user := c.user() if has, _ := engine.Where("id = ? AND user_id = ?", id, user.Id).Get(&models.Topic{}); !has { return c.Forbidden("抱歉,您没有权限") } topic.Validate(c.Validation) if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Topic.Edit(id)) } topic.Category = models.Category{Id: category} aff, _ := engine.Id(id).Cols("title", "category_id", "content").Update(&topic) if aff > 0 { c.Flash.Success("编辑帖子成功") cache.Flush() } else { c.Flash.Error("编辑帖子失败") } return c.Redirect(routes.Topic.Show(id)) }
func (c Admin) EditCategoryPost(id int64, category models.Category) revel.Result { category.Id = id category.Validate(c.Validation) if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Admin.EditCategory(id)) } aff, _ := engine.Id(id).Update(&category) if aff > 0 { c.Flash.Success("编辑分类成功") cache.Flush() } else { c.Flash.Error("编辑分类失败") return c.Redirect(routes.Admin.EditCategory(id)) } return c.Redirect(routes.Admin.ListCategory()) }
func (c Topic) NewPost(topic models.Topic, category int64) revel.Result { topic.Validate(c.Validation) if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Topic.New()) } topic.User = models.User{Id: c.user().Id} topic.Category = models.Category{Id: category} aff, _ := engine.Insert(&topic) if aff > 0 { c.Flash.Success("发表新帖成功") cache.Flush() } else { c.Flash.Error("发表新帖失败") } return c.Redirect(routes.Topic.Index(1)) }
// 回复帖子 func (c Topic) Reply(id int64, content string) revel.Result { c.Validation.Required(content).Message("请填写回复内容") if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Topic.Show(id)) } aff, _ := engine.Insert(&models.Reply{ Topic: models.Topic{Id: id}, User: models.User{Id: c.user().Id}, Content: content, }) if aff > 0 { engine.Exec("UPDATE topic SET replies = replies + 1 WHERE id = ?", id) cache.Flush() } else { c.Flash.Error("发表回复失败") } return c.Redirect(routes.Topic.Show(id)) }