summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-10-30 21:42:51 -0700
committerSteve Manuel <nilslice@gmail.com>2016-10-30 21:42:51 -0700
commit3ba9236ae25cdf4a2b80b7a1f860edbf2e749e39 (patch)
tree15b5ed73dc200664a0011879e042cebb0f141c03
parent83e0095de7091816080a9b7e70ef09c84cdbbb42 (diff)
slight refactor of pending content code & reimplementing where needed
-rw-r--r--system/api/external.go3
-rw-r--r--system/db/content.go32
2 files changed, 20 insertions, 15 deletions
diff --git a/system/api/external.go b/system/api/external.go
index 52240d5..6489c68 100644
--- a/system/api/external.go
+++ b/system/api/external.go
@@ -66,7 +66,6 @@ func externalPostsHandler(res http.ResponseWriter, req *http.Request) {
ts := fmt.Sprintf("%d", time.Now().Unix()*1000)
req.PostForm.Set("timestamp", ts)
req.PostForm.Set("updated", ts)
- req.PostForm.Set("id", ts)
urlPaths, err := upload.StoreFiles(req)
if err != nil {
@@ -100,7 +99,7 @@ func externalPostsHandler(res http.ResponseWriter, req *http.Request) {
req.PostForm.Del(discardKey)
}
- err = db.SetPendingContent(t+"_pending", req.PostForm)
+ _, err = db.SetPendingContent(t, req.PostForm)
if err != nil {
log.Println("[External] error:", err)
res.WriteHeader(http.StatusInternalServerError)
diff --git a/system/db/content.go b/system/db/content.go
index f3397ba..2f6fb90 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -3,14 +3,12 @@ package db
import (
"bytes"
"encoding/json"
- "errors"
"fmt"
"log"
"net/url"
"sort"
"strconv"
"strings"
- "time"
"github.com/bosssauce/ponzu/content"
"github.com/bosssauce/ponzu/management/editor"
@@ -116,33 +114,41 @@ func insert(ns string, data url.Values) (int, error) {
}
// SetPendingContent inserts submitted content for pending approval
-func SetPendingContent(target string, data url.Values) error {
- if !strings.Contains(target, "_pending") {
- return errors.New("Only set items into _pending bucket using SetPendingContent. Namespace should be <Type>_pending")
- }
-
- ns := strings.Split(target, "_")[0]
+func SetPendingContent(ns string, data url.Values) (int, error) {
+ var effectedID int
err := store.Update(func(tx *bolt.Tx) error {
- b, err := tx.CreateBucketIfNotExists([]byte(target))
+ b, err := tx.CreateBucketIfNotExists([]byte(ns + "_pending"))
+ if err != nil {
+ return err
+ }
+
+ // get the next available ID and convert to string
+ // also set effectedID to int of ID
+ id, err := b.NextSequence()
if err != nil {
return err
}
+ cid := strconv.FormatUint(id, 10)
+ effectedID, err = strconv.Atoi(cid)
+ if err != nil {
+ return err
+ }
+ data.Set("id", cid)
- key := fmt.Sprintf("%d", time.Now().UTC().Unix())
j, err := postToJSON(ns, data)
if err != nil {
return err
}
- b.Put([]byte(key), j)
+ b.Put([]byte(cid), j)
return nil
})
if err != nil {
- return err
+ return 0, err
}
- return nil
+ return effectedID, nil
}
// DeleteContent removes an item from the database. Deleting a non-existent item