This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using controller response struct pattern
- Loading branch information
Showing
5 changed files
with
67 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/labstack/echo" | ||
"go.uber.org/dig" | ||
) | ||
|
||
// BookControllerResponse is response of BookController | ||
type BookControllerResponse struct { | ||
dig.In | ||
} | ||
|
||
func (BookControllerResponse) invalidMessage(ctx echo.Context, err error) error { | ||
return ctx.JSON(http.StatusBadRequest, GeneralResponse{ | ||
Message: "Invalid Message", | ||
}) | ||
} | ||
|
||
func (BookControllerResponse) invalidID(ctx echo.Context, err error) error { | ||
return ctx.JSON(http.StatusBadRequest, GeneralResponse{ | ||
Message: "Invalid ID", | ||
}) | ||
} | ||
|
||
func (BookControllerResponse) insertSuccess(ctx echo.Context, lastInsertID int64) error { | ||
return ctx.JSON(http.StatusCreated, GeneralResponse{ | ||
Message: fmt.Sprintf("Success insert new record #%d", lastInsertID), | ||
}) | ||
} | ||
|
||
func (BookControllerResponse) bookNotFound(ctx echo.Context, id int64) error { | ||
return ctx.JSON(http.StatusNotFound, GeneralResponse{ | ||
Message: fmt.Sprintf("book #%d not found", id), | ||
}) | ||
} | ||
|
||
func (BookControllerResponse) bookDeleted(ctx echo.Context, id int64) error { | ||
return ctx.JSON(http.StatusOK, GeneralResponse{ | ||
Message: fmt.Sprintf("Delete #%d done", id), | ||
}) | ||
} | ||
|
||
func (BookControllerResponse) bookUpdated(ctx echo.Context, id int64) error { | ||
return ctx.JSON(http.StatusOK, GeneralResponse{ | ||
Message: fmt.Sprintf("Update #%d success", id), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package controller | ||
|
||
// GeneralResponse is general response | ||
type GeneralResponse struct { | ||
Message string `json:"message"` | ||
} |
This file was deleted.
Oops, something went wrong.