Fix metadata editor

Convert slices to postgres arrays.
This commit is contained in:
Las Zenow 2017-05-30 23:28:42 +00:00
parent 71d975c2f7
commit 2562f4c2c8
2 changed files with 15 additions and 8 deletions

View file

@ -87,6 +87,7 @@ func cleanEmptyStr(s []string) []string {
} }
func saveHandler(h handler) { func saveHandler(h handler) {
// XXX: check for errors (ISBN, length(lang), ...)
id := mux.Vars(h.r)["id"] id := mux.Vars(h.r)["id"]
if !h.sess.IsModerator() { if !h.sess.IsModerator() {
notFound(h) notFound(h)
@ -97,26 +98,26 @@ func saveHandler(h handler) {
publisher := h.r.FormValue("publisher") publisher := h.r.FormValue("publisher")
date := h.r.FormValue("date") date := h.r.FormValue("date")
description := h.r.FormValue("description") 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"), ",")) tags := cleanEmptyStr(strings.Split(h.r.FormValue("tags"), ","))
isbn := parser.ISBN(h.r.FormValue("isbn")) // XXX: check for errors isbn := parser.ISBN(h.r.FormValue("isbn"))
lang := cleanEmptyStr(h.r.Form["lang"]) lang := h.r.FormValue("lang")
book := map[string]interface{}{"title": title, book := map[string]interface{}{"title": title,
"publisher": publisher, "publisher": publisher,
"date": date, "date": date,
"description": description, "description": description,
"author": author, "authors": authors,
"tags": tags, "tags": tags,
"isbn": isbn, "isbn": isbn,
"lang": lang} "lang": lang}
err := h.db.UpdateBook(id, book) err := h.db.UpdateBook(id, book)
if err != nil { if err != nil {
log.Error("Updating book: ", err) log.Error("Updating book: ", err)
notFound(h) h.sess.Notify("Can't modify book!", err.Error(), "error")
return } else {
h.sess.Notify("Book Modified!", "", "success")
} }
h.sess.Notify("Book Modified!", "", "success")
h.sess.Save(h.w, h.r) h.sess.Save(h.w, h.r)
if h.db.IsBookActive(id) { if h.db.IsBookActive(id) {
http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound) http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound)

View file

@ -4,6 +4,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/go-pg/pg"
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
) )
@ -132,7 +134,11 @@ func (db *pgDB) UpdateBook(id string, data map[string]interface{}) error {
setCondition += ", " setCondition += ", "
} }
setCondition += col + " = ?" 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{}). _, err := db.sql.Model(&Book{}).
Set(setCondition, params...). Set(setCondition, params...).