Files
warehouse-management-BE/response/success_response.go
Tran Anh Tuan 6a4a96e0ca Base Project
2026-05-08 14:32:24 +07:00

51 lines
1.5 KiB
Go

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)
}