veza/veza-backend-api/internal/response/response.go
2025-12-03 20:29:37 +01:00

79 lines
1.7 KiB
Go

package response
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Success sends a successful JSON response
func Success(c *gin.Context, data interface{}, message ...string) {
response := gin.H{
"success": true,
"data": data,
}
if len(message) > 0 {
response["message"] = message[0]
}
c.JSON(http.StatusOK, response)
}
// Created sends a 201 Created response
func Created(c *gin.Context, data interface{}, message ...string) {
response := gin.H{
"success": true,
"data": data,
}
if len(message) > 0 {
response["message"] = message[0]
}
c.JSON(http.StatusCreated, response)
}
// BadRequest sends a 400 Bad Request response
func BadRequest(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"error": message,
})
}
// Unauthorized sends a 401 Unauthorized response
func Unauthorized(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"error": message,
})
}
// Forbidden sends a 403 Forbidden response
func Forbidden(c *gin.Context, message string) {
c.JSON(http.StatusForbidden, gin.H{
"success": false,
"error": message,
})
}
// NotFound sends a 404 Not Found response
func NotFound(c *gin.Context, message string) {
c.JSON(http.StatusNotFound, gin.H{
"success": false,
"error": message,
})
}
// InternalServerError sends a 500 Internal Server Error response
func InternalServerError(c *gin.Context, message string) {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"error": message,
})
}
// Error sends a custom error response with specified status code
func Error(c *gin.Context, status int, message string) {
c.JSON(status, gin.H{
"success": false,
"error": message,
})
}