Base Project
This commit is contained in:
50
response/success_response.go
Normal file
50
response/success_response.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SuccessResponse represents a structured success response
|
||||
type SuccessResponse struct {
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"`
|
||||
ReasonStatusCode string `json:"reason_status_code"`
|
||||
Option any `json:"option,omitempty"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// NewSuccessResponse creates a new SuccessResponse
|
||||
func NewSuccessResponse(message string, statusCode int, reasonStatusCode string, option any, data any) *SuccessResponse {
|
||||
return &SuccessResponse{
|
||||
Message: message,
|
||||
Status: statusCode,
|
||||
ReasonStatusCode: reasonStatusCode,
|
||||
Option: option,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// Send sends the success response to the client
|
||||
func (sr *SuccessResponse) Send(c *gin.Context) {
|
||||
c.JSON(sr.Status, sr)
|
||||
}
|
||||
|
||||
// Ok represents a 200 OK success response
|
||||
func Ok(c *gin.Context, message string, metadata any) {
|
||||
if message == "" {
|
||||
message = GetReasonPhrase(http.StatusOK)
|
||||
}
|
||||
response := NewSuccessResponse(message, http.StatusOK, GetReasonPhrase(http.StatusOK), nil, metadata)
|
||||
response.Send(c)
|
||||
}
|
||||
|
||||
// Created represents a 201 Created success response
|
||||
func Created(c *gin.Context, message string, metadata any) {
|
||||
if message == "" {
|
||||
message = GetReasonPhrase(http.StatusCreated)
|
||||
}
|
||||
response := NewSuccessResponse(message, http.StatusCreated, GetReasonPhrase(http.StatusCreated), nil, metadata)
|
||||
response.Send(c)
|
||||
}
|
||||
Reference in New Issue
Block a user