当前位置: 首页>>代码示例>>Golang>>正文


Golang Post.IsBest方法代码示例

本文整理汇总了Golang中github.com/go-tango/wego/models.Post.IsBest方法的典型用法代码示例。如果您正苦于以下问题:Golang Post.IsBest方法的具体用法?Golang Post.IsBest怎么用?Golang Post.IsBest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/go-tango/wego/models.Post的用法示例。


在下文中一共展示了Post.IsBest方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Post

func (this *Post) Post() {
	if this.CheckActiveRedirect() {
		return
	}

	if !this.IsAjax() {
		return
	}

	result := map[string]interface{}{
		"success": false,
	}
	action := this.GetString("action")
	switch action {
	case "toggle-best":
		if this.User.IsAdmin {
			if postId, err := this.GetInt("post"); err == nil {
				//set post best
				var post models.Post
				if err := models.GetById(postId, &post); err == nil {
					post.IsBest = !post.IsBest
					if models.UpdateById(post.Id, post, "is_best") == nil {
						result["success"] = true
					}
				}
			} else {
				this.Logger.Error("post value is not int:", this.GetString("post"))
			}
		}
	case "toggle-fav":
		if postId, err := this.GetInt("post"); err == nil {
			var post models.Post
			if err := models.GetById(postId, &post); err == nil {
				var favoritePost = models.FavoritePost{
					PostId: post.Id,
					UserId: this.User.Id,
				}

				if err := models.GetByExample(&favoritePost); err == nil {
					//toogle IsFav
					favoritePost.IsFav = !favoritePost.IsFav
					if models.UpdateById(favoritePost.Id, favoritePost, "is_fav") == nil {
						//update user fav post count
						if favoritePost.IsFav {
							this.User.FavPosts += 1
						} else {
							this.User.FavPosts -= 1

						}
						if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil {
							result["success"] = true
						}
					}
				} else if err == models.ErrNotExist {
					favoritePost = models.FavoritePost{
						UserId: this.User.Id,
						PostId: post.Id,
						IsFav:  true,
					}
					if models.Insert(favoritePost) == nil {
						//update user fav post count
						this.User.FavPosts += 1
						if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil {
							result["success"] = true
						}
					}
				} else {
					this.Logger.Error("Get favorite post err:", err)
				}
			}
		}
	}
	this.Data["json"] = result
	this.ServeJson(this.Data)
}
开发者ID:trigrass2,项目名称:wego,代码行数:75,代码来源:post.go


注:本文中的github.com/go-tango/wego/models.Post.IsBest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。