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

     1  package routers
     2  
     3  import (
     4  	"github.com/insionng/makross"
     5  
     6  	"time"
     7  	"github.com/insionng/yougam/helper"
     8  	"github.com/insionng/yougam/models"
     9  )
    10  
    11  func GetFavoriteHandler(self *makross.Context) error {
    12  
    13  	_usr_, okay := self.Session.Get("SignedUser").(*models.User)
    14  	if !okay {
    15  		return self.NoContent(401)
    16  	}
    17  	if helper.IsSpider(self.UserAgent()) != true {
    18  		name := self.Param("name").String()
    19  		id := self.Param("id").MustInt64()
    20  		uid := _usr_.Id
    21  		/*
    22  			if name == "question" {
    23  				if models.IsQuestionMark(uid, id) {
    24  					self.Ctx.Output.SetStatus(304)
    25  					return
    26  
    27  				} else {
    28  					if qs, err := models.GetQuestion(id); err == nil {
    29  
    30  						qs.Hotup = qs.Hotup + 1
    31  						qs.Hotscore = helper.Qhot_QScore(qs.Hotup, qs.Hotdown)
    32  						qs.Hotvote = helper.Qhot_Vote(qs.Hotup, qs.Hotdown)
    33  						qs.Hotness = helper.Qhot(qs.Views, qs.ReplyCount, qs.Hotscore, models.GetAScoresByPid(id), qs.Created, qs.ReplyTime)
    34  
    35  						if _, err := models.PutQuestion(id, qs); err != nil {
    36  							fmt.Println("PutQuestion执行错误:", err)
    37  						} else {
    38  							models.SetQuestionMark(uid, id)
    39  						}
    40  						//♥ 有用 ({{.article.Hotup}})
    41  						self.Ctx.WriteString(strconv.Itoa(int(qs.Hotscore)))
    42  					} else {
    43  						return
    44  					}
    45  				}
    46  			} else*/
    47  		if name == "topic" {
    48  			itm := models.IsTopicMark(uid, id)
    49  
    50  			switch {
    51  			case itm == true: //已经被收藏 则以下取消收藏
    52  				{
    53  
    54  					if tp, err := models.GetTopic(id); err == nil {
    55  
    56  						tp.Hotdown = tp.Hotdown + 1
    57  						tp.Hotscore = helper.Score(tp.Hotup, tp.Hotdown)
    58  						tp.Hotvote = helper.QhotVote(tp.Hotup, tp.Hotdown)
    59  						tp.Hotness = helper.Hotness(tp.Hotup, tp.Hotdown, tp.Created)
    60  
    61  						models.DelTopicMark(uid, id)
    62  
    63  						//统计话题被收藏数
    64  						tp.FavoriteCount, _ = models.TopicMarkCount(id)
    65  						models.PutTopic(id, tp)
    66  
    67  						if usr, e := models.GetUser(uid); e == nil && usr != nil {
    68  							tmc, _ := models.TopicMarkCountByUid(uid)
    69  
    70  							//用户自己收藏的话题总数
    71  							usr.FavoriteCount = tmc
    72  							models.PutUser(uid, usr)
    73  						}
    74  						//return false, strconv.FormatInt(tp.FavoriteCount, 10)
    75  						data := map[string]interface{}{}
    76  						data["isFavorite"] = false
    77  						data["FavoriteCount"] = tp.FavoriteCount
    78  						return self.JSON(data)
    79  					}
    80  
    81  				}
    82  			case itm == false: //尚未被收藏 则以下进行收藏
    83  				{
    84  
    85  					if tp, err := models.GetTopic(id); err == nil {
    86  
    87  						tp.Hotup = tp.Hotup + 1
    88  						tp.Hotscore = helper.Score(tp.Hotup, tp.Hotdown)
    89  						tp.Hotvote = helper.QhotVote(tp.Hotup, tp.Hotdown)
    90  						tp.Hotness = helper.Hotness(tp.Hotup, tp.Hotdown, tp.Created)
    91  
    92  						models.SetTopicMark(uid, tp.Cid, id)
    93  
    94  						//统计话题被收藏数
    95  						tp.FavoriteCount, _ = models.TopicMarkCount(id)
    96  						models.PutTopic(id, tp)
    97  
    98  						if usr, e := models.GetUser(uid); e == nil && usr != nil {
    99  							tmc, _ := models.TopicMarkCountByUid(uid)
   100  
   101  							//用户自己收藏的话题总数
   102  							usr.FavoriteCount = tmc
   103  							models.PutUser(uid, usr)
   104  						}
   105  						//return true, strconv.FormatInt(tp.FavoriteCount, 10)
   106  						data := map[string]interface{}{}
   107  						data["isFavorite"] = true
   108  						data["FavoriteCount"] = tp.FavoriteCount
   109  						return self.JSON(data)
   110  					}
   111  
   112  				}
   113  
   114  			}
   115  		} else if name == "node" {
   116  
   117  			if nd, err := models.GetNode(id); err == nil {
   118  
   119  				nd.Hotup = nd.Hotup + 1
   120  				nd.Hotscore = helper.Score(nd.Hotup, nd.Hotdown)
   121  				nd.Hotness = helper.Hotness(nd.Hotup, nd.Hotdown, time.Now().Unix())
   122  				models.PutNode(id, nd)
   123  
   124  				data := map[string]interface{}{}
   125  				data["isFavorite"] = true
   126  				data["FavoriteCount"] = nd.FavoriteCount
   127  				return self.JSON(data)
   128  			} else {
   129  
   130  				data := map[string]interface{}{}
   131  				data["isFavorite"] = false
   132  				data["FavoriteCount"] = nd.FavoriteCount
   133  				return self.JSON(data)
   134  			}
   135  		} else {
   136  			return self.NoContent(304)
   137  		}
   138  
   139  	}
   140  
   141  	return self.NoContent(401)
   142  
   143  }