@@ -16,29 +16,57 @@ import (
1616 "github.com/gin-gonic/gin"
1717)
1818
19- func GenericResponse (c * gin.Context , statusCode int , message string , result interface {}) {
20- c .JSON (statusCode , models.APIResponse {
21- Message : message ,
22- Result : result ,
23- })
19+ // OK sends a 200 response with result payload.
20+ func OK [T any ](c * gin.Context , message string , result T ) {
21+ c .JSON (http .StatusOK , models.APIResponse [T ]{Success : true , Message : message , Result : result })
2422}
2523
26- func InternalServerErrorResponse (c * gin.Context , message string , result interface {}) {
27- GenericResponse (c , http .StatusInternalServerError , message , result )
24+ // OKMessage sends a 200 response with a message only (no result payload).
25+ func OKMessage (c * gin.Context , message string ) {
26+ c .JSON (http .StatusOK , models.APIResponse [any ]{Success : true , Message : message })
2827}
2928
30- func OKResponse (c * gin.Context , message string , result interface {}) {
31- GenericResponse (c , http .StatusOK , message , result )
29+ // Created sends a 201 response with result payload.
30+ func Created [T any ](c * gin.Context , message string , result T ) {
31+ c .JSON (http .StatusCreated , models.APIResponse [T ]{Success : true , Message : message , Result : result })
3232}
3333
34- func NotFoundResponse (c * gin.Context , message string , result interface {}) {
35- GenericResponse (c , http .StatusNotFound , message , result )
34+ // ErrorResponse sends an error response with the given status code.
35+ func ErrorResponse (c * gin.Context , status int , message string ) {
36+ c .JSON (status , models.APIResponse [any ]{Success : false , Message : message })
3637}
3738
38- func BadRequestResponse (c * gin.Context , message string , result interface {}) {
39- GenericResponse (c , http .StatusBadRequest , message , result )
39+ // BadRequest sends a 400 error response.
40+ func BadRequest (c * gin.Context , message string ) {
41+ ErrorResponse (c , http .StatusBadRequest , message )
4042}
4143
42- func ForbiddenResponse (c * gin.Context , message string , result interface {}) {
43- GenericResponse (c , http .StatusForbidden , message , result )
44+ // Unauthorized sends a 401 error response.
45+ func Unauthorized (c * gin.Context , message string ) {
46+ ErrorResponse (c , http .StatusUnauthorized , message )
47+ }
48+
49+ // Forbidden sends a 403 error response.
50+ func Forbidden (c * gin.Context , message string ) {
51+ ErrorResponse (c , http .StatusForbidden , message )
52+ }
53+
54+ // NotFound sends a 404 error response.
55+ func NotFound (c * gin.Context , message string ) {
56+ ErrorResponse (c , http .StatusNotFound , message )
57+ }
58+
59+ // Conflict sends a 409 error response.
60+ func Conflict (c * gin.Context , message string ) {
61+ ErrorResponse (c , http .StatusConflict , message )
62+ }
63+
64+ // InternalError sends a 500 error response.
65+ func InternalError (c * gin.Context , message string ) {
66+ ErrorResponse (c , http .StatusInternalServerError , message )
67+ }
68+
69+ // AbortUnauthorized aborts the request chain and sends a 401 response.
70+ func AbortUnauthorized (c * gin.Context , message string ) {
71+ c .AbortWithStatusJSON (http .StatusUnauthorized , models.APIResponse [any ]{Success : false , Message : message })
4472}
0 commit comments