github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/routers/root/RUpdateTopic.go (about)

     1  package root
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/insionng/makross"
     6  	
     7  	"strconv"
     8  	"github.com/insionng/yougam/helper"
     9  	"github.com/insionng/yougam/models"
    10  )
    11  
    12  func GetRUpdateTopicHandler(self *makross.Context) error {
    13  	
    14  
    15  	self.Set("catpage", "RUpdateTopicHandler")
    16  	if tid := self.Param("tid").MustInt64(); tid > 0 {
    17  		if tp, err := models.GetTopic(tid); tp != nil && err == nil {
    18  			self.Set("topic", *tp)
    19  			self.Set("images", tp.Attachment)
    20  		} else {
    21  			self.Flash.Error(err.Error())
    22  			return err
    23  		}
    24  	}
    25  
    26  	if nds, err := models.GetNodes(0, 0, "id"); nds != nil && err == nil {
    27  		self.Set("nodes", *nds)
    28  	}
    29  
    30  	TplNames := "root/update_topic"
    31  	return self.Render(TplNames)
    32  
    33  }
    34  
    35  func PostRUpdateTopicHandler(self *makross.Context) error {
    36  
    37  	
    38  	_usr_, okay := self.Session.Get("SignedUser").(*models.User)
    39  	if !okay {
    40  		return self.NoContent(401)
    41  	}
    42  
    43  	self.Set("catpage", "RUpdateTopicHandler")
    44  	TplNames := "root/update_topic"
    45  	tid := self.Param("tid").MustInt64()
    46  
    47  	title := self.FormValue("title")
    48  	images := self.FormValue("images")
    49  
    50  	policy := helper.ObjPolicy()
    51  	content := policy.Sanitize(self.FormValue("content"))
    52  
    53  	cid := self.Args("cid").MustInt64()
    54  	nid := self.Args("nodeid").MustInt64()
    55  	pid := self.Args("pid").MustInt64()
    56  	uid := _usr_.Id //当前管理员uid
    57  	if tid > 0 {
    58  		if tp, err := models.GetTopic(tid); tp != nil && err == nil {
    59  			tp.Title = title
    60  			tp.Content = content
    61  			tp.Attachment = images
    62  			tp.Pid = pid
    63  			tp.Cid = cid
    64  			tp.Nid = nid
    65  			if tp.Uid <= 0 {
    66  				tp.Uid = uid //uid默认不作修改,不然会把用户的uid替换掉,当uid为0才设为管理员uid
    67  			}
    68  			if nd, e := models.GetNode(nid); nd != nil && e == nil {
    69  				tp.Node = nd.Title
    70  			}
    71  
    72  			if tid, err := models.PutTopic(tid, tp); err != nil || tid <= 0 {
    73  				self.Flash.Error(fmt.Sprint("更新话题出现错误:", err))
    74  
    75  			} else {
    76  				self.Flash.Success("更新话题成功!")
    77  			}
    78  
    79  		}
    80  
    81  		return self.Redirect("/root/read/topic/" + strconv.FormatInt(tid, 10) + "/")
    82  
    83  	} else {
    84  		self.Flash.Error("更新话题出现错误:不存在该话题!")
    85  		return self.Redirect("/root/read/node/")
    86  
    87  	}
    88  	return self.Render(TplNames)
    89  
    90  }