Files
base/internal/dto/response.go
2026-04-10 18:25:21 +03:30

108 lines
1.9 KiB
Go

package dto
import "net/http"
// SuccessResponse represents a successful response for setting stock.
type SuccessResponse struct {
Message string `json:"message"`
Status int `json:"status" example:"200"`
}
// ErrorResponse represents a generic error response.
type ErrorResponse struct {
Message string `json:"message"`
Status int `json:"status" example:"400"`
}
type Response struct {
Message string `json:"message"`
Status int `json:"status"`
Data any `json:"data,omitempty"`
}
func OK() Response {
return Response{
Message: "OK",
Status: http.StatusOK,
}
}
func Created(data any) Response {
return Response{
Message: "Created",
Status: http.StatusCreated,
Data: data,
}
}
func BadRequest() Response {
return Response{
Message: "bad request",
Status: http.StatusBadRequest,
}
}
func NotFound() Response {
return Response{
Message: "not found",
Status: http.StatusNotFound,
}
}
func InternalServerError() Response {
return Response{
Message: "internal server error",
Status: http.StatusInternalServerError,
}
}
func UnprocessableEntity() Response {
return Response{
Message: "unprocessable entity",
Status: http.StatusUnprocessableEntity,
}
}
func UnprocessableEntityException() Response {
return Response{
Message: "unprocessable entity exception",
Status: http.StatusUnprocessableEntity,
}
}
func Forbidden() Response {
return Response{
Message: "forbidden",
Status: http.StatusForbidden,
}
}
func Unauthorized() Response {
return Response{
Message: "unauthorized",
Status: http.StatusUnauthorized,
}
}
func Conflict() Response {
return Response{
Message: "conflict",
Status: http.StatusConflict,
}
}
func (r Response) WithMessage(msg string) Response {
r.Message = msg
return r
}
func (r Response) WithStatus(status int) Response {
r.Status = status
return r
}
func (r Response) WithData(data any) Response {
r.Data = data
return r
}