-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (120 loc) · 4.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
log "github.com/sirupsen/logrus"
"net/http"
"github.com/unrolled/render"
"github.com/CrudGO/user"
"github.com/gorilla/mux"
"encoding/json"
)
/*
GetAllUsers - § Return a list of all users as a JSON string
e.g.:
{ "users":
[{ "username": "jsmith", "displayName": "John Smith", "department": "Sales" },
{ "username": "jdoe", "displayName": "John Doe", "department": "Development" }]
}
*/
func GetAllUsers(w http.ResponseWriter, r *http.Request) {
render := render.New()
users,err := user.GetAllUsers()
//check for error, if there is error then return 400 status code with empty user, else return status 200 with all users
if(err != nil){
render.JSON(w,http.StatusBadRequest, users)
}else{
render.JSON(w,http.StatusOK, users)
}
}
/*
GetUserByName - Return the data of a particular user as a JSON string or status code 404 if not found
e.g.:
{ "displayName": "John Smith", "department": "Sales" }
*/
func GetUserByName(w http.ResponseWriter, r *http.Request) {
render := render.New()
//getting username from the API url
vars := mux.Vars(r)
username := vars["username"]
returnedUser,err := user.GetUserByName(username);
getUserAPIResponse := user.Response{}
//check for error, if there is error then return 404 status code with empty returnedUSer, else return status 200 with all users
if(err != nil){
getUserAPIResponse.Message = "User not found"
getUserAPIResponse.Status = http.StatusNotFound
render.JSON(w,http.StatusNotFound, getUserAPIResponse)
}else{
render.JSON(w,http.StatusOK, returnedUser)
}
return
}
//InsertUser - inserts user to database
func InsertUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
render := render.New()
inputUser := user.User{}
insertAPIResponse := user.Response{}
//decoding the request into team, so that it can be used to save the team details
err := json.NewDecoder(r.Body).Decode(&inputUser)
if err != nil {
insertAPIResponse.Message = "Invalid input : Unable to parse input body"
insertAPIResponse.Status = http.StatusBadRequest
render.JSON(w,http.StatusBadRequest, insertAPIResponse)
return
}
//check for empty username, Node : this condition is not merged with prev error condition to avoid panic
if len(inputUser.UserName) <= 0{
insertAPIResponse.Message = "Invalid input : Please enter username"
insertAPIResponse.Status = http.StatusBadRequest
render.JSON(w,http.StatusBadRequest, insertAPIResponse)
return
}
exist, err := user.InsertNewUser(inputUser)
//check if user already exist in the database
if exist {
insertAPIResponse.Message = "User already found in the database"
insertAPIResponse.Status = http.StatusConflict
render.JSON(w,http.StatusConflict, insertAPIResponse)
return
}
//check for error returned from InsertNewUser function
if(err != nil){
insertAPIResponse.Message = "Unable to insert user into database"
insertAPIResponse.Status = http.StatusForbidden
render.JSON(w,http.StatusForbidden, insertAPIResponse)
}else{
insertAPIResponse.Message = "Inserted user into database"
insertAPIResponse.Status = http.StatusOK
render.JSON(w,http.StatusOK, insertAPIResponse)
}
return
}
//DeleteUser - test route
func DeleteUser(w http.ResponseWriter, r *http.Request) {
render := render.New()
//getting username from the API url
vars := mux.Vars(r)
username := vars["username"]
err := user.DeleteUser(username)
deleteAPIResponse := user.Response{}
if(err != nil){
deleteAPIResponse.Message = "Unable to delete user from database"
deleteAPIResponse.Status = http.StatusNotFound
render.JSON(w,http.StatusNotFound, deleteAPIResponse)
}else{
deleteAPIResponse.Message = "Deleted user from database"
deleteAPIResponse.Status = http.StatusOK
render.JSON(w,http.StatusOK, deleteAPIResponse)
}
return
}
func main() {
log.Info("Started CRUD following are the APIs available.... \n 1. Create New User \n 2. Update user \n 3. Get All Users")
//router for all APIs
router := mux.NewRouter()
router.HandleFunc("/users", GetAllUsers).Methods("GET") //Get all users present in the database
router.HandleFunc("/users", InsertUser).Methods("POST") //inserts a user into database
router.HandleFunc("/users/{username}", GetUserByName).Methods("GET") //get user by user name
router.HandleFunc("/users/{username}", DeleteUser).Methods("DELETE") // deletes user corresponds to username
log.Fatal(http.ListenAndServe(":8888", router))
}