Ejemplo n.º 1
0
func SetLikeSign(pagination *db.Pagination, userId int64) {
	//设置当前用户点赞标记
	if len(pagination.Data) < 1 || userId <= 0 {
		return
	}

	ids, err := utils.ExtractFieldValues(pagination.Data, "Id")
	if nil != err {
		return
	}

	signs := ArticleLikeLogs(userId, ids)
	signMap := make(map[int64]bool)
	for _, v := range signs {
		signMap[v] = true
	}

	var articles []entities.Article
	for _, v := range pagination.Data {
		article := v.(entities.Article)
		article.HasLike = signMap[article.Id]
		articles = append(articles, article)
	}
	pagination.SetData(articles)
}
Ejemplo n.º 2
0
func ArticlesGyCategory(userId int64, category string, pagination *db.Pagination, isFilterUserId bool) {
	db := db.NewDB()
	var articles []entities.Article
	query := db.From("article")
	if userId > 0 && isFilterUserId {
		query.Where("user_id", userId)
	}

	if len(category) > 0 {
		category = strings.ToLower(category)
		beego.Error("category", category)
		query.Like("categories", "%"+category+"%")
	}

	query.OrderBy("created_at desc").FillPagination(&articles, pagination)

	//设置当前用户点赞标记
	if len(pagination.Data) < 1 || userId <= 0 {
		return
	}

	ids, err := utils.ExtractFieldValues(pagination.Data, "Id")
	if nil != err {
		return
	}
	if userId > 0 {
		signs := ArticleLikeLogs(userId, ids)
		signMap := make(map[int64]bool)
		for _, v := range signs {
			signMap[v] = true
		}
		var newArticles []entities.Article
		for _, v := range articles {
			v.HasLike = signMap[v.Id]
			newArticles = append(newArticles, v)
		}

		pagination.SetData(newArticles)

		newArticles = setUser(newArticles)

		pagination.SetData(newArticles)
	}

}
Ejemplo n.º 3
0
func setUser(articles []entities.Article) []entities.Article {
	userIds, err := utils.ExtractFieldValues(articles, "UserId")
	if nil != err {
		return articles
	}

	userMap := UserMap(userIds)
	var newArticles []entities.Article
	for _, article := range articles {
		userId := article.UserId
		user := userMap[userId]
		article.User = user
		newArticles = append(newArticles, article)
	}

	return newArticles

}
Ejemplo n.º 4
0
func TestExtractFieldValues(t *testing.T) {

	var users []entities.User

	users = append(users, entities.User{Id: 1, Name: "第1个"})
	users = append(users, entities.User{Id: 2, Name: "第2个"})
	users = append(users, entities.User{Id: 3, Name: "第3个"})
	users = append(users, entities.User{Id: 4, Name: "第4个"})
	users = append(users, entities.User{Id: 5, Name: "第5个"})
	users = append(users, entities.User{Id: 6, Name: "第6个"})

	ids, err := utils.ExtractFieldValues(users, "Id")
	if nil != err {
		fmt.Println("err:", err)
	}
	for _, id := range ids {
		fmt.Println("id:", id)
	}

	str := utils.SliceToString(ids, ",")

	fmt.Println("str:", str)

}