/
respond.go
293 lines (234 loc) Β· 7.51 KB
/
respond.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package respond
import "strconv"
type Respond struct {
// Status code of response
statusCode int
// Status text of response
statusText string
// Error code of response
errorCode int
// Language of response
// this option work with en.yml and fa.yml files for responding
lang string
}
var Default = &Respond{}
var DefaultWithLang = func(lang string) *Respond {
return &Respond{ lang: lang }
}
// Get message type
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return *Message
func (respond Respond) Message() *Message {
var message Message
message.Lang = respond.lang
if message.Lang == "" {
message.Lang = "en"
}
data := message.LoadConfig()
return data
}
// Set status code of response and set default value as 0
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return Respond
// @param code int
func (respond Respond) SetStatusCode(code int) Respond {
if respond.statusCode == 0 {
respond.statusCode = code
}
return respond
}
// Set status code of response and set default value as 0
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return Respond
// @param code int
func (respond Respond) SetErrorCode(code int) Respond {
if respond.errorCode == 0 {
respond.errorCode = code
}
return respond
}
// Set status text of response
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return Respond
// @param text string
func (respond Respond) SetStatusText(text string) Respond {
if respond.statusText == "" {
respond.statusText = text
}
return respond
}
// Pass response with result data like this array
//
// array := map[string]interface{} {
// "status": respond.statusText,
// "result": result,
// }
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @param result map[string]interface{}
// @return (statuscode int, result interface{})
func (respond Respond) RespondWithResult(result interface{}) (int, interface{}) {
return respond.statusCode, map[string] interface{}{
"status": respond.statusText,
"result": result,
}
}
// Pass response with message text as string
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @param message interface{}
// @return (statuscode int, result interface{})
func (respond Respond) RespondWithMessage(message interface{}) (int, interface{}) {
data := map[string]interface{}{
"status": respond.statusText,
"message": message,
}
if respond.errorCode != 0 {
data["error"] = respond.errorCode
}
return respond.statusCode, data
}
// return notfound result
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) NotFound() (int, interface{}) {
return respond.Error(404, 5404)
}
// return success result with data
//
// data := map[string]interface{} {
// "data": "somedata"
// }
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @param data map[string]interface{}
// @return (statuscode int, result interface{})
func (respond Respond) Succeed(data interface{}) (int, interface{}) {
return respond.SetStatusCode(200).SetStatusText(respond.Message().Success).RespondWithResult(data)
}
// Insert action is succeed
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) InsertSucceeded() (int, interface{}) {
message := respond.Message().Errors["success"]
return respond.SetStatusCode(200).
SetStatusText(respond.Message().Success).RespondWithMessage(message["insert"])
}
// Insert action is failed
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) InsertFailed() (int, interface{}) {
message := respond.Message().Errors["failed"]
return respond.SetStatusCode(448).
SetStatusText(respond.Message().Failed).RespondWithMessage(message["insert"])
}
// Delete action is succeed
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) DeleteSucceeded() (int, interface{}) {
message := respond.Message().Errors["success"]
return respond.SetStatusCode(200).
SetStatusText(respond.Message().Success).RespondWithMessage(message["delete"])
}
// Delete action is failed
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) DeleteFailed() (int, interface{}) {
message := respond.Message().Errors["failed"]
return respond.SetStatusCode(447).
SetStatusText(respond.Message().Failed).RespondWithMessage(message["delete"])
}
// Update action is succeed
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) UpdateSucceeded() (int, interface{}) {
message := respond.Message().Errors["success"]
return respond.SetStatusCode(200).
SetStatusText(respond.Message().Success).RespondWithMessage(message["update"])
}
// Update action is failed
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) UpdateFailed() (int, interface{}) {
message := respond.Message().Errors["failed"]
return respond.SetStatusCode(449).
SetStatusText(respond.Message().Failed).RespondWithMessage(message["update"])
}
// Wrong parameters are entered
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) WrongParameters() (int, interface{}) {
return respond.Error(406, 5406)
}
// Wrong parameters are entered
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) MethodNotAllowed() (int, interface{}) {
return respond.Error(405, 5405)
}
// There ara validation translations
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @param translations map[string]interface{}
// @return (statuscode int, result interface{})
func (respond Respond) ValidationErrors(errors interface{}) (int, interface{}) {
return respond.SetStatusCode(420).
SetStatusText(respond.Message().Failed).SetErrorCode(5420).RespondWithResult(errors)
}
// The request field is not found
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) RequestFieldNotfound() (int, interface{}) {
return respond.Error(446, 1001)
}
// The request field is duplicated
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @return (statuscode int, result interface{})
func (respond Respond) RequestFieldDuplicated() (int, interface{}) {
return respond.Error(400, 1004)
}
// The error message
//
// @author Alireza Josheghani <josheghani.dev@gmail.com>
// @since 15 Mar 2018
// @param statusCode int,errorCode string
// @return (statuscode int, result interface{})
func (respond Respond) Error(statusCode int, errorCode int) (int, interface{}) {
message := respond.Message().Errors[strconv.Itoa(errorCode)]
return respond.SetStatusCode(statusCode).
SetStatusText(respond.Message().Failed).SetErrorCode(errorCode).RespondWithMessage(message["message"])
}