Updated some responses to return the base struct with embedding

This commit is contained in:
James Tombleson 2024-04-21 08:59:07 -07:00
parent 5b2ff607b9
commit e218da5e7d
2 changed files with 29 additions and 15 deletions

View File

@ -1,19 +1,30 @@
package domain package domain
type LoginResponse struct { type LoginResponse struct {
Success bool `json:"success"` ErrorResponse
Token string `json:"token"` Token string `json:"token"`
Type string `json:"type"` Type string `json:"type"`
RefreshToken string `json:"refreshToken"` RefreshToken string `json:"refreshToken"`
} }
// This /
// /auth/refreshToken
type TokenRefreshResponse struct {
ErrorResponse
}
type BaseResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
type ErrorResponse struct { type ErrorResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
Message string `json:"message"` Message string `json:"message"`
} }
type HelloWhoResponse struct { type HelloWhoResponse struct {
Success bool `json:"success"` BaseResponse
Error string `json:"error"` Error string `json:"error"`
Message string `json:"message"`
} }

View File

@ -11,16 +11,20 @@ import (
func (h *Handler) DemoHello(c echo.Context) error { func (h *Handler) DemoHello(c echo.Context) error {
return c.JSON(http.StatusOK, domain.HelloWhoResponse{ return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true, BaseResponse: domain.BaseResponse{
Message: "Hello world!", Success: true,
Message: "Hello world!",
},
}) })
} }
func (h *Handler) HelloWho(c echo.Context) error { func (h *Handler) HelloWho(c echo.Context) error {
name := c.Param("who") name := c.Param("who")
return c.JSON(http.StatusOK, domain.HelloWhoResponse{ return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true, BaseResponse: domain.BaseResponse{
Message: fmt.Sprintf("Hello, %s", name), Success: true,
Message: fmt.Sprintf("Hello, %s", name),
},
}) })
} }
@ -28,15 +32,14 @@ func (h *Handler) HelloBody(c echo.Context) error {
request := domain.HelloBodyRequest{} request := domain.HelloBodyRequest{}
err := (&echo.DefaultBinder{}).BindBody(c, &request) err := (&echo.DefaultBinder{}).BindBody(c, &request)
if err != nil { if err != nil {
return c.JSON(http.StatusBadRequest, domain.HelloWhoResponse{ return h.InternalServerErrorResponse(c, err.Error())
Success: false,
Error: err.Error(),
})
} }
return c.JSON(http.StatusOK, domain.HelloWhoResponse{ return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true, BaseResponse: domain.BaseResponse{
Message: fmt.Sprintf("Hello, %s", request.Name), Success: true,
Message: fmt.Sprintf("Hello, %s", request.Name),
},
}) })
} }