github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/routers/root/RDeleteTopic.go (about) 1 package root 2 3 import ( 4 "fmt" 5 "github.com/insionng/makross" 6 7 "strconv" 8 "time" 9 "github.com/insionng/yougam/models" 10 ) 11 12 func GetRDeleteTopicHandler(self *makross.Context) error { 13 14 15 _usr_, okay := self.Session.Get("SignedUser").(*models.User) 16 if !okay { 17 return self.NoContent(401) 18 } 19 20 uid := _usr_.Id 21 role := _usr_.Role 22 if tid := self.Param("tid").MustInt64(); tid > 0 { 23 24 if tp, err := models.GetTopic(tid); tp != nil && err == nil { 25 if tp.Pid == 0 { //如果是主题 就删光下属所有话题 26 27 if tps := models.GetTopicsByPid(tid, 0, 0, 0, "id"); tps != nil { 28 for _, g := range *tps { 29 models.DelTopic(g.Id, uid, role) //该操作是在后台进行 所以设为当前管理员uid 30 models.DelTopicMark(g.Uid, g.Id) //删除该用户的话题收藏 所以这里是g.Uid 31 } 32 33 //删除下属评论 34 models.DelReplysByTid(tid) 35 36 self.Flash.Success("删除 Topic id:" + strconv.FormatInt(tid, 10) + "成功!") 37 return self.Redirect("/root/read/topic/") 38 39 } 40 41 } else { //pid不等于0是子话题 此时只需要删除该话题 上级和其他子话题都不用删除 42 if e := models.DelTopic(tid, uid, role); e != nil { 43 self.Flash.Error("删除 Topic id:" + strconv.FormatInt(tid, 10) + "出现错误 " + fmt.Sprintf("%s", e) + "!") 44 return e 45 } else { 46 47 models.DelTopicMark(tp.Uid, tid) //删除该用户的话题收藏 所以这里是tp.Uid 48 49 //删除下属评论 50 models.DelReplysByTid(tp.Pid) 51 52 self.Flash.Success("删除 Topic id:" + strconv.FormatInt(tid, 10) + "成功!") 53 return self.Redirect("/root/read/topic/") 54 55 } 56 57 } 58 } else { 59 //读取数据异常后返回 60 self.Flash.Error("删除 Topic id:" + strconv.FormatInt(tid, 10) + "出现错误 " + err.Error() + "!") 61 return self.Redirect("/root/read/topic/") 62 63 } 64 } 65 66 return self.Redirect("/root/dashboard/?version=" + strconv.FormatInt(time.Now().Unix(), 10)) 67 68 }