50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package common
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// APIResponse strukturiert alle erfolgreichen Antworten
|
|
type APIResponse struct {
|
|
Data interface{} `json:"data,omitempty"`
|
|
Message string `json:"message,omitempty"`
|
|
}
|
|
|
|
// APIError strukturiert Fehlerantworten
|
|
type APIError struct {
|
|
Error string `json:"error"`
|
|
Details string `json:"details,omitempty"`
|
|
}
|
|
|
|
// RespondSuccess sendet 200 OK mit Daten
|
|
func RespondSuccess(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, APIResponse{
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// RespondMessage sendet 200 OK mit einer Nachricht
|
|
func RespondMessage(c *gin.Context, message string) {
|
|
c.JSON(http.StatusOK, APIResponse{
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// RespondCreated sendet 201 Created mit Daten
|
|
func RespondCreated(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusCreated, APIResponse{
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// RespondError sendet einen Fehler mit Statuscode und Nachricht
|
|
func RespondError(c *gin.Context, code int, message string, details ...string) {
|
|
errResp := APIError{Error: message}
|
|
if len(details) > 0 {
|
|
errResp.Details = details[0]
|
|
}
|
|
c.AbortWithStatusJSON(code, errResp)
|
|
}
|