feat:add context interface, and change core.writeresponse function to use it
This commit is contained in:
		
							
								
								
									
										5
									
								
								internal/pkg/core/context.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								internal/pkg/core/context.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | |||||||
|  | package core | ||||||
|  |  | ||||||
|  | type Context interface { | ||||||
|  | 	JSON(code int, obj any) | ||||||
|  | } | ||||||
| @ -26,7 +26,6 @@ import ( | |||||||
| 	"net/http" | 	"net/http" | ||||||
|  |  | ||||||
| 	"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno" | 	"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno" | ||||||
| 	"github.com/gin-gonic/gin" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type ErrResponse struct { | type ErrResponse struct { | ||||||
| @ -36,7 +35,7 @@ type ErrResponse struct { | |||||||
|  |  | ||||||
| // WriteResponse writes the response to the HTTP response writer with HTTP code | // WriteResponse writes the response to the HTTP response writer with HTTP code | ||||||
| // and potential errors. | // and potential errors. | ||||||
| func WriteResponse(c *gin.Context, err error, data any) { | func WriteResponse(c Context, err error, data any) { | ||||||
| 	// No error, write json response directly | 	// No error, write json response directly | ||||||
| 	if err == nil { | 	if err == nil { | ||||||
| 		c.JSON(http.StatusOK, data) | 		c.JSON(http.StatusOK, data) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user