Skip to content

Commit

Permalink
Merge pull request #21 from nuveo/update
Browse files Browse the repository at this point in the history
Update closes #9
  • Loading branch information
felipeweb committed Dec 5, 2016
2 parents 41da80e + a6becd4 commit a68223a
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
36 changes: 36 additions & 0 deletions adapters/postgres/postgres.go
Expand Up @@ -169,3 +169,39 @@ func Delete(database, schema, table, where string) (jsonData []byte, err error)
jsonData, err = json.Marshal(data)
return
}

// Update execute update sql into a table
func Update(database, schema, table, where string, body api.Request) (jsonData []byte, err error) {
var result sql.Result
var rowsAffected int64

fields := []string{}
for key, value := range body.Data {
fields = append(fields, fmt.Sprintf("%s='%s'", key, value))
}
setSyntax := strings.Join(fields, ", ")

sql := fmt.Sprintf("UPDATE %s.%s.%s SET %s", database, schema, table, setSyntax)

if where != "" {
sql = fmt.Sprint(
sql,
" WHERE ",
where)
}

db := Conn()
result, err = db.Exec(sql)
if err != nil {
return
}
rowsAffected, err = result.RowsAffected()
if err != nil {
return
}

data := make(map[string]interface{})
data["rows_affected"] = rowsAffected
jsonData, err = json.Marshal(data)
return
}
13 changes: 13 additions & 0 deletions adapters/postgres/postgres_test.go
Expand Up @@ -73,3 +73,16 @@ func TestDelete(t *testing.T) {
So(len(json), ShouldBeGreaterThan, 0)
})
}

func TestUpdate(t *testing.T) {
Convey("Update data into a table", t, func() {
r := api.Request{
Data: map[string]string{
"name": "prest",
},
}
json, err := Update("prest", "public", "test", "name='prest'", r)
So(err, ShouldBeNil)
So(len(json), ShouldBeGreaterThan, 0)
})
}
41 changes: 41 additions & 0 deletions controllers/tables.go
Expand Up @@ -187,3 +187,44 @@ func DeleteFromTable(w http.ResponseWriter, r *http.Request) {

w.Write(object)
}

// UpdateTable perform update table
func UpdateTable(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
database, ok := vars["database"]
if !ok {
log.Println("Unable to parse database in URI")
http.Error(w, "Unable to parse database in URI", http.StatusInternalServerError)
return
}
schema, ok := vars["schema"]
if !ok {
log.Println("Unable to parse schema in URI")
http.Error(w, "Unable to parse schema in URI", http.StatusInternalServerError)
return
}
table, ok := vars["table"]
if !ok {
log.Println("Unable to parse table in URI")
http.Error(w, "Unable to parse table in URI", http.StatusInternalServerError)
return
}

where := postgres.WhereByRequest(r)
req := api.Request{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

object, err := postgres.Update(database, schema, table, where, req)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Write(object)
}
24 changes: 24 additions & 0 deletions controllers/tables_test.go
Expand Up @@ -96,3 +96,27 @@ func TestDeleteFromTable(t *testing.T) {
doValidDeleteRequest(server.URL + "/prest/public/test?name=nuveo")
})
}

func TestUpdateFromTable(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/{database}/{schema}/{table}", UpdateTable).Methods("PUT", "PATCH")
server := httptest.NewServer(router)
defer server.Close()
r := api.Request{
Data: map[string]string{
"name": "prest",
},
}
Convey("excute update in a table without where clause using PUT", t, func() {
doValidPutRequest(server.URL+"/prest/public/test", r)
})
Convey("excute update in a table with where clause using PUT", t, func() {
doValidPutRequest(server.URL+"/prest/public/test?name=nuveo", r)
})
Convey("excute update in a table without where clause using PATCH", t, func() {
doValidPatchRequest(server.URL+"/prest/public/test", r)
})
Convey("excute update in a table with where clause using PATCH", t, func() {
doValidPatchRequest(server.URL+"/prest/public/test?name=nuveo", r)
})
}
26 changes: 26 additions & 0 deletions controllers/utils_test.go
Expand Up @@ -48,3 +48,29 @@ func doValidDeleteRequest(url string) {
_, err = ioutil.ReadAll(resp.Body)
So(err, ShouldBeNil)
}

func doValidPutRequest(url string, r api.Request) {
byt, err := json.Marshal(r)
So(err, ShouldBeNil)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(byt))
So(err, ShouldBeNil)
client := &http.Client{}
resp, err := client.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, 200)
_, err = ioutil.ReadAll(resp.Body)
So(err, ShouldBeNil)
}

func doValidPatchRequest(url string, r api.Request) {
byt, err := json.Marshal(r)
So(err, ShouldBeNil)
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(byt))
So(err, ShouldBeNil)
client := &http.Client{}
resp, err := client.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, 200)
_, err = ioutil.ReadAll(resp.Body)
So(err, ShouldBeNil)
}
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -31,6 +31,7 @@ func main() {
r.HandleFunc("/{database}/{schema}/{table}", controllers.SelectFromTables).Methods("GET")
r.HandleFunc("/{database}/{schema}/{table}", controllers.InsertInTables).Methods("POST")
r.HandleFunc("/{database}/{schema}/{table}", controllers.DeleteFromTable).Methods("DELETE")
r.HandleFunc("/{database}/{schema}/{table}", controllers.UpdateTable).Methods("PUT", "PATCH")

n.UseHandler(r)
n.Run(fmt.Sprintf(":%v", cfg.HTTPPort))
Expand Down

0 comments on commit a68223a

Please sign in to comment.