Skip to content

Commit c4c47a2

Browse files
committed
refactored api to handle post request with body processing
1 parent a6a5c32 commit c4c47a2

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

do_db_query.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package main
22

33
import (
44
"database/sql"
5+
"errors"
56
"fmt"
67
"log"
78
)
89

9-
func do_db_query(query_string string) {
10+
func do_db_query(query_string string) ([]interface{}, error) {
1011
db_type := "postgres"
1112
db_user := "postgres"
1213
db_password := "123456"
@@ -31,6 +32,7 @@ func do_db_query(query_string string) {
3132
cols, _ := rows.Columns()
3233

3334
var query_results []interface{}
35+
var error error
3436

3537
for rows.Next() {
3638

@@ -42,33 +44,37 @@ func do_db_query(query_string string) {
4244
}
4345

4446
if err := rows.Scan(valuePointers...); err != nil {
45-
log.Fatal(err)
47+
error = err
4648
}
4749

4850
for i, col := range cols {
4951
query_result_dict := map[interface{}]interface{}{}
52+
var query_value interface{}
5053

5154
val := values[i]
5255
b, ok := val.([]byte)
5356

54-
var v interface{}
55-
5657
if ok {
57-
v = string(b)
58+
query_value = string(b)
5859
} else {
59-
v = val
60+
query_value = val
6061
}
61-
query_result_dict[col] = v
62+
query_result_dict[col] = query_value
6263

6364
fmt.Println(query_result_dict)
6465
query_results = append(query_results, query_result_dict)
6566
}
6667

67-
fmt.Println("resutls---------")
68+
fmt.Println("resutls---------", "")
69+
// fmt.Println("")
6870
fmt.Println(query_results)
6971

7072
}
7173

74+
if len(query_results) < 1 {
75+
error = errors.New("empty name")
76+
}
77+
7278
defer db.Close()
73-
// return nil
79+
return query_results, error
7480
}

main.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io/ioutil"
57
"log"
68
"net/http"
79
"strconv"
@@ -18,6 +20,10 @@ import (
1820

1921
// }
2022

23+
type QueryRequest struct {
24+
QueryString string `json:"query_string"`
25+
}
26+
2127
func notFound(w http.ResponseWriter, r *http.Request) {
2228
w.Header().Set("Content-Type", "application/json")
2329
w.WriteHeader(http.StatusNotFound)
@@ -26,6 +32,20 @@ func notFound(w http.ResponseWriter, r *http.Request) {
2632

2733
func post(w http.ResponseWriter, r *http.Request) {
2834
w.Header().Set("Content-Type", "application/json")
35+
reqBody, _ := ioutil.ReadAll(r.Body)
36+
37+
var queryrequest QueryRequest
38+
json.Unmarshal(reqBody, &queryrequest)
39+
fmt.Println(queryrequest)
40+
41+
response, err := do_db_query(queryrequest.QueryString)
42+
if err != nil {
43+
w.WriteHeader(http.StatusInternalServerError)
44+
w.Write([]byte(`{"message": "error"}`))
45+
}
46+
47+
fmt.Println(response)
48+
2949
w.WriteHeader(http.StatusCreated)
3050
w.Write([]byte(`{"message": "post called"}`))
3151
}
@@ -68,7 +88,7 @@ func withParams(w http.ResponseWriter, r *http.Request) {
6888
w.Write([]byte(fmt.Sprintf(`{"userID": %d, "commentID": %d, "location": "%s" }`, userID, commentID, location)))
6989
}
7090

71-
func main() {
91+
func handleRequests() {
7292
r := mux.NewRouter()
7393
api := r.PathPrefix("/api/v1").Subrouter()
7494
api.HandleFunc("", get).Methods(http.MethodGet)
@@ -78,3 +98,7 @@ func main() {
7898

7999
log.Fatal(http.ListenAndServe(":8080", r))
80100
}
101+
102+
func main() {
103+
handleRequests()
104+
}

0 commit comments

Comments
 (0)