post reuqest handled plus some checks
This commit is contained in:
parent
d176131378
commit
5f593b769a
|
@ -10,6 +10,6 @@ To run docker compose
|
|||
|
||||
|
||||
To connect with psql using following command
|
||||
`docker exec -it genity-code-review_db_1 psql -U root_user -W db_genity`
|
||||
`docker exec -it poc-golang-postgres-docker_db_1 psql -U root_user -W db_genity`
|
||||
|
||||
|
||||
|
|
34
api.go
34
api.go
|
@ -17,7 +17,8 @@ type Users struct {
|
|||
users []User
|
||||
}
|
||||
|
||||
func UserHandler(w http.ResponseWriter, req *http.Request) {
|
||||
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method == "GET" {
|
||||
users := Users{}
|
||||
|
||||
err := queryRepos(&users)
|
||||
|
@ -26,7 +27,6 @@ func UserHandler(w http.ResponseWriter, req *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
|
||||
out, err := json.Marshal(users.users)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
|
@ -34,6 +34,36 @@ func UserHandler(w http.ResponseWriter, req *http.Request) {
|
|||
}
|
||||
|
||||
fmt.Fprintf(w, string(out))
|
||||
} else {
|
||||
fmt.Fprintf(w, "Sorry, only Get methods are supported for thus api.")
|
||||
}
|
||||
}
|
||||
|
||||
func UserPostHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method == "POST" {
|
||||
if err := req.ParseForm(); err != nil {
|
||||
fmt.Fprintf(w, "ParseForm() err: %v", err)
|
||||
return
|
||||
}
|
||||
title := req.FormValue("title")
|
||||
sqlInsert := `
|
||||
INSERT INTO users (title, created_on)
|
||||
VALUES ($1, $2)
|
||||
RETURNING user_id`
|
||||
if len(title) > 0 {
|
||||
user_id := 0
|
||||
row := db.QueryRow(sqlInsert, title, time.Now())
|
||||
err := row.Scan(&user_id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Fprintf(w, "user creted")
|
||||
} else {
|
||||
fmt.Fprintf(w, "Invalid data")
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "Sorry, only POST methods are supported for thus api.")
|
||||
}
|
||||
}
|
||||
|
||||
func queryRepos(users *Users) error {
|
||||
|
|
2
db_dump
2
db_dump
|
@ -1,4 +1,4 @@
|
|||
CREATE TABLE users ( user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL);
|
||||
INSERT INTO users (title,created_on) VALUES('hemani','2013-06-01');
|
||||
INSERT INTO users (title,created_on) VALUES('shahzad','2013-06-01');
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue