@@ -2,6 +2,7 @@ package core
22
33import (
44 "context"
5+ "fmt"
56 "net/http"
67 "reflect"
78)
@@ -210,26 +211,6 @@ func DecodeAndCheckId(w http.ResponseWriter, r *http.Request, obj interface{}, k
210211 }
211212 return CheckId (w , r , obj , keysJson , mapIndex , options ... )
212213}
213- func HandleDelete (w http.ResponseWriter , r * http.Request , count int64 , err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) {
214- var resource , action string
215- if len (options ) > 0 && len (options [0 ]) > 0 {
216- resource = options [0 ]
217- }
218- if len (options ) > 1 && len (options [1 ]) > 0 {
219- action = options [1 ]
220- }
221- if err != nil {
222- RespondAndLog (w , r , http .StatusInternalServerError , InternalServerError , err , logError , writeLog , resource , action )
223- return
224- }
225- if count > 0 {
226- Succeed (w , r , http .StatusOK , count , writeLog , resource , action )
227- } else if count == 0 {
228- ReturnAndLog (w , r , http .StatusNotFound , count , writeLog , false , resource , action , "Data Not Found" )
229- } else {
230- ReturnAndLog (w , r , http .StatusConflict , count , writeLog , false , resource , action , "Conflict" )
231- }
232- }
233214func BuildFieldMapAndCheckId (w http.ResponseWriter , r * http.Request , obj interface {}, keysJson []string , mapIndex map [string ]int , ignorePatch bool ) (* http.Request , map [string ]interface {}, error ) {
234215 if ignorePatch == false {
235216 r = r .WithContext (context .WithValue (r .Context (), Method , Patch ))
@@ -252,22 +233,7 @@ func BuildMapAndCheckId(w http.ResponseWriter, r *http.Request, obj interface{},
252233 }
253234 return r2 , json , er1
254235}
255- func BuildTrackingMapAndCheckId (w http.ResponseWriter , r * http.Request , obj interface {}, keysJson []string , mapIndex map [string ]int , buildPatch func (context.Context , interface {}) error , opts ... bool ) (* http.Request , map [string ]interface {}, error ) {
256- ignorePatch := false
257- if len (opts ) > 0 {
258- ignorePatch = opts [0 ]
259- }
260- r2 , body , er0 := BuildFieldMapAndCheckId (w , r , obj , keysJson , mapIndex , ignorePatch )
261- if er0 != nil {
262- return r2 , body , er0
263- }
264- json , er1 := BodyToJsonMap (r , obj , body , keysJson , mapIndex , buildPatch )
265- if er1 != nil {
266- http .Error (w , er1 .Error (), http .StatusBadRequest )
267- }
268- return r2 , json , er1
269- }
270- func HasError (w http.ResponseWriter , r * http.Request , errors []ErrorMessage , err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) bool {
236+ func HasError (w http.ResponseWriter , r * http.Request , errors []ErrorMessage , err error , logError func (context.Context , string , ... map [string ]interface {}), model interface {}, writeLog func (context.Context , string , string , bool , string ) error , options ... string ) bool {
271237 var resource , action string
272238 if len (options ) > 0 && len (options [0 ]) > 0 {
273239 resource = options [0 ]
@@ -276,16 +242,31 @@ func HasError(w http.ResponseWriter, r *http.Request, errors []ErrorMessage, err
276242 action = options [1 ]
277243 }
278244 if err != nil {
279- RespondAndLog (w , r , http .StatusInternalServerError , InternalServerError , err , logError , writeLog , resource , action )
245+ if writeLog != nil {
246+ writeLog (r .Context (), resource , action , false , err .Error ())
247+ }
248+ if logError != nil {
249+ if IsNil (model ) {
250+ logError (r .Context (), err .Error ())
251+ } else {
252+ logError (r .Context (), err .Error (), MakeMap (model ))
253+ }
254+ }
255+ if logError == nil && writeLog == nil {
256+ http .Error (w , err .Error (), http .StatusInternalServerError )
257+ } else {
258+ http .Error (w , InternalServerError , http .StatusInternalServerError )
259+ }
280260 return true
281261 }
282262 if len (errors ) > 0 {
283- ReturnAndLog (w , r , http .StatusUnprocessableEntity , errors , writeLog , false , resource , action , "Data Validation Failed" )
263+ writeLog (r .Context (), resource , action , false , fmt .Sprintf ("Data Validation Failed %+v Error: %+v" , model , err ))
264+ JSON (w , http .StatusUnprocessableEntity , errors )
284265 return true
285266 }
286267 return false
287268}
288- func HandleResult (w http.ResponseWriter , r * http.Request , body interface {} , count int64 , err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) {
269+ func AfterDeletedWithLog (w http.ResponseWriter , r * http.Request , count int64 , err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) {
289270 var resource , action string
290271 if len (options ) > 0 && len (options [0 ]) > 0 {
291272 resource = options [0 ]
@@ -294,27 +275,57 @@ func HandleResult(w http.ResponseWriter, r *http.Request, body interface{}, coun
294275 action = options [1 ]
295276 }
296277 if err != nil {
297- if IsNil (body ) {
298- RespondAndLog (w , r , http .StatusInternalServerError , InternalServerError , err , logError , writeLog , resource , action )
278+ if logError != nil {
279+ logError (r .Context (), "DELETE " + r .URL .Path + " with error " + err .Error ())
280+ }
281+ if writeLog != nil {
282+ writeLog (r .Context (), resource , action , false , "DELETE " + r .URL .Path + " with error " + err .Error ())
283+ }
284+ if logError == nil && writeLog == nil {
285+ JSON (w , http .StatusInternalServerError , err .Error ())
299286 } else {
300- logError (r .Context (), err .Error (), MakeMap (body ))
301- RespondAndLog (w , r , http .StatusInternalServerError , InternalServerError , err , nil , writeLog , resource , action )
287+ JSON (w , http .StatusInternalServerError , InternalServerError )
302288 }
303289 return
304290 }
305291 if count > 0 {
306- if IsNil (body ) {
307- Succeed (w , r , http .StatusOK , count , writeLog , resource , action )
292+ if writeLog != nil {
293+ writeLog (r .Context (), resource , action , true , "DELETE " + r .URL .Path )
294+ }
295+ JSON (w , http .StatusOK , count )
296+ } else if count == 0 {
297+ if writeLog != nil {
298+ writeLog (r .Context (), resource , action , false , "Data Not Found " + r .URL .Path )
299+ }
300+ JSON (w , http .StatusNotFound , count )
301+ } else {
302+ if writeLog != nil {
303+ writeLog (r .Context (), resource , action , false , "DELETE " + r .URL .Path + " Conflict" )
304+ }
305+ JSON (w , http .StatusConflict , count )
306+ }
307+ }
308+ func AfterDeleted (w http.ResponseWriter , r * http.Request , count int64 , err error , logError func (context.Context , string , ... map [string ]interface {})) {
309+ if err != nil {
310+ if logError != nil {
311+ logError (r .Context (), "DELETE " + r .URL .Path + " with error " + err .Error ())
312+ }
313+ if logError == nil {
314+ JSON (w , http .StatusInternalServerError , err .Error ())
308315 } else {
309- Succeed (w , r , http .StatusOK , body , writeLog , resource , action )
316+ JSON (w , http .StatusInternalServerError , InternalServerError )
310317 }
318+ return
319+ }
320+ if count > 0 {
321+ JSON (w , http .StatusOK , count )
311322 } else if count == 0 {
312- ReturnAndLog (w , r , http .StatusNotFound , 0 , writeLog , false , resource , action , "Data Not Found" )
323+ JSON (w , http .StatusNotFound , count )
313324 } else {
314- ReturnAndLog (w , r , http .StatusConflict , count , writeLog , false , resource , action , "Data Version Error" )
325+ JSON (w , http .StatusConflict , count )
315326 }
316327}
317- func AfterCreated (w http.ResponseWriter , r * http.Request , body interface {}, count int64 , err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) {
328+ func AfterSavedWithLog (w http.ResponseWriter , r * http.Request , body interface {}, count int64 , err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) {
318329 var resource , action string
319330 if len (options ) > 0 && len (options [0 ]) > 0 {
320331 resource = options [0 ]
@@ -323,25 +334,77 @@ func AfterCreated(w http.ResponseWriter, r *http.Request, body interface{}, coun
323334 action = options [1 ]
324335 }
325336 if err != nil {
337+ if logError != nil {
338+ if IsNil (body ) {
339+ logError (r .Context (), err .Error ())
340+ } else {
341+ logError (r .Context (), err .Error (), MakeMap (body ))
342+ }
343+ }
344+ if writeLog != nil {
345+ writeLog (r .Context (), resource , action , false , err .Error ())
346+ }
347+ if logError == nil && writeLog == nil {
348+ JSON (w , http .StatusInternalServerError , err .Error ())
349+ } else {
350+ JSON (w , http .StatusInternalServerError , InternalServerError )
351+ }
352+ return
353+ }
354+ if count > 0 {
355+ if writeLog != nil {
356+ writeLog (r .Context (), resource , action , true , r .URL .Path )
357+ }
326358 if IsNil (body ) {
327- RespondAndLog (w , r , http .StatusInternalServerError , InternalServerError , err , logError , writeLog , resource , action )
359+ JSON (w , http .StatusCreated , count )
360+ } else {
361+ JSON (w , http .StatusCreated , body )
362+ }
363+ } else if count == 0 {
364+ if writeLog != nil {
365+ writeLog (r .Context (), resource , action , false , "Data Not Found " + r .URL .Path )
366+ }
367+ JSON (w , http .StatusNotFound , count )
368+ } else {
369+ if writeLog != nil {
370+ if IsNil (body ) {
371+ writeLog (r .Context (), resource , action , false , "Conflict Data" )
372+ } else {
373+ writeLog (r .Context (), resource , action , false , fmt .Sprintf ("Conflict Data %+v" , body ))
374+ }
375+ }
376+ JSON (w , http .StatusConflict , count )
377+ }
378+ }
379+ func AfterSaved (w http.ResponseWriter , r * http.Request , body interface {}, count int64 , err error , logError func (context.Context , string , ... map [string ]interface {})) {
380+ if err != nil {
381+ if logError != nil {
382+ if IsNil (body ) {
383+ logError (r .Context (), err .Error ())
384+ } else {
385+ logError (r .Context (), err .Error (), MakeMap (body ))
386+ }
387+ }
388+ if logError == nil {
389+ JSON (w , http .StatusInternalServerError , err .Error ())
328390 } else {
329- logError (r .Context (), err .Error (), MakeMap (body ))
330- RespondAndLog (w , r , http .StatusInternalServerError , InternalServerError , err , nil , writeLog , resource , action )
391+ JSON (w , http .StatusInternalServerError , InternalServerError )
331392 }
332393 return
333394 }
334395 if count > 0 {
335396 if IsNil (body ) {
336- Succeed (w , r , http .StatusCreated , count , writeLog , resource , action )
397+ JSON (w , http .StatusCreated , count )
337398 } else {
338- Succeed (w , r , http .StatusCreated , body , writeLog , resource , action )
399+ JSON (w , http .StatusCreated , body )
339400 }
401+ } else if count == 0 {
402+ JSON (w , http .StatusNotFound , count )
340403 } else {
341- ReturnAndLog (w , r , http .StatusConflict , count , writeLog , false , resource , action , "Duplicate Key" )
404+ JSON (w , http .StatusConflict , count )
342405 }
343406}
344- func Respond (w http.ResponseWriter , r * http.Request , code int , result interface {}, err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) {
407+ func AfterCreatedWithLog (w http.ResponseWriter , r * http.Request , body interface {}, count int64 , err error , logError func (context.Context , string , ... map [string ]interface {}), writeLog func (context.Context , string , string , bool , string ) error , options ... string ) {
345408 var resource , action string
346409 if len (options ) > 0 && len (options [0 ]) > 0 {
347410 resource = options [0 ]
@@ -350,8 +413,66 @@ func Respond(w http.ResponseWriter, r *http.Request, code int, result interface{
350413 action = options [1 ]
351414 }
352415 if err != nil {
353- RespondAndLog (w , r , http .StatusInternalServerError , InternalServerError , err , logError , writeLog , resource , action )
416+ if logError != nil {
417+ if IsNil (body ) {
418+ logError (r .Context (), err .Error ())
419+ } else {
420+ logError (r .Context (), err .Error (), MakeMap (body ))
421+ }
422+ }
423+ if writeLog != nil {
424+ writeLog (r .Context (), resource , action , false , err .Error ())
425+ }
426+ if logError == nil && writeLog == nil {
427+ JSON (w , http .StatusInternalServerError , err .Error ())
428+ } else {
429+ JSON (w , http .StatusInternalServerError , InternalServerError )
430+ }
431+ return
432+ }
433+ if count > 0 {
434+ if writeLog != nil {
435+ writeLog (r .Context (), resource , action , true , "" )
436+ }
437+ if IsNil (body ) {
438+ JSON (w , http .StatusCreated , count )
439+ } else {
440+ JSON (w , http .StatusCreated , body )
441+ }
442+ } else {
443+ if writeLog != nil {
444+ if IsNil (body ) {
445+ writeLog (r .Context (), resource , action , false , "Duplicate Key" )
446+ } else {
447+ writeLog (r .Context (), resource , action , false , fmt .Sprintf ("Duplicate Key %+v" , body ))
448+ }
449+ }
450+ JSON (w , http .StatusConflict , count )
451+ }
452+ }
453+ func AfterCreated (w http.ResponseWriter , r * http.Request , body interface {}, count int64 , err error , logError func (context.Context , string , ... map [string ]interface {})) {
454+ if err != nil {
455+ if logError != nil {
456+ if IsNil (body ) {
457+ logError (r .Context (), err .Error ())
458+ } else {
459+ logError (r .Context (), err .Error (), MakeMap (body ))
460+ }
461+ }
462+ if logError == nil {
463+ JSON (w , http .StatusInternalServerError , err .Error ())
464+ } else {
465+ JSON (w , http .StatusInternalServerError , InternalServerError )
466+ }
467+ return
468+ }
469+ if count > 0 {
470+ if IsNil (body ) {
471+ JSON (w , http .StatusCreated , count )
472+ } else {
473+ JSON (w , http .StatusCreated , body )
474+ }
354475 } else {
355- Succeed (w , r , code , result , writeLog , resource , action )
476+ JSON (w , http . StatusConflict , count )
356477 }
357478}
0 commit comments