summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-10-30 22:35:47 -0700
committerSteve Manuel <nilslice@gmail.com>2016-10-30 22:35:47 -0700
commitdd0fd60357e8a8dcaf3732947a35e5072eeefaf7 (patch)
tree56fcb8f5cfb9d3bef8c437b434bb4d0a68a24633
parent07f7c974724d1ab1f86131d2a75cff3f52ace5c4 (diff)
refactor some db code and update how status vars interpolate throughour UI code
-rw-r--r--system/admin/handlers.go18
-rw-r--r--system/api/external.go2
-rw-r--r--system/db/content.go61
3 files changed, 33 insertions, 48 deletions
diff --git a/system/admin/handlers.go b/system/admin/handlers.go
index f7460a3..dbeefd2 100644
--- a/system/admin/handlers.go
+++ b/system/admin/handlers.go
@@ -534,6 +534,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) {
}
order := strings.ToLower(q.Get("order"))
+ status := q.Get("status")
posts := db.ContentAll(t + "_sorted")
b := &bytes.Buffer{}
@@ -628,7 +629,6 @@ func postsHandler(res http.ResponseWriter, req *http.Request) {
</form>
</div>`
if hasExt {
- status := q.Get("status")
if status == "" {
q.Add("status", "public")
}
@@ -677,7 +677,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) {
continue
}
- post := adminPostListItem(p, t, "pending")
+ post := adminPostListItem(p, t, status)
b.Write(post)
}
} else {
@@ -692,7 +692,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) {
continue
}
- post := adminPostListItem(p, t, "")
+ post := adminPostListItem(p, t, status)
b.Write(post)
}
}
@@ -710,7 +710,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) {
continue
}
- post := adminPostListItem(p, t, "pending")
+ post := adminPostListItem(p, t, status)
b.Write(post)
}
} else {
@@ -725,7 +725,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) {
continue
}
- post := adminPostListItem(p, t, "")
+ post := adminPostListItem(p, t, status)
b.Write(post)
}
}
@@ -779,6 +779,12 @@ func adminPostListItem(p editor.Editable, t, status string) []byte {
cid := fmt.Sprintf("%d", p.ContentID())
+ if status == "public" {
+ status = ""
+ } else {
+ status = "_" + status
+ }
+
post := `
<li class="col s12">
<a href="/admin/edit?type=` + t + `&status=` + status + `&id=` + cid + `">` + p.ContentName() + `</a>
@@ -788,7 +794,7 @@ func adminPostListItem(p editor.Editable, t, status string) []byte {
<form enctype="multipart/form-data" class="quick-delete-post __ponzu right" action="/admin/edit/delete" method="post">
<span>Delete</span>
<input type="hidden" name="id" value="` + cid + `" />
- <input type="hidden" name="type" value="` + t + `_` + status + `" />
+ <input type="hidden" name="type" value="` + t + status + `" />
</form>
</li>`
diff --git a/system/api/external.go b/system/api/external.go
index 6489c68..7426272 100644
--- a/system/api/external.go
+++ b/system/api/external.go
@@ -99,7 +99,7 @@ func externalPostsHandler(res http.ResponseWriter, req *http.Request) {
req.PostForm.Del(discardKey)
}
- _, err = db.SetPendingContent(t, req.PostForm)
+ _, err = db.SetContent(t+":-1", 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 2f6fb90..2c49e77 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -38,13 +38,20 @@ func SetContent(target string, data url.Values) (int, error) {
}
func update(ns, id string, data url.Values) (int, error) {
+ var specifier string // i.e. _pending, _sorted, etc.
+ if strings.Contains(ns, "_") {
+ spec := strings.Split(ns, "_")
+ ns = spec[0]
+ specifier = spec[1]
+ }
+
cid, err := strconv.Atoi(id)
if err != nil {
return 0, err
}
err = store.Update(func(tx *bolt.Tx) error {
- b, err := tx.CreateBucketIfNotExists([]byte(ns))
+ b, err := tx.CreateBucketIfNotExists([]byte(ns + specifier))
if err != nil {
return err
}
@@ -65,16 +72,24 @@ func update(ns, id string, data url.Values) (int, error) {
return 0, nil
}
- go SortContent(ns)
+ if specifier == "" {
+ go SortContent(ns)
+ }
return cid, nil
}
func insert(ns string, data url.Values) (int, error) {
var effectedID int
+ var specifier string // i.e. _pending, _sorted, etc.
+ if strings.Contains(ns, "_") {
+ spec := strings.Split(ns, "_")
+ ns = spec[0]
+ specifier = spec[1]
+ }
err := store.Update(func(tx *bolt.Tx) error {
- b, err := tx.CreateBucketIfNotExists([]byte(ns))
+ b, err := tx.CreateBucketIfNotExists([]byte(ns + specifier))
if err != nil {
return err
}
@@ -108,44 +123,8 @@ func insert(ns string, data url.Values) (int, error) {
return 0, err
}
- go SortContent(ns)
-
- return effectedID, nil
-}
-
-// SetPendingContent inserts submitted content for pending approval
-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(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)
-
- j, err := postToJSON(ns, data)
- if err != nil {
- return err
- }
- b.Put([]byte(cid), j)
-
- return nil
- })
- if err != nil {
- return 0, err
+ if specifier == "" {
+ go SortContent(ns)
}
return effectedID, nil