Code cleanup WIP 4
This commit is contained in:
parent
ba40f0192c
commit
f0f58efe36
2
Makefile
2
Makefile
|
@ -5,6 +5,6 @@ run: build
|
||||||
docker-compose up
|
docker-compose up
|
||||||
|
|
||||||
build-binary:
|
build-binary:
|
||||||
GOOS=linux GOARCH=amd64 go build
|
GOOS=linux GOARCH=amd64 go build -o bins/genityapp -v cmd/genityapp/*
|
||||||
|
|
||||||
.PHONY: build-binary run build
|
.PHONY: build-binary run build
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"genityapi"
|
"domain/genityapp/genityapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
|
@ -32,7 +32,6 @@ func main() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func initDb() {
|
func initDb() {
|
||||||
config := dbConfig()
|
config := dbConfig()
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
dbhost=genitywebapp_db_1
|
dbhost=${COMPOSE_PROJECT_NAME}_db_1
|
||||||
dbport=5432
|
dbport=5432
|
||||||
dbuser=root_user
|
dbuser=root_user
|
||||||
dbpass=shahzad12
|
dbpass=shahzad12
|
||||||
|
|
|
@ -3,6 +3,7 @@ package genityapi
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
uuid "github.com/nu7hatch/gouuid"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -19,14 +20,28 @@ type Users struct {
|
||||||
|
|
||||||
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method == "GET" {
|
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{}
|
users := Users{}
|
||||||
|
|
||||||
err := queryRepos(&users)
|
err := queryRepos(&users, userTitle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(users.users) > 0 {
|
||||||
|
|
||||||
out, err := json.Marshal(users.users)
|
out, err := json.Marshal(users.users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
|
@ -34,6 +49,9 @@ func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, string(out))
|
fmt.Fprintf(w, string(out))
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "User does not exist")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, "Sorry, only Get methods are supported for thus api.")
|
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)
|
fmt.Fprintf(w, "ParseForm() err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uuid, err := uuid.NewV4()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
title := req.FormValue("title")
|
title := req.FormValue("title")
|
||||||
sqlInsert := `
|
sqlInsert := `
|
||||||
INSERT INTO users (title, created_on)
|
INSERT INTO users (title, created_on,uuid)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2, $3)
|
||||||
RETURNING user_id`
|
RETURNING user_id`
|
||||||
if len(title) > 0 {
|
if len(title) > 0 {
|
||||||
user_id := 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)
|
err := row.Scan(&user_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "user creted")
|
fmt.Fprintf(w, "user created.")
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, "Invalid data")
|
fmt.Fprintf(w, "Invalid data")
|
||||||
}
|
}
|
||||||
|
@ -66,15 +91,18 @@ RETURNING user_id`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryRepos(users *Users) error {
|
func queryRepos(users *Users, userTitle string) error {
|
||||||
rows, err := db.Query(`
|
query := `
|
||||||
SELECT
|
SELECT
|
||||||
user_id,
|
user_id,
|
||||||
title,
|
title,
|
||||||
created_on
|
created_on
|
||||||
|
|
||||||
FROM users;
|
FROM users
|
||||||
`)
|
WHERE title='` + userTitle + `';
|
||||||
|
`
|
||||||
|
|
||||||
|
rows, err := db.Query(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue