Skip to content

Commit cba6ee8

Browse files
committed
Addition information in JSON response
Add success, code, message and data for success response and success, code and error for error response. Affected File: - todo_handler.go - user_handler.go - jwt.go
1 parent ee8c55a commit cba6ee8

File tree

3 files changed

+162
-50
lines changed

3 files changed

+162
-50
lines changed

api/handlers/todo_handler.go

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ import (
1212
func GetAllTodos(c *gin.Context) {
1313
todos, err := services.GetAllTodos()
1414
if err != nil {
15-
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"message": err.Error()})
15+
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
16+
"success": false,
17+
"code": http.StatusBadRequest,
18+
"error": err.Error(),
19+
})
1620
return
1721
}
1822

1923
c.JSON(http.StatusOK, gin.H{
20-
"message": "GetAllTodos Success",
21-
"data": todos})
24+
"success": true,
25+
"code": http.StatusOK,
26+
"message": "Get all todos success",
27+
"data": todos,
28+
})
2229
}
2330

2431
func GetTodoByUserID(c *gin.Context) {
@@ -27,21 +34,30 @@ func GetTodoByUserID(c *gin.Context) {
2734
userID, err := getUserID(c)
2835
if err != nil {
2936
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
30-
"message": err.Error(),
37+
"success": false,
38+
"code": http.StatusBadRequest,
39+
"error": err.Error(),
3140
})
3241
return
3342
}
3443
todo.UserID = userID
3544

3645
result, err := services.GetTodoByUserID(todo)
3746
if err != nil {
38-
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"message": err.Error()})
47+
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
48+
"success": false,
49+
"code": http.StatusBadRequest,
50+
"error": err.Error(),
51+
})
3952
return
4053
}
4154

4255
c.JSON(http.StatusOK, gin.H{
43-
"message": "GetTodoByUserID Success",
44-
"data": result})
56+
"success": true,
57+
"code": http.StatusOK,
58+
"message": "Get todo by user ID success",
59+
"data": result,
60+
})
4561
}
4662

4763
func CreateTodo(c *gin.Context) {
@@ -50,33 +66,44 @@ func CreateTodo(c *gin.Context) {
5066
if err := c.BindJSON(&todo); err != nil {
5167
if err.Error() == "EOF" {
5268
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
53-
"message": "Make sure you have entered data in the body",
69+
"success": false,
70+
"code": http.StatusBadRequest,
71+
"error": "Make sure you have entered data in the body",
5472
})
5573
return
5674
}
5775
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
58-
"message": err.Error(),
76+
"success": false,
77+
"code": http.StatusBadRequest,
78+
"error": err.Error(),
5979
})
6080
return
6181
}
6282

6383
userID, err := getUserID(c)
6484
if err != nil {
6585
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
66-
"message": err.Error(),
86+
"success": false,
87+
"code": http.StatusBadRequest,
88+
"error": err.Error(),
6789
})
6890
return
6991
}
7092
todo.UserID = userID
7193

7294
if err := services.CreateTodo(todo); err != nil {
7395
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
74-
"message": err.Error(),
96+
"success": false,
97+
"code": http.StatusBadRequest,
98+
"error": err.Error(),
7599
})
76100
return
77101
}
78102
c.JSON(http.StatusOK, gin.H{
79-
"message": "CreateTodo Success",
103+
"success": true,
104+
"code": http.StatusCreated,
105+
"message": "Create todo success",
106+
"data": todo,
80107
})
81108
}
82109

@@ -86,34 +113,45 @@ func UpdateTodo(c *gin.Context) {
86113
if err := c.BindJSON(&todo); err != nil {
87114
if err.Error() == "EOF" {
88115
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
89-
"message": "Make sure you have entered data in the body",
116+
"success": false,
117+
"code": http.StatusBadRequest,
118+
"error": "Make sure you have entered data in the body",
90119
})
91120
return
92121
}
93122
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
94-
"message": err.Error(),
123+
"success": false,
124+
"code": http.StatusBadRequest,
125+
"error": err.Error(),
95126
})
96127
return
97128
}
98129

99130
userID, err := getUserID(c)
100131
if err != nil {
101132
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
102-
"message": err.Error(),
133+
"success": false,
134+
"code": http.StatusBadRequest,
135+
"error": err.Error(),
103136
})
104137
return
105138
}
106139
todo.UserID = userID
107140

108141
if err := services.UpdateTodo(todo); err != nil {
109142
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
110-
"message": err.Error(),
143+
"success": false,
144+
"code": http.StatusBadRequest,
145+
"error": err.Error(),
111146
})
112147
return
113148
}
114149

115150
c.JSON(http.StatusOK, gin.H{
116-
"message": "UpdateTodo Success",
151+
"success": true,
152+
"code": http.StatusOK,
153+
"message": "Update todo success",
154+
"data": todo,
117155
})
118156
}
119157

@@ -124,7 +162,9 @@ func DeleteTodo(c *gin.Context) {
124162
uintID, err := strconv.ParseUint(strID, 10, 0)
125163
if err != nil {
126164
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
127-
"message": err.Error(),
165+
"success": false,
166+
"code": http.StatusBadRequest,
167+
"error": err.Error(),
128168
})
129169
return
130170
}
@@ -133,19 +173,26 @@ func DeleteTodo(c *gin.Context) {
133173
userID, err := getUserID(c)
134174
if err != nil {
135175
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
136-
"message": err.Error(),
176+
"success": false,
177+
"code": http.StatusBadRequest,
178+
"error": err.Error(),
137179
})
138180
return
139181
}
140182
todo.UserID = userID
141183

142184
if err := services.DeleteTodo(todo); err != nil {
143185
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
144-
"message": err.Error(),
186+
"success": false,
187+
"code": http.StatusBadRequest,
188+
"error": err.Error(),
145189
})
146190
return
147191
}
148192
c.JSON(http.StatusOK, gin.H{
149-
"message": "DeleteTodo Success",
193+
"success": true,
194+
"code": http.StatusOK,
195+
"message": "Delete todo success",
196+
"data": todo,
150197
})
151198
}

0 commit comments

Comments
 (0)