From 2562f4c2c8c7a02a3fc43191753d343d77698adb Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 30 May 2017 23:28:42 +0000 Subject: [PATCH] Fix metadata editor Convert slices to postgres arrays. --- lib/admin.go | 15 ++++++++------- lib/database/books.go | 8 +++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/admin.go b/lib/admin.go index a21b4e3..6d93d58 100644 --- a/lib/admin.go +++ b/lib/admin.go @@ -87,6 +87,7 @@ func cleanEmptyStr(s []string) []string { } func saveHandler(h handler) { + // XXX: check for errors (ISBN, length(lang), ...) id := mux.Vars(h.r)["id"] if !h.sess.IsModerator() { notFound(h) @@ -97,26 +98,26 @@ func saveHandler(h handler) { publisher := h.r.FormValue("publisher") date := h.r.FormValue("date") description := h.r.FormValue("description") - author := cleanEmptyStr(h.r.Form["author"]) + authors := cleanEmptyStr(h.r.Form["author"]) tags := cleanEmptyStr(strings.Split(h.r.FormValue("tags"), ",")) - isbn := parser.ISBN(h.r.FormValue("isbn")) // XXX: check for errors - lang := cleanEmptyStr(h.r.Form["lang"]) + isbn := parser.ISBN(h.r.FormValue("isbn")) + lang := h.r.FormValue("lang") book := map[string]interface{}{"title": title, "publisher": publisher, "date": date, "description": description, - "author": author, + "authors": authors, "tags": tags, "isbn": isbn, "lang": lang} err := h.db.UpdateBook(id, book) if err != nil { log.Error("Updating book: ", err) - notFound(h) - return + h.sess.Notify("Can't modify book!", err.Error(), "error") + } else { + h.sess.Notify("Book Modified!", "", "success") } - h.sess.Notify("Book Modified!", "", "success") h.sess.Save(h.w, h.r) if h.db.IsBookActive(id) { http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound) diff --git a/lib/database/books.go b/lib/database/books.go index b64b5d9..3a10c18 100644 --- a/lib/database/books.go +++ b/lib/database/books.go @@ -4,6 +4,8 @@ import ( "strings" "time" + "github.com/go-pg/pg" + log "github.com/cihub/seelog" ) @@ -132,7 +134,11 @@ func (db *pgDB) UpdateBook(id string, data map[string]interface{}) error { setCondition += ", " } setCondition += col + " = ?" - params = append(params, val) + if col == "authors" || col == "tags" { + params = append(params, pg.Array(val)) + } else { + params = append(params, val) + } } _, err := db.sql.Model(&Book{}). Set(setCondition, params...).