From 74490e762221817b48d900b90cd343db4b1000fd Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:51:55 -0700 Subject: changing db call to use more efficient query func --- system/admin/handlers.go | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 742b898..ad5098e 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "net/http" + "strconv" "strings" "time" @@ -537,9 +538,6 @@ 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{} - if _, ok := content.Types[t]; !ok { res.WriteHeader(http.StatusBadRequest) errView, err := Error405() @@ -571,6 +569,47 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { hasExt = true } + count, err := strconv.Atoi(q.Get("count")) // int: determines number of posts to return (10 default, -1 is all) + if err != nil { + if q.Get("count") == "" { + count = 10 + } else { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + } + + offset, err := strconv.Atoi(q.Get("offset")) // int: multiplier of count for pagination (0 default) + if err != nil { + if q.Get("offset") == "" { + offset = 0 + } else { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + } + + opts := db.QueryOptions{ + Count: count, + Offset: offset, + Order: order, + } + + posts := db.Query(t+"_sorted", opts) + b := &bytes.Buffer{} + html := `
@@ -636,7 +675,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { case "pending": // get _pending posts of type t from the db - posts = db.ContentAll(t + "_pending") + posts = db.Query(t+"_pending", opts) html += `
Status: -- cgit v1.2.3 From 7ec1187b1987861831a88737ffb8512e8568ad8c Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:58:41 -0700 Subject: swapping order of post winding --- system/admin/handlers.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index ad5098e..5540767 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -691,8 +691,8 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { switch order { case "desc", "": if hasExt { - // reverse the order of posts slice - for i := len(posts) - 1; i >= 0; i-- { + // keep natural order of posts slice, as returned from unsorted bucket + for i := range posts { err := json.Unmarshal(posts[i], &p) if err != nil { log.Println("Error unmarshal json into", t, err, posts[i]) @@ -705,9 +705,10 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { post := adminPostListItem(p, t, status) b.Write(post) } + } else { - // keep natural order of posts slice, as returned from sorted bucket - for i := range posts { + // reverse the order of posts slice + for i := len(posts) - 1; i >= 0; i-- { err := json.Unmarshal(posts[i], &p) if err != nil { log.Println("Error unmarshal json into", t, err, posts[i]) @@ -724,8 +725,8 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { case "asc": if hasExt { - // keep natural order of posts slice, as returned from sorted bucket - for i := range posts { + // reverse the order of posts slice + for i := len(posts) - 1; i >= 0; i-- { err := json.Unmarshal(posts[i], &p) if err != nil { log.Println("Error unmarshal json into", t, err, posts[i]) @@ -739,8 +740,8 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { b.Write(post) } } else { - // reverse the order of posts slice - for i := len(posts) - 1; i >= 0; i-- { + // keep natural order of posts slice, as returned from sorted bucket + for i := range posts { err := json.Unmarshal(posts[i], &p) if err != nil { log.Println("Error unmarshal json into", t, err, posts[i]) -- cgit v1.2.3 From 022b3455d5c7a67a2a8fb9d168a54cb95b063df3 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 01:05:55 -0700 Subject: reworking the order logic for admin post sorting --- system/admin/handlers.go | 81 +++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 59 deletions(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 5540767..6c4cd83 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -688,72 +688,35 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { } html += `
    ` - switch order { - case "desc", "": - if hasExt { - // keep natural order of posts slice, as returned from unsorted bucket - for i := range posts { - err := json.Unmarshal(posts[i], &p) - if err != nil { - log.Println("Error unmarshal json into", t, err, posts[i]) - - post := `
  • Error decoding data. Possible file corruption.
  • ` - b.Write([]byte(post)) - continue - } + if hasExt { + // reverse the order of posts slice + for i := len(posts) - 1; i >= 0; i-- { + err := json.Unmarshal(posts[i], &p) + if err != nil { + log.Println("Error unmarshal json into", t, err, posts[i]) - post := adminPostListItem(p, t, status) - b.Write(post) + post := `
  • Error decoding data. Possible file corruption.
  • ` + b.Write([]byte(post)) + continue } - } else { - // reverse the order of posts slice - for i := len(posts) - 1; i >= 0; i-- { - err := json.Unmarshal(posts[i], &p) - if err != nil { - log.Println("Error unmarshal json into", t, err, posts[i]) - - post := `
  • Error decoding data. Possible file corruption.
  • ` - b.Write([]byte(post)) - continue - } - - post := adminPostListItem(p, t, status) - b.Write(post) - } + post := adminPostListItem(p, t, status) + b.Write(post) } + } else { + // keep natural order of posts slice, as returned from sorted bucket + for i := range posts { + err := json.Unmarshal(posts[i], &p) + if err != nil { + log.Println("Error unmarshal json into", t, err, posts[i]) - case "asc": - if hasExt { - // reverse the order of posts slice - for i := len(posts) - 1; i >= 0; i-- { - err := json.Unmarshal(posts[i], &p) - if err != nil { - log.Println("Error unmarshal json into", t, err, posts[i]) - - post := `
  • Error decoding data. Possible file corruption.
  • ` - b.Write([]byte(post)) - continue - } - - post := adminPostListItem(p, t, status) - b.Write(post) + post := `
  • Error decoding data. Possible file corruption.
  • ` + b.Write([]byte(post)) + continue } - } else { - // keep natural order of posts slice, as returned from sorted bucket - for i := range posts { - err := json.Unmarshal(posts[i], &p) - if err != nil { - log.Println("Error unmarshal json into", t, err, posts[i]) - post := `
  • Error decoding data. Possible file corruption.
  • ` - b.Write([]byte(post)) - continue - } - - post := adminPostListItem(p, t, status) - b.Write(post) - } + post := adminPostListItem(p, t, status) + b.Write(post) } } -- cgit v1.2.3 From 330c35078d1b12657a373473d1638612f6cbbb8e Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 01:20:19 -0700 Subject: adding proper padding on month int to dir name for file upload --- system/admin/upload/upload.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/admin') diff --git a/system/admin/upload/upload.go b/system/admin/upload/upload.go index 169bffe..dc6d8cc 100644 --- a/system/admin/upload/upload.go +++ b/system/admin/upload/upload.go @@ -45,7 +45,7 @@ func StoreFiles(req *http.Request) (map[string]string, error) { urlPathPrefix := "api" uploadDirName := "uploads" - uploadDir := filepath.Join(pwd, uploadDirName, fmt.Sprintf("%d", tm.Year()), fmt.Sprintf("%d", tm.Month())) + uploadDir := filepath.Join(pwd, uploadDirName, fmt.Sprintf("%d", tm.Year()), fmt.Sprintf("%02d", tm.Month())) err = os.MkdirAll(uploadDir, os.ModeDir|os.ModePerm) // loop over all files and save them to disk -- cgit v1.2.3 From cb5e82f71478a4fead100d487ff53b1d3c7b63c0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 01:21:47 -0700 Subject: adding proper padding on month int to dir name for file upload --- system/admin/upload/upload.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/admin') diff --git a/system/admin/upload/upload.go b/system/admin/upload/upload.go index dc6d8cc..323f371 100644 --- a/system/admin/upload/upload.go +++ b/system/admin/upload/upload.go @@ -81,7 +81,7 @@ func StoreFiles(req *http.Request) (map[string]string, error) { } // add name:urlPath to req.PostForm to be inserted into db - urlPath := fmt.Sprintf("/%s/%s/%d/%d/%s", urlPathPrefix, uploadDirName, tm.Year(), tm.Month(), filename) + urlPath := fmt.Sprintf("/%s/%s/%d/%02d/%s", urlPathPrefix, uploadDirName, tm.Year(), tm.Month(), filename) urlPaths[name] = urlPath } -- cgit v1.2.3 From 9a88eb23e74c8b853dc8cfbb40c02c9c701b52de Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 01:27:48 -0700 Subject: adding post unmarshal into cases based on status --- system/admin/handlers.go | 54 ++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 6c4cd83..f1e17e8 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -673,6 +673,20 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { Pending
` + for i := range posts { + err := json.Unmarshal(posts[i], &p) + if err != nil { + log.Println("Error unmarshal json into", t, err, posts[i]) + + post := `
  • Error decoding data. Possible file corruption.
  • ` + b.Write([]byte(post)) + continue + } + + post := adminPostListItem(p, t, status) + b.Write(post) + } + case "pending": // get _pending posts of type t from the db posts = db.Query(t+"_pending", opts) @@ -683,42 +697,24 @@ func postsHandler(res http.ResponseWriter, req *http.Request) {  |  Pending
    ` - } - } - html += `
      ` + for i := len(posts); i >= 0; i-- { + err := json.Unmarshal(posts[i], &p) + if err != nil { + log.Println("Error unmarshal json into", t, err, posts[i]) - if hasExt { - // reverse the order of posts slice - for i := len(posts) - 1; i >= 0; i-- { - err := json.Unmarshal(posts[i], &p) - if err != nil { - log.Println("Error unmarshal json into", t, err, posts[i]) + post := `
    • Error decoding data. Possible file corruption.
    • ` + b.Write([]byte(post)) + continue + } - post := `
    • Error decoding data. Possible file corruption.
    • ` - b.Write([]byte(post)) - continue + post := adminPostListItem(p, t, status) + b.Write(post) } - - post := adminPostListItem(p, t, status) - b.Write(post) } - } else { - // keep natural order of posts slice, as returned from sorted bucket - for i := range posts { - err := json.Unmarshal(posts[i], &p) - if err != nil { - log.Println("Error unmarshal json into", t, err, posts[i]) - - post := `
    • Error decoding data. Possible file corruption.
    • ` - b.Write([]byte(post)) - continue - } - post := adminPostListItem(p, t, status) - b.Write(post) - } } + html += `
        ` b.Write([]byte(`
    `)) -- cgit v1.2.3 From 0bc6673fd1747eef84124cf7137bfa32c9cb5e6d Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 01:30:17 -0700 Subject: adding post unmarshal into cases based on status --- system/admin/handlers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index f1e17e8..630796a 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -698,7 +698,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { Pending ` - for i := len(posts); i >= 0; i-- { + for i := len(posts) - 1; i >= 0; i-- { err := json.Unmarshal(posts[i], &p) if err != nil { log.Println("Error unmarshal json into", t, err, posts[i]) -- cgit v1.2.3 From 38182ae48f05d0913ada452730a4e564423592a0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 01:32:25 -0700 Subject: adding default to order param in postsHandler --- system/admin/handlers.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 630796a..e2309aa 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -536,6 +536,10 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { } order := strings.ToLower(q.Get("order")) + if order != "asc" { + order = "desc" + } + status := q.Get("status") if _, ok := content.Types[t]; !ok { -- cgit v1.2.3 From 2bc4e3714ed16977135a2329e881f860930f2513 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 01:38:44 -0700 Subject: fixing default status --- system/admin/handlers.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index e2309aa..e3d99cc 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -768,9 +768,10 @@ func adminPostListItem(p editor.Editable, t, status string) []byte { cid := fmt.Sprintf("%d", p.ContentID()) - if status == "public" { + switch status { + case "public", "": status = "" - } else { + default: status = "_" + status } -- cgit v1.2.3 From b782dc0a2dad9b4bf0de267d516c1291bbd8da1c Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 02:03:12 -0700 Subject: adding fix for redirect after save on pending post content --- system/admin/handlers.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index e3d99cc..68d05c4 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -1050,8 +1050,16 @@ func editHandler(res http.ResponseWriter, req *http.Request) { host := req.URL.Host path := req.URL.Path sid := fmt.Sprintf("%d", id) - desURL := scheme + host + path + "?type=" + t + "&id=" + sid - http.Redirect(res, req, desURL, http.StatusFound) + if strings.Contains(t, "_") { + t = strings.Split(t, "_")[0] + } + redir := scheme + host + path + "?type=" + t + "&id=" + sid + + if req.URL.Query().Get("status") == "pending" { + redir += redir + "&status=pending" + } + + http.Redirect(res, req, redir, http.StatusFound) default: res.WriteHeader(http.StatusMethodNotAllowed) -- cgit v1.2.3 From 759506ca799c443402fed9c6413a8b49406e197f Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 02:20:11 -0700 Subject: string addition mistake --- system/admin/handlers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/admin') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 68d05c4..12750e2 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -1056,7 +1056,7 @@ func editHandler(res http.ResponseWriter, req *http.Request) { redir := scheme + host + path + "?type=" + t + "&id=" + sid if req.URL.Query().Get("status") == "pending" { - redir += redir + "&status=pending" + redir += "&status=pending" } http.Redirect(res, req, redir, http.StatusFound) -- cgit v1.2.3