From b0c1df4e544137fcbe1641641660a8c5f49d63f0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 18 Oct 2016 18:38:31 -0700 Subject: enriching admin view with more post info per post item --- system/admin/handlers.go | 58 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 10 deletions(-) (limited to 'system') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 2bce74c..07cfb6d 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "log" "net/http" "strings" "time" @@ -368,20 +369,33 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { if order == "desc" || order == "" { // keep natural order of posts slice, as returned from sorted bucket for i := range posts { - json.Unmarshal(posts[i], &p) - post := `
  • ` + p.ContentName() + `
  • ` - b.Write([]byte(post)) + 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) + b.Write(post) } + } else if order == "asc" { // reverse the order of posts slice for i := len(posts) - 1; i >= 0; i-- { - json.Unmarshal(posts[i], &p) - post := `
  • ` + p.ContentName() + `
  • ` - b.Write([]byte(post)) + 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) + b.Write(post) } } @@ -401,6 +415,30 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { res.Write(adminView) } +// adminPostListItem is a helper to create the li containing a post. +// p is the asserted post as an Editable, t is the Type of the post. +func adminPostListItem(p editor.Editable, t string) []byte { + s, ok := p.(editor.Sortable) + if !ok { + log.Println("Content type", t, "doesn't implement editor.Sortable") + post := `
  • Error retreiving data. Your data type doesn't implement necessary interfaces.
  • ` + return []byte(post) + } + + // use sort to get other info to display in admin UI post list + tsTime := time.Unix(int64(s.Time()/1000), 0) + upTime := time.Unix(int64(s.Touch()/1000), 0) + updatedTime := upTime.Format("Jan 2, 2006 15:04 PM") + publishTime := tsTime.Format("1/2/06") + + post := ` +
  • + ` + p.ContentName() + ` + Updated: ` + updatedTime + ` + Updated: ` + publishTime + ` +
  • ` +} + func editHandler(res http.ResponseWriter, req *http.Request) { switch req.Method { case http.MethodGet: -- cgit v1.2.3