diff --git a/README.md b/README.md index eff20ef..f0e4973 100644 --- a/README.md +++ b/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 `docker-compose up --build` diff --git a/internal/app/api.go b/internal/app/api.go index d5ce527..39d432c 100644 --- a/internal/app/api.go +++ b/internal/app/api.go @@ -12,12 +12,16 @@ type User struct { UserID int Title string CreatedOn time.Time + Uuid string } type Users struct { users []User } +/** +Sample request http://localhost:5000/get-data?title=shahzad +*/ func UserGetHandler(w http.ResponseWriter, req *http.Request) { if req.Method == "GET" { @@ -32,17 +36,17 @@ func UserGetHandler(w http.ResponseWriter, req *http.Request) { // we only want the single item. userTitle := title[0] - users := Users{} + usersData := Users{} - err := queryRepos(&users, userTitle) + err := queryRepos(&usersData, userTitle) if err != nil { http.Error(w, err.Error(), 500) 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 { http.Error(w, err.Error(), 500) 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) { if req.Method == "POST" { if err := req.ParseForm(); err != nil { @@ -71,18 +78,15 @@ func UserPostHandler(w http.ResponseWriter, req *http.Request) { } title := req.FormValue("title") - sqlInsert := ` -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().Format(time.RFC3339), uuid.String()) + row := Db.QueryRow(SqlPostUser, title, time.Now().Format(time.RFC3339), uuid.String()) err := row.Scan(&user_id) if err != nil { - panic(err) + fmt.Fprintf(w, err.Error()) } - fmt.Fprintf(w, "user created.") + fmt.Fprintf(w, `title=`+title) } else { fmt.Fprintf(w, "Invalid data") } @@ -92,17 +96,8 @@ RETURNING user_id` } 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 { return err } @@ -113,6 +108,7 @@ func queryRepos(users *Users, userTitle string) error { &user.UserID, &user.Title, &user.CreatedOn, + &user.Uuid, ) if err != nil { return err diff --git a/internal/app/db.go b/internal/app/db.go index f53d291..5c5aad1 100644 --- a/internal/app/db.go +++ b/internal/app/db.go @@ -14,6 +14,23 @@ const ( 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 func InitDb() {