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
type LoginResponse struct {
Success bool `json:"success"`
Token string `json:"token"`
Type string `json:"type"`
ErrorResponse
Token string `json:"token"`
Type string `json:"type"`
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 {
Success bool `json:"success"`
Message string `json:"message"`
}
type HelloWhoResponse struct {
Success bool `json:"success"`
BaseResponse
Error string `json:"error"`
Message string `json:"message"`
}

View File

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