2020-09-14 13:16:47 +00:00
|
|
|
package app
|
2020-09-11 09:13:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-09-11 14:06:41 +00:00
|
|
|
uuid "github.com/nu7hatch/gouuid"
|
2020-09-11 09:13:41 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
UserID int
|
|
|
|
Title string
|
|
|
|
CreatedOn time.Time
|
2020-09-16 13:20:22 +00:00
|
|
|
Uuid string
|
2020-09-11 09:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Users struct {
|
|
|
|
users []User
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:20:22 +00:00
|
|
|
/**
|
|
|
|
Sample request http://localhost:5000/get-data?title=shahzad
|
|
|
|
*/
|
2020-09-11 09:13:41 +00:00
|
|
|
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method == "GET" {
|
|
|
|
|
2020-09-11 14:06:41 +00:00
|
|
|
title, ok := req.URL.Query()["title"]
|
|
|
|
|
|
|
|
if !ok || len(title[0]) < 1 {
|
|
|
|
http.Error(w, "Url Param 'title' is missing", 400)
|
2020-09-11 09:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:06:41 +00:00
|
|
|
// Query()["key"] will return an array of items,
|
|
|
|
// we only want the single item.
|
|
|
|
userTitle := title[0]
|
|
|
|
|
2020-09-16 13:20:22 +00:00
|
|
|
usersData := Users{}
|
2020-09-11 14:06:41 +00:00
|
|
|
|
2020-09-16 13:20:22 +00:00
|
|
|
err := queryRepos(&usersData, userTitle)
|
2020-09-11 09:13:41 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:20:22 +00:00
|
|
|
if len(usersData.users) > 0 {
|
2020-09-11 14:06:41 +00:00
|
|
|
|
2020-09-16 13:20:22 +00:00
|
|
|
out, err := json.Marshal(usersData.users)
|
2020-09-11 14:06:41 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, string(out))
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "User does not exist")
|
|
|
|
}
|
2020-09-11 09:13:41 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Sorry, only Get methods are supported for thus api.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:20:22 +00:00
|
|
|
/**
|
|
|
|
Sample request curl --request POST 'http://localhost:5000/post-data' --data "title=shahzad"
|
|
|
|
*/
|
2020-09-11 09:13:41 +00:00
|
|
|
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
|
|
|
|
}
|
2020-09-11 14:06:41 +00:00
|
|
|
|
|
|
|
uuid, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-11 09:13:41 +00:00
|
|
|
title := req.FormValue("title")
|
2020-09-16 13:20:22 +00:00
|
|
|
|
2020-09-11 09:13:41 +00:00
|
|
|
if len(title) > 0 {
|
|
|
|
user_id := 0
|
2020-09-16 13:20:22 +00:00
|
|
|
row := Db.QueryRow(SqlPostUser, title, time.Now().Format(time.RFC3339), uuid.String())
|
2020-09-11 09:13:41 +00:00
|
|
|
err := row.Scan(&user_id)
|
|
|
|
if err != nil {
|
2020-09-16 13:20:22 +00:00
|
|
|
fmt.Fprintf(w, err.Error())
|
2020-09-11 09:13:41 +00:00
|
|
|
}
|
2020-09-16 13:20:22 +00:00
|
|
|
fmt.Fprintf(w, `title=`+title)
|
2020-09-11 09:13:41 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Invalid data")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Sorry, only POST methods are supported for thus api.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:06:41 +00:00
|
|
|
func queryRepos(users *Users, userTitle string) error {
|
2020-09-16 13:20:22 +00:00
|
|
|
|
|
|
|
rows, err := Db.Query(SqlGetUser, userTitle)
|
2020-09-11 09:13:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
user := User{}
|
|
|
|
err = rows.Scan(
|
|
|
|
&user.UserID,
|
|
|
|
&user.Title,
|
|
|
|
&user.CreatedOn,
|
2020-09-16 13:20:22 +00:00
|
|
|
&user.Uuid,
|
2020-09-11 09:13:41 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users.users = append(users.users, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|