github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/routers/root/RCreateTopic.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 GetRCreateTopicHandler(self *makross.Context) error {
    13  
    14  	self.Set("haspid", false)
    15  
    16  	self.Set("catpage", "RCreateTopicHandler")
    17  	if pid := self.Param("pid").MustInt64(); pid > 0 {
    18  		self.Set("haspid", true)
    19  		self.Set("pid", pid)
    20  	}
    21  
    22  	if tid := self.Param("tid").MustInt64(); tid > 0 {
    23  		if tp, err := models.GetTopic(tid); tp != nil && err == nil {
    24  			self.Set("topic", *tp)
    25  		} else {
    26  			self.Flash.Error(err.Error())
    27  			return err
    28  		}
    29  	}
    30  
    31  	if nds, err := models.GetNodes(0, 0, "id"); nds != nil && err == nil {
    32  		self.Set("nodes", *nds)
    33  	}
    34  
    35  	TplNames := "root/create_topic"
    36  	return self.Render(TplNames)
    37  
    38  }
    39  
    40  func PostRCreateTopicHandler(self *makross.Context) error {
    41  
    42  	_usr_, okay := self.Session.Get("SignedUser").(*models.User)
    43  	if !okay {
    44  		return self.NoContent(401)
    45  	}
    46  
    47  	policy := helper.ObjPolicy()
    48  	content := policy.Sanitize(self.FormValue("content"))
    49  
    50  	title := self.FormValue("title")
    51  	//images := self.FormValue("images")
    52  
    53  	cid := self.Args("cid").MustInt64()
    54  	nid := self.Args("nodeid").MustInt64()
    55  
    56  	pid := self.Args("pid").MustInt64()
    57  	if pid == 0 {
    58  		pid = self.Param("pid").MustInt64()
    59  	}
    60  
    61  	if (len(content) > 0) && (_usr_.Id > 0) {
    62  		if tid, err := models.AddTopic(title, content, _usr_.Avatar, _usr_.AvatarLarge, _usr_.AvatarMedium, _usr_.AvatarSmall, pid, cid, nid, _usr_.Id); err != nil || tid <= 0 {
    63  			self.Flash.Error(fmt.Sprint("增加话题出现错误:", err))
    64  			return self.Redirect("/root/create/topic/")
    65  
    66  		} else {
    67  			if pid == 0 {
    68  				pid = tid
    69  			}
    70  
    71  			self.Flash.Success("新增话题成功!")
    72  			return self.Redirect("/root/create/" + strconv.FormatInt(pid, 10) + "/topic/")
    73  			//return self.Redirect( "/root/read/topic/"+strconv.FormatInt(tid, 10)+"/")
    74  
    75  		}
    76  
    77  	} else {
    78  		return self.Redirect("/root/read/topic/")
    79  	}
    80  
    81  }