Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
using controller response struct pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Sep 18, 2019
1 parent 99dabe8 commit bd1cd40
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 46 deletions.
20 changes: 10 additions & 10 deletions app/controller/book_controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"fmt"
"net/http"
"strconv"

Expand All @@ -16,6 +15,7 @@ import (
// BookController is controller to book entity
type BookController struct {
dig.In
BookControllerResponse
service.BookService
}

Expand All @@ -37,14 +37,14 @@ func (c *BookController) Create(ctx echo.Context) (err error) {
}
err = validator.New().Struct(book)
if err != nil {
return invalidMessage(ctx, err)
return c.invalidMessage(ctx, err)
}
ctx0 := ctx.Request().Context()
result, err := c.BookService.Insert(ctx0, book)
if err != nil {
return err
}
return insertSuccess(ctx, result)
return c.insertSuccess(ctx, result)
}

// List of book
Expand All @@ -61,15 +61,15 @@ func (c *BookController) List(ctx echo.Context) error {
func (c *BookController) Get(ctx echo.Context) error {
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
return invalidID(ctx, err)
return c.invalidID(ctx, err)
}
ctx0 := ctx.Request().Context()
book, err := c.BookService.Find(ctx0, id)
if err != nil {
return err
}
if book == nil {
return ctx.JSON(http.StatusNotFound, map[string]string{"message": fmt.Sprintf("book #%d not found", id)})
return c.bookNotFound(ctx, id)
}
return ctx.JSON(http.StatusOK, book)
}
Expand All @@ -78,14 +78,14 @@ func (c *BookController) Get(ctx echo.Context) error {
func (c *BookController) Delete(ctx echo.Context) error {
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
return invalidID(ctx, err)
return c.invalidID(ctx, err)
}
ctx0 := ctx.Request().Context()
err = c.BookService.Delete(ctx0, id)
if err != nil {
return err
}
return ctx.JSON(http.StatusOK, map[string]string{"message": fmt.Sprintf("Delete #%d done", id)})
return c.bookDeleted(ctx, id)
}

// Update book
Expand All @@ -96,16 +96,16 @@ func (c *BookController) Update(ctx echo.Context) (err error) {
return err
}
if book.ID <= 0 {
return invalidID(ctx, err)
return c.invalidID(ctx, err)
}
err = validator.New().Struct(book)
if err != nil {
return invalidMessage(ctx, err)
return c.invalidMessage(ctx, err)
}
ctx0 := ctx.Request().Context()
err = c.BookService.Update(ctx0, book)
if err != nil {
return err
}
return ctx.JSON(http.StatusOK, map[string]string{"message": "Update success"})
return c.bookUpdated(ctx, book.ID)
}
50 changes: 50 additions & 0 deletions app/controller/book_controller_response.go
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),
})
}
2 changes: 1 addition & 1 deletion app/controller/book_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ func TestBookController_Update(t *testing.T) {
rr, err := echokit.DoPUT(bookCntrl.Update, "/", `{"id": 1, "author":"some-author", "title":"some-title"}`)
require.NoError(t, err)
require.Equal(t, http.StatusOK, rr.Code)
require.Equal(t, "{\"message\":\"Update success\"}\n", rr.Body.String())
require.Equal(t, "{\"message\":\"Update #1 success\"}\n", rr.Body.String())
})
}
6 changes: 6 additions & 0 deletions app/controller/general_response.go
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"`
}
35 changes: 0 additions & 35 deletions app/controller/response.go

This file was deleted.

0 comments on commit bd1cd40

Please sign in to comment.