Skip to content

Commit

Permalink
Merge pull request #102 from ponzu-cms/ponzu-dev
Browse files Browse the repository at this point in the history
separate UpdateContent and SetContent
  • Loading branch information
nilslice authored Mar 20, 2017
2 parents f6f6d6e + ac3a652 commit 483d5f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions system/api/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ func updateContentHandler(res http.ResponseWriter, req *http.Request) {
// set specifier for db bucket in case content is/isn't Trustable
var spec string

_, err = db.SetContent(t+spec+":"+id, req.PostForm)
_, err = db.UpdateContent(t+spec+":"+id, req.PostForm)
if err != nil {
log.Println("[Update] error calling SetContent:", err)
log.Println("[Update] error calling UpdateContent:", err)
res.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
31 changes: 23 additions & 8 deletions system/db/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package db
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
Expand All @@ -18,16 +17,18 @@ import (
uuid "github.com/satori/go.uuid"
)

// IsValidID checks that an ID from a DB target is valid.
// ID should be an integer greater than 0.
// ID of -1 is special for new posts, not updates.
// IDs start at 1 for auto-incrementing
func IsValidID(id string) bool {
// ID should be a non-negative integer.
// ID of -1 is special for new posts, not updates.
if i, err := strconv.Atoi(id); err != nil || i < 0 {
if i, err := strconv.Atoi(id); err != nil || i < 1 {
return false
}
return true
}

// SetContent inserts or updates values in the database.
// SetContent inserts/replaces values in the database.
// The `target` argument is a string made up of namespace:id (string:int)
func SetContent(target string, data url.Values) (int, error) {
t := strings.Split(target, ":")
Expand All @@ -43,6 +44,19 @@ func SetContent(target string, data url.Values) (int, error) {
return insert(ns, data)
}

return update(ns, id, data, nil)
}

// UpdateContent updates/merges values in the database.
// The `target` argument is a string made up of namespace:id (string:int)
func UpdateContent(target string, data url.Values) (int, error) {
t := strings.Split(target, ":")
ns, id := t[0], t[1]

if !IsValidID(id) {
return 0, fmt.Errorf("Invalid ID in target for UpdateContent: %s", target)
}

// retrieve existing content from the database
existingContent, err := Content(target)
if err != nil {
Expand All @@ -51,7 +65,9 @@ func SetContent(target string, data url.Values) (int, error) {
return update(ns, id, data, &existingContent)
}

// update can support merge or replace behavior
// update can support merge or replace behavior depending on existingContent.
// if existingContent is non-nil, we merge field values. empty/missing fields are ignored.
// if existingContent is nil, we replace field values. empty/missing fields are reset.
func update(ns, id string, data url.Values, existingContent *[]byte) (int, error) {
var specifier string // i.e. __pending, __sorted, etc.
if strings.Contains(ns, "__") {
Expand Down Expand Up @@ -112,8 +128,7 @@ func mergeData(ns string, data url.Values, existingContent []byte) ([]byte, erro
var j []byte
t, ok := item.Types[ns]
if !ok {
log.Println("Type not found from namespace:", ns)
return j, errors.New("Invalid type.")
return nil, fmt.Errorf("namespace type not found:", ns)
}

// Unmarsal the existing values
Expand Down

0 comments on commit 483d5f9

Please sign in to comment.