From d167a89638b4eecad852c66385737ef89354ec99 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Wed, 23 Sep 2015 22:46:47 +0100 Subject: post /caches now returns created object --- TODO | 1 - db.go | 18 +++++++++++++----- files.go | 4 ++-- handlers.go | 21 +++++++++++++++++---- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/TODO b/TODO index f0e1544..b671699 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ * Remove things that should be configy to a config file (FileRoot, db params, server port/addr, ...) * Remove mp3 assumption -* Replace the NamedExec in postCache with a Select, using INSERT ... RETURNING *, then return the APICache to the user * Betterness the Path <-> URI functions diff --git a/db.go b/db.go index a2c1d1e..b71cb54 100644 --- a/db.go +++ b/db.go @@ -39,17 +39,25 @@ func getCache(id uint64) (DBCache, error) { return cache, err } -func postCache(dbcache DBCache) error { +func postCache(incache DBCache) (DBCache, error) { + var outcache DBCache + db, err := sqlx.Connect("postgres", "user=audiocache password=audiocache dbname=audiocache sslmode=disable") if err != nil { - return err + return outcache, err + } + + rows, err := db.NamedQuery("INSERT INTO caches (latitude, longitude, created, path) VALUES (:latitude, :longitude, :created, :path) RETURNING *", incache) + if err != nil { + return outcache, err } - _, err = db.NamedExec("INSERT INTO caches (latitude, longitude, created, path) VALUES (:latitude, :longitude, :created, :path)", dbcache) + rows.Next() + err = rows.StructScan(&outcache) if err != nil { - return err + return outcache, err } err = db.Close() - return err + return outcache, err } diff --git a/files.go b/files.go index f1a3d6c..2effe6e 100644 --- a/files.go +++ b/files.go @@ -9,8 +9,8 @@ func writeFile(postcache PostCache, filename string) error { data := postcache.Data var binary []byte - n, err := base64.StdEncoding.Decode(binary, []byte(data)) - if n == 0 || err != nil { + binary, err := base64.StdEncoding.DecodeString(data) + if err != nil { return err } diff --git a/handlers.go b/handlers.go index c0ec657..77fdeed 100644 --- a/handlers.go +++ b/handlers.go @@ -34,6 +34,11 @@ func CacheIndex(w http.ResponseWriter, r *http.Request) { } } +func MarshalCache(apicache APICache) (string, error) { + str, err := json.Marshal(apicache) + return string(str), err +} + func CacheShow(w http.ResponseWriter, r *http.Request) { cacheId, err := strconv.ParseUint(mux.Vars(r)["cacheId"], 10, 64) if err != nil { @@ -48,9 +53,12 @@ func CacheShow(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) - if err := json.NewEncoder(w).Encode(DBToAPI(cache)); err != nil { + str, err := MarshalCache(DBToAPI(cache)) + if err != nil { panic(err) } + + fmt.Fprintln(w, str) } func CacheCreate(w http.ResponseWriter, r *http.Request) { @@ -72,9 +80,9 @@ func CacheCreate(w http.ResponseWriter, r *http.Request) { } filename := uuid.New() + ".mp3" - dbcache := PostToDB(postcache, filename) + incache := PostToDB(postcache, filename) - err = postCache(dbcache) + outcache, err := postCache(incache) if err != nil { panic(err) } @@ -87,5 +95,10 @@ func CacheCreate(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusCreated) - fmt.Fprintln(w, "OK") + str, err := MarshalCache(DBToAPI(outcache)) + if err != nil { + panic(err) + } + + fmt.Fprintln(w, str) } -- cgit v1.2.1