Skip to content

Commit

Permalink
chore: Fix typo and a few maintenance stuff (bxcodec#15)
Browse files Browse the repository at this point in the history
* fix typo
* change logic in usecase to get author details
* fix some test and change code styling
  • Loading branch information
bxcodec authored Nov 25, 2018
1 parent f7eab0b commit ec0848d
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 351 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
branch = "master"
name = "golang.org/x/sync"
47 changes: 24 additions & 23 deletions article/delivery/http/article_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@ import (

"github.com/sirupsen/logrus"

models "github.com/bxcodec/go-clean-arch/models"
"github.com/bxcodec/go-clean-arch/models"

articleUcase "github.com/bxcodec/go-clean-arch/article"
"github.com/bxcodec/go-clean-arch/article"
"github.com/labstack/echo"

validator "gopkg.in/go-playground/validator.v9"
)

// ResponseError represent the reseponse error struct
type ResponseError struct {
Message string `json:"message"`
}

// HttpArticleHandler represent the httphandler for article
type HttpArticleHandler struct {
AUsecase articleUcase.ArticleUsecase
AUsecase article.Usecase
}

func NewArticleHttpHandler(e *echo.Echo, us article.Usecase) {
handler := &HttpArticleHandler{
AUsecase: us,
}
e.GET("/articles", handler.FetchArticle)
e.POST("/articles", handler.Store)
e.GET("/articles/:id", handler.GetByID)
e.DELETE("/articles/:id", handler.Delete)

}

func (a *HttpArticleHandler) FetchArticle(c echo.Context) error {
Expand Down Expand Up @@ -84,13 +98,14 @@ func (a *HttpArticleHandler) Store(c echo.Context) error {
ctx = context.Background()
}

ar, err := a.AUsecase.Store(ctx, &article)
err = a.AUsecase.Store(ctx, &article)

if err != nil {
return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
}
return c.JSON(http.StatusCreated, ar)
return c.JSON(http.StatusCreated, article)
}

func (a *HttpArticleHandler) Delete(c echo.Context) error {
idP, err := strconv.Atoi(c.Param("id"))
id := int64(idP)
Expand All @@ -99,10 +114,9 @@ func (a *HttpArticleHandler) Delete(c echo.Context) error {
ctx = context.Background()
}

_, err = a.AUsecase.Delete(ctx, id)
err = a.AUsecase.Delete(ctx, id)

if err != nil {

return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
}
return c.NoContent(http.StatusNoContent)
Expand All @@ -113,28 +127,15 @@ func getStatusCode(err error) int {
if err == nil {
return http.StatusOK
}

logrus.Error(err)
switch err {
case models.INTERNAL_SERVER_ERROR:

case models.ErrInternalServerError:
return http.StatusInternalServerError
case models.NOT_FOUND_ERROR:
case models.ErrNotFound:
return http.StatusNotFound
case models.CONFLICT_ERROR:
case models.ErrConflict:
return http.StatusConflict
default:
return http.StatusInternalServerError
}
}

func NewArticleHttpHandler(e *echo.Echo, us articleUcase.ArticleUsecase) {
handler := &HttpArticleHandler{
AUsecase: us,
}
e.GET("/article", handler.FetchArticle)
e.POST("/article", handler.Store)
e.GET("/article/:id", handler.GetByID)
e.DELETE("/article/:id", handler.Delete)

}
18 changes: 9 additions & 9 deletions article/delivery/http/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

articleHttp "github.com/bxcodec/go-clean-arch/article/delivery/http"
"github.com/bxcodec/go-clean-arch/article/mocks"
models "github.com/bxcodec/go-clean-arch/models"
"github.com/bxcodec/go-clean-arch/models"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -23,7 +23,7 @@ func TestFetch(t *testing.T) {
var mockArticle models.Article
err := faker.FakeData(&mockArticle)
assert.NoError(t, err)
mockUCase := new(mocks.ArticleUsecase)
mockUCase := new(mocks.Usecase)
mockListArticle := make([]*models.Article, 0)
mockListArticle = append(mockListArticle, &mockArticle)
num := 1
Expand All @@ -49,10 +49,10 @@ func TestFetch(t *testing.T) {
}

func TestFetchError(t *testing.T) {
mockUCase := new(mocks.ArticleUsecase)
mockUCase := new(mocks.Usecase)
num := 1
cursor := "2"
mockUCase.On("Fetch", mock.Anything, cursor, int64(num)).Return(nil, "", models.INTERNAL_SERVER_ERROR)
mockUCase.On("Fetch", mock.Anything, cursor, int64(num)).Return(nil, "", models.ErrInternalServerError)

e := echo.New()
req, err := http.NewRequest(echo.GET, "/article?num=1&cursor="+cursor, strings.NewReader(""))
Expand All @@ -77,7 +77,7 @@ func TestGetByID(t *testing.T) {
err := faker.FakeData(&mockArticle)
assert.NoError(t, err)

mockUCase := new(mocks.ArticleUsecase)
mockUCase := new(mocks.Usecase)

num := int(mockArticle.ID)

Expand Down Expand Up @@ -111,12 +111,12 @@ func TestStore(t *testing.T) {

tempMockArticle := mockArticle
tempMockArticle.ID = 0
mockUCase := new(mocks.ArticleUsecase)
mockUCase := new(mocks.Usecase)

j, err := json.Marshal(tempMockArticle)
assert.NoError(t, err)

mockUCase.On("Store", mock.Anything, mock.AnythingOfType("*models.Article")).Return(&mockArticle, nil)
mockUCase.On("Store", mock.Anything, mock.AnythingOfType("*models.Article")).Return(nil)

e := echo.New()
req, err := http.NewRequest(echo.POST, "/article", strings.NewReader(string(j)))
Expand All @@ -141,11 +141,11 @@ func TestDelete(t *testing.T) {
err := faker.FakeData(&mockArticle)
assert.NoError(t, err)

mockUCase := new(mocks.ArticleUsecase)
mockUCase := new(mocks.Usecase)

num := int(mockArticle.ID)

mockUCase.On("Delete", mock.Anything, int64(num)).Return(true, nil)
mockUCase.On("Delete", mock.Anything, int64(num)).Return(nil)

e := echo.New()
req, err := http.NewRequest(echo.DELETE, "/article/"+strconv.Itoa(int(num)), strings.NewReader(""))
Expand Down
86 changes: 35 additions & 51 deletions article/mocks/ArticleRepository.go → article/mocks/Repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ec0848d

Please sign in to comment.