some refactoring
This commit is contained in:
parent
0242bce0c0
commit
532aa65e38
11
README.md
11
README.md
|
@ -1,6 +1,15 @@
|
||||||
|
|
||||||
|
|
||||||
Just basic golang and postgres docker poc.
|
#How to run this project
|
||||||
|
|
||||||
|
##Navigate to root of project, run following command
|
||||||
|
`make run`
|
||||||
|
|
||||||
|
###API interfacing
|
||||||
|
* Get request
|
||||||
|
`curl http://localhost:5000/get-data?title=shahzad`
|
||||||
|
* Post request
|
||||||
|
`curl --request POST 'http://localhost:5000/post-data' --data "title=shahzad"`
|
||||||
|
|
||||||
To build docker compose
|
To build docker compose
|
||||||
`docker-compose up --build`
|
`docker-compose up --build`
|
||||||
|
|
|
@ -12,12 +12,16 @@ type User struct {
|
||||||
UserID int
|
UserID int
|
||||||
Title string
|
Title string
|
||||||
CreatedOn time.Time
|
CreatedOn time.Time
|
||||||
|
Uuid string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Users struct {
|
type Users struct {
|
||||||
users []User
|
users []User
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sample request http://localhost:5000/get-data?title=shahzad
|
||||||
|
*/
|
||||||
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method == "GET" {
|
if req.Method == "GET" {
|
||||||
|
|
||||||
|
@ -32,17 +36,17 @@ func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
// we only want the single item.
|
// we only want the single item.
|
||||||
userTitle := title[0]
|
userTitle := title[0]
|
||||||
|
|
||||||
users := Users{}
|
usersData := Users{}
|
||||||
|
|
||||||
err := queryRepos(&users, userTitle)
|
err := queryRepos(&usersData, 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 {
|
if len(usersData.users) > 0 {
|
||||||
|
|
||||||
out, err := json.Marshal(users.users)
|
out, err := json.Marshal(usersData.users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
|
@ -57,6 +61,9 @@ func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sample request curl --request POST 'http://localhost:5000/post-data' --data "title=shahzad"
|
||||||
|
*/
|
||||||
func UserPostHandler(w http.ResponseWriter, req *http.Request) {
|
func UserPostHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method == "POST" {
|
if req.Method == "POST" {
|
||||||
if err := req.ParseForm(); err != nil {
|
if err := req.ParseForm(); err != nil {
|
||||||
|
@ -71,18 +78,15 @@ func UserPostHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
title := req.FormValue("title")
|
title := req.FormValue("title")
|
||||||
sqlInsert := `
|
|
||||||
INSERT INTO users (title, created_on,uuid)
|
|
||||||
VALUES ($1, $2, $3)
|
|
||||||
RETURNING user_id`
|
|
||||||
if len(title) > 0 {
|
if len(title) > 0 {
|
||||||
user_id := 0
|
user_id := 0
|
||||||
row := Db.QueryRow(sqlInsert, title, time.Now().Format(time.RFC3339), uuid.String())
|
row := Db.QueryRow(SqlPostUser, 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)
|
fmt.Fprintf(w, err.Error())
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "user created.")
|
fmt.Fprintf(w, `title=`+title)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, "Invalid data")
|
fmt.Fprintf(w, "Invalid data")
|
||||||
}
|
}
|
||||||
|
@ -92,17 +96,8 @@ RETURNING user_id`
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryRepos(users *Users, userTitle string) error {
|
func queryRepos(users *Users, userTitle string) error {
|
||||||
query := `
|
|
||||||
SELECT
|
|
||||||
user_id,
|
|
||||||
title,
|
|
||||||
created_on
|
|
||||||
|
|
||||||
FROM users
|
|
||||||
WHERE title='` + userTitle + `';
|
|
||||||
`
|
|
||||||
|
|
||||||
rows, err := Db.Query(query)
|
rows, err := Db.Query(SqlGetUser, userTitle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -113,6 +108,7 @@ func queryRepos(users *Users, userTitle string) error {
|
||||||
&user.UserID,
|
&user.UserID,
|
||||||
&user.Title,
|
&user.Title,
|
||||||
&user.CreatedOn,
|
&user.CreatedOn,
|
||||||
|
&user.Uuid,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -14,6 +14,23 @@ const (
|
||||||
dbname = "dbname"
|
dbname = "dbname"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SqlGetUser = `
|
||||||
|
SELECT
|
||||||
|
user_id,
|
||||||
|
title,
|
||||||
|
created_on,
|
||||||
|
uuid
|
||||||
|
|
||||||
|
FROM users
|
||||||
|
WHERE title=$1;
|
||||||
|
`
|
||||||
|
SqlPostUser = `
|
||||||
|
INSERT INTO users (title, created_on,uuid)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
RETURNING user_id`
|
||||||
|
)
|
||||||
|
|
||||||
var Db *sql.DB
|
var Db *sql.DB
|
||||||
|
|
||||||
func InitDb() {
|
func InitDb() {
|
||||||
|
|
Loading…
Reference in a new issue