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) }
func TestArticlesGyCategory(t *testing.T) { pagination := new(db.Pagination) pagination.PerPage = 10 pagination.Page = 1 services.ArticlesGyCategory(1, "bootstrap", pagination, false) for _, v := range pagination.Data { fmt.Println("id:", v.(entities.Article).Id, "title:", v.(entities.Article).Title) } }
func AllArticles(userId int64, pagination *db.Pagination) { db := db.NewDB() var articles []entities.Article query := db.From("article") if userId > 0 { query.Where("user_id", userId) } query.OrderBy("created_at desc").FillPagination(&articles, pagination) articles = setUser(articles) pagination.SetData(articles) }
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) } }