summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-10-18 11:22:19 -0700
committerSteve Manuel <nilslice@gmail.com>2016-10-18 11:22:19 -0700
commitc9dd1ac8c68e5e552f0620bab2bddb21732015de (patch)
tree37e08d7632696133bbb550b8d0e2b05ac5e0fe90
parent6746d1952b40c14d093717cb6c58fb7382150534 (diff)
implementing Sort on sortable posts, testing functionality
-rw-r--r--system/admin/handlers.go2
-rw-r--r--system/db/content.go49
2 files changed, 51 insertions, 0 deletions
diff --git a/system/admin/handlers.go b/system/admin/handlers.go
index edf351d..17a4843 100644
--- a/system/admin/handlers.go
+++ b/system/admin/handlers.go
@@ -498,6 +498,8 @@ func editHandler(res http.ResponseWriter, req *http.Request) {
return
}
+ go db.SortContent(t)
+
scheme := req.URL.Scheme
host := req.URL.Host
path := req.URL.Path
diff --git a/system/db/content.go b/system/db/content.go
index 1e5b95a..362f1a5 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -4,7 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
+ "log"
"net/url"
+ "sort"
"strconv"
"strings"
@@ -199,3 +201,50 @@ func ContentAll(namespace string) [][]byte {
return posts
}
+
+// SortContent sorts all content of the type supplied as the namespace by time,
+// in descending order, from most recent to least recent
+// Should be called from a goroutine after SetContent is successful
+func SortContent(namespace string) {
+ all := ContentAll(namespace)
+
+ var posts sortablePosts
+ post := content.Types[namespace]()
+ // decode each (json) into Editable
+ for i := range all {
+ j := all[i]
+ err := json.Unmarshal(j, &post)
+ if err != nil {
+ log.Println("Error decoding json while sorting", namespace, ":", err)
+ return
+ }
+
+ posts = append(posts, post.(editor.Sortable))
+ }
+
+ fmt.Println(posts)
+ fmt.Println("------------------------NOW SORTED------------------------")
+
+ // sort posts
+ sort.Sort(posts)
+
+ fmt.Println(posts)
+
+ // one by one, encode to json and store as
+ // store in __sorted bucket inside namespace bucket, first delete existing
+
+}
+
+type sortablePosts []editor.Sortable
+
+func (s sortablePosts) Len() int {
+ return len(s)
+}
+
+func (s sortablePosts) Less(i, j int) bool {
+ return s[i].Time() < s[j].Time()
+}
+
+func (s sortablePosts) Swap(i, j int) {
+ s[i], s[j] = s[j], s[i]
+}