Optimized data loading, added stable sorting

pull/17/head
マリウス 3 years ago
parent 32b5cc9304
commit d22540a96c
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F

@ -2,6 +2,7 @@ package database
import ( import (
"context" "context"
"sort"
"sync" "sync"
orbitdb "berty.tech/go-orbit-db" orbitdb "berty.tech/go-orbit-db"
@ -212,10 +213,15 @@ func (db *Database) GetArticleByID(id string) (models.Article, error) {
func (db *Database) ListArticles() ([]models.Article, error) { func (db *Database) ListArticles() ([]models.Article, error) {
var articles []models.Article var articles []models.Article
entities, err := db.Store.Query(db.ctx, func(e interface{})(bool, error) { _, err := db.Store.Query(db.ctx, func(e interface{})(bool, error) {
entity := e.(map[string]interface{}) entity := e.(map[string]interface{})
if entity["type"] == "article" { if entity["type"] == "article" {
return true, nil var article models.Article
err := mapstructure.Decode(entity, &article)
if err == nil {
articles = append(articles, article)
}
return true, err
} }
return false, nil return false, nil
}) })
@ -223,14 +229,9 @@ func (db *Database) ListArticles() ([]models.Article, error) {
return articles, err return articles, err
} }
for _, entity := range entities { sort.SliceStable(articles, func(i, j int) bool {
var article models.Article return articles[i].Date > articles[j].Date
err = mapstructure.Decode(entity, &article) })
if err != nil {
return articles, err
}
articles = append(articles, article)
}
return articles, nil return articles, nil
} }

Loading…
Cancel
Save