point 7 8 10 11
This commit is contained in:
parent
4c2414fa0a
commit
51b1ac1fa3
58
api.go
58
api.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
uuid "github.com/nu7hatch/gouuid"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
@ -19,21 +20,38 @@ type Users struct {
|
|||
|
||||
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method == "GET" {
|
||||
|
||||
title, ok := req.URL.Query()["title"]
|
||||
|
||||
if !ok || len(title[0]) < 1 {
|
||||
http.Error(w, "Url Param 'title' is missing", 400)
|
||||
return
|
||||
}
|
||||
|
||||
// Query()["key"] will return an array of items,
|
||||
// we only want the single item.
|
||||
userTitle := title[0]
|
||||
|
||||
users := Users{}
|
||||
|
||||
err := queryRepos(&users)
|
||||
err := queryRepos(&users, userTitle)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
out, err := json.Marshal(users.users)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
if len(users.users) > 0 {
|
||||
|
||||
fmt.Fprintf(w, string(out))
|
||||
out, err := json.Marshal(users.users)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, string(out))
|
||||
} else {
|
||||
fmt.Fprintf(w, "User does not exist")
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "Sorry, only Get methods are supported for thus api.")
|
||||
}
|
||||
|
@ -45,19 +63,26 @@ func UserPostHandler(w http.ResponseWriter, req *http.Request) {
|
|||
fmt.Fprintf(w, "ParseForm() err: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
uuid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
|
||||
}
|
||||
|
||||
title := req.FormValue("title")
|
||||
sqlInsert := `
|
||||
INSERT INTO users (title, created_on)
|
||||
VALUES ($1, $2)
|
||||
INSERT INTO users (title, created_on,uuid)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING user_id`
|
||||
if len(title) > 0 {
|
||||
user_id := 0
|
||||
row := db.QueryRow(sqlInsert, title, time.Now())
|
||||
row := db.QueryRow(sqlInsert, title, time.Now().Format(time.RFC3339), uuid.String())
|
||||
err := row.Scan(&user_id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Fprintf(w, "user creted")
|
||||
fmt.Fprintf(w, "user created.")
|
||||
} else {
|
||||
fmt.Fprintf(w, "Invalid data")
|
||||
}
|
||||
|
@ -66,15 +91,18 @@ RETURNING user_id`
|
|||
}
|
||||
}
|
||||
|
||||
func queryRepos(users *Users) error {
|
||||
rows, err := db.Query(`
|
||||
func queryRepos(users *Users, userTitle string) error {
|
||||
query := `
|
||||
SELECT
|
||||
user_id,
|
||||
title,
|
||||
created_on
|
||||
|
||||
FROM users;
|
||||
`)
|
||||
FROM users
|
||||
WHERE title='` + userTitle + `';
|
||||
`
|
||||
|
||||
rows, err := db.Query(query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -4,4 +4,5 @@ go 1.14
|
|||
|
||||
require (
|
||||
github.com/lib/pq v1.8.0
|
||||
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
|
||||
)
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
CREATE TABLE users (user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL);
|
||||
CREATE TABLE users (user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL,
|
||||
uuid VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL);
|
Loading…
Reference in a new issue