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

     1  package root
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/insionng/makross"
     7  	
     8  	"strconv"
     9  	"time"
    10  	"github.com/insionng/yougam/models"
    11  )
    12  
    13  func GetRDeleteNodeHandler(self *makross.Context) error {
    14  
    15  	
    16  	_usr_, okay := self.Session.Get("SignedUser").(*models.User)
    17  	if !okay {
    18  		return self.NoContent(401)
    19  	}
    20  
    21  	if nid := self.Param("nid").MustInt64(); nid > 0 {
    22  		if nd, e := models.GetNode(nid); e == nil {
    23  			if e := models.DelNode(nid, _usr_.Id, _usr_.Role); e != nil {
    24  				self.Flash.Error("删除 Node id:" + strconv.Itoa(int(nid)) + "出现错误 " + fmt.Sprintf("%s", e) + "!")
    25  				return e
    26  			} else {
    27  				//删除节点成功后 就去统计有多少个同样分类id的节点,把统计出来的数目写入该分类的nodecount项
    28  				if nd.Cid > 0 {
    29  					if nc, e := models.GetNodesByCid(nd.Cid, 0, 0, "id"); e == nil {
    30  						if catz, e := models.GetCategory(nd.Cid); e == nil {
    31  							catz.NodeCount = int64(len(*nc))
    32  							models.PutCategory(nd.Cid, catz)
    33  						}
    34  					}
    35  				}
    36  
    37  				self.Flash.Success("删除 Node id:" + strconv.Itoa(int(nid)) + "成功!")
    38  				return self.Redirect("/root/read/node/")
    39  
    40  			}
    41  		} else {
    42  			es := "Node id不存在!"
    43  			self.Flash.Error(es)
    44  			return errors.New(es)
    45  		}
    46  
    47  	}
    48  
    49  	return self.Redirect("/root/dashboard/?version=" + strconv.FormatInt(time.Now().Unix(), 10))
    50  
    51  }