Files
MyClub/internal/controllers/response_helper.go
T
Tomas Dvorak 9ccca365b3 dev day #65
2025-10-19 18:09:28 +02:00

133 lines
3.3 KiB
Go

package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
)
// ResponseHelper provides standardized API response methods
type ResponseHelper struct{}
// Response represents a standard API response
type Response struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
Meta interface{} `json:"meta,omitempty"`
}
// NewResponseHelper creates a new ResponseHelper instance
func NewResponseHelper() *ResponseHelper {
return &ResponseHelper{}
}
// Success sends a successful response
func (rh *ResponseHelper) Success(c *gin.Context, data interface{}, message string) {
c.JSON(http.StatusOK, Response{
Success: true,
Message: message,
Data: data,
})
}
// SuccessWithMeta sends a successful response with metadata (e.g., pagination)
func (rh *ResponseHelper) SuccessWithMeta(c *gin.Context, data interface{}, meta interface{}, message string) {
c.JSON(http.StatusOK, Response{
Success: true,
Message: message,
Data: data,
Meta: meta,
})
}
// Created sends a 201 Created response
func (rh *ResponseHelper) Created(c *gin.Context, data interface{}, message string) {
c.JSON(http.StatusCreated, Response{
Success: true,
Message: message,
Data: data,
})
}
// NoContent sends a 204 No Content response
func (rh *ResponseHelper) NoContent(c *gin.Context) {
c.Status(http.StatusNoContent)
}
// BadRequest sends a 400 Bad Request response
func (rh *ResponseHelper) BadRequest(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, Response{
Success: false,
Error: message,
})
}
// Unauthorized sends a 401 Unauthorized response
func (rh *ResponseHelper) Unauthorized(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, Response{
Success: false,
Error: message,
})
}
// Forbidden sends a 403 Forbidden response
func (rh *ResponseHelper) Forbidden(c *gin.Context, message string) {
c.JSON(http.StatusForbidden, Response{
Success: false,
Error: message,
})
}
// NotFound sends a 404 Not Found response
func (rh *ResponseHelper) NotFound(c *gin.Context, message string) {
c.JSON(http.StatusNotFound, Response{
Success: false,
Error: message,
})
}
// Conflict sends a 409 Conflict response
func (rh *ResponseHelper) Conflict(c *gin.Context, message string) {
c.JSON(http.StatusConflict, Response{
Success: false,
Error: message,
})
}
// InternalError sends a 500 Internal Server Error response
func (rh *ResponseHelper) InternalError(c *gin.Context, message string) {
c.JSON(http.StatusInternalServerError, Response{
Success: false,
Error: message,
})
}
// ValidationError sends a 422 Unprocessable Entity response
func (rh *ResponseHelper) ValidationError(c *gin.Context, errors interface{}) {
c.JSON(http.StatusUnprocessableEntity, Response{
Success: false,
Error: "Validation failed",
Data: errors,
})
}
// Custom sends a custom status code response
func (rh *ResponseHelper) Custom(c *gin.Context, statusCode int, success bool, data interface{}, message string, errMsg string) {
response := Response{
Success: success,
Data: data,
}
if message != "" {
response.Message = message
}
if errMsg != "" {
response.Error = errMsg
}
c.JSON(statusCode, response)
}
// Global ResponseHelper instance for convenience
var Respond = NewResponseHelper()