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 --- db.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'db.go') 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 } -- cgit v1.2.1