File size: 1,093 Bytes
3f70c4e
 
 
 
 
 
8b40e41
 
3f70c4e
 
 
 
 
fd402c4
3f70c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package utils

import (
	"net/http"
	"reflect"

	"api.qobiltu.id/models"
	"api.qobiltu.id/services"
	"github.com/gin-gonic/gin"
)

func ResponseOK(c *gin.Context, data any) {
	c.JSON(http.StatusOK, gin.H{"message": "Request Success!", "data": data, "status": "success"})
	return
}

func ResponseFAIL(c *gin.Context, status int, exception models.Exception) {
	message := exception.Message
	exception.Message = ""
	c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception})
	return
}

func SendResponse(c *gin.Context, data services.Service[any, any]) {
	if reflect.ValueOf(data.Exception).IsNil() {
		ResponseOK(c, data)
	} else {
		if data.Exception.Unauthorized {
			ResponseFAIL(c, 401, data.Exception)
		} else if data.Exception.BadRequest {
			ResponseFAIL(c, 400, data.Exception)
		} else if data.Exception.DataNotFound {
			ResponseFAIL(c, 404, data.Exception)
		} else if data.Exception.InternalServerError {
			ResponseFAIL(c, 500, data.Exception)
		} else {
			ResponseFAIL(c, 403, data.Exception)
		}
	}
}