-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes-handlers.go
122 lines (113 loc) · 3.63 KB
/
routes-handlers.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
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
func renderHome(response http.ResponseWriter, request *http.Request) {
http.ServeFile(response, request, "views/index.html")
}
func getUsers(response http.ResponseWriter, request *http.Request) {
var httpError = ErrorResponse{
Code: http.StatusInternalServerError, Message: "It's not you it's me.",
}
jsonResponse := getUsersFromDB()
if jsonResponse == nil {
returnErrorResponse(response, request, httpError)
} else {
response.Header().Set("Content-Type", "application/json")
response.Write(jsonResponse)
}
}
func insertUser(response http.ResponseWriter, request *http.Request) {
var httpError = ErrorResponse{
Code: http.StatusInternalServerError, Message: "It's not you it's me.",
}
var userDetails User
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&userDetails)
defer request.Body.Close()
if err != nil {
returnErrorResponse(response, request, httpError)
} else {
httpError.Code = http.StatusBadRequest
if userDetails.Name == "" {
httpError.Message = "First Name can't be empty"
returnErrorResponse(response, request, httpError)
} else if userDetails.Lname == "" {
httpError.Message = "Last Name can't be empty"
returnErrorResponse(response, request, httpError)
} else if userDetails.Country == "" {
httpError.Message = "Country can't be empty"
returnErrorResponse(response, request, httpError)
} else {
isInserted := insertUserInDB(userDetails)
if isInserted {
getUsers(response, request)
} else {
returnErrorResponse(response, request, httpError)
}
}
}
}
func deleteUser(response http.ResponseWriter, request *http.Request) {
var httpError = ErrorResponse{
Code: http.StatusInternalServerError, Message: "It's not you it's me.",
}
userID := mux.Vars(request)["id"]
if userID == "" {
httpError.Message = "User id can't be empty"
returnErrorResponse(response, request, httpError)
} else {
isdeleted := deleteUserFromDB(userID)
if isdeleted {
getUsers(response, request)
} else {
returnErrorResponse(response, request, httpError)
}
}
}
func updateUser(response http.ResponseWriter, request *http.Request) {
var httpError = ErrorResponse{
Code: http.StatusInternalServerError, Message: "It's not you it's me.",
}
var userDetails User
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&userDetails)
defer request.Body.Close()
if err != nil {
returnErrorResponse(response, request, httpError)
} else {
httpError.Code = http.StatusBadRequest
if userDetails.Name == "" {
httpError.Message = "First Name can't be empty"
returnErrorResponse(response, request, httpError)
} else if userDetails.ID == 0 {
httpError.Message = "user Id can't be empty"
returnErrorResponse(response, request, httpError)
} else if userDetails.Lname == "" {
httpError.Message = "Last Name can't be empty"
returnErrorResponse(response, request, httpError)
} else if userDetails.Country == "" {
httpError.Message = "Country can't be empty"
returnErrorResponse(response, request, httpError)
} else {
isUpdated := updateUserInDB(userDetails)
if isUpdated {
getUsers(response, request)
} else {
returnErrorResponse(response, request, httpError)
}
}
}
}
func returnErrorResponse(response http.ResponseWriter, request *http.Request, errorMesage ErrorResponse) {
httpResponse := &ErrorResponse{Code: errorMesage.Code, Message: errorMesage.Message}
jsonResponse, err := json.Marshal(httpResponse)
if err != nil {
panic(err)
}
response.Header().Set("Content-Type", "application/json")
response.WriteHeader(errorMesage.Code)
response.Write(jsonResponse)
}