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 Topic) Show(id int64) revel.Result { topic := new(models.Topic) str := strconv.Itoa(int(id)) if err := cache.Get("topic"+str, &topic); err != nil { has, _ := c.Engine.Id(id).Get(topic) if !has { return c.NotFound("帖子不存在") } go cache.Set("topic"+str, topic, cache.FOREVER) } topic.Hits += 1 c.Engine.Id(id).Cols("hits").Update(topic) replies := c.getReplies(id) title := topic.Title return c.Render(title, topic, replies) }
func (c Topic) EditPost(id int64, 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.Edit(id)) } topic.Category = models.Category{Id: category} aff, _ := c.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 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, _ := c.Engine.Insert(&topic) if aff > 0 { c.Flash.Success("发表新帖成功") cache.Flush() } else { c.Flash.Error("发表新帖失败") } return c.Redirect(routes.Topic.Index(1)) }