Skip to content

Commit

Permalink
chore: apply linter and clean dirty codes (#22)
Browse files Browse the repository at this point in the history
* chore: add linter and clean the dirty code

* chore: re-order the imports
  • Loading branch information
bxcodec authored Apr 30, 2019
1 parent b1e3031 commit ffdaa8a
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 114 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ article_clean
_*
*.test
.DS_Store
engine
engine
bin/
22 changes: 16 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ vendor:
@dep ensure -v

engine: vendor
go build -o ${BINARY}

install:
go build -o ${BINARY}
go build -o ${BINARY} app/*.go

unittest:
go test -short $$(go list ./... | grep -v /vendor/)
go test -short ./...

clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
Expand All @@ -26,4 +23,17 @@ run:
stop:
docker-compose down

.PHONY: clean install unittest build docker run stop vendor
lint-prepare:
@echo "Installing golangci-lint"
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s latest

lint:
./bin/golangci-lint run \
--exclude-use-default=false \
--enable=golint \
--enable=gocyclo \
--enable=goconst \
--enable=unconvert \
./...

.PHONY: clean install unittest build docker run stop vendor lint-prepare lint
46 changes: 24 additions & 22 deletions article/delivery/http/article_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,37 @@ import (
"net/http"
"strconv"

"github.com/labstack/echo"
"github.com/sirupsen/logrus"

"github.com/bxcodec/go-clean-arch/models"
validator "gopkg.in/go-playground/validator.v9"

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

validator "gopkg.in/go-playground/validator.v9"
"github.com/bxcodec/go-clean-arch/models"
)

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

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

func NewArticleHttpHandler(e *echo.Echo, us article.Usecase) {
handler := &HttpArticleHandler{
// NewArticleHandler will initialize the articles/ resources endpoint
func NewArticleHandler(e *echo.Echo, us article.Usecase) {
handler := &ArticleHandler{
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 {

// FetchArticle will fetch the article based on given params
func (a *ArticleHandler) FetchArticle(c echo.Context) error {
numS := c.QueryParam("num")
num, _ := strconv.Atoi(numS)
cursor := c.QueryParam("cursor")
Expand All @@ -54,36 +52,37 @@ func (a *HttpArticleHandler) FetchArticle(c echo.Context) error {
return c.JSON(http.StatusOK, listAr)
}

func (a *HttpArticleHandler) GetByID(c echo.Context) error {

// GetByID will get article by given id
func (a *ArticleHandler) GetByID(c echo.Context) error {
idP, err := strconv.Atoi(c.Param("id"))
id := int64(idP)
if err != nil {
return c.JSON(http.StatusNotFound, models.ErrNotFound.Error())
}

id := int64(idP)
ctx := c.Request().Context()
if ctx == nil {
ctx = context.Background()
}

art, err := a.AUsecase.GetByID(ctx, id)

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

func isRequestValid(m *models.Article) (bool, error) {

validate := validator.New()

err := validate.Struct(m)
if err != nil {
return false, err
}
return true, nil
}

func (a *HttpArticleHandler) Store(c echo.Context) error {
// Store will store the article by given request body
func (a *ArticleHandler) Store(c echo.Context) error {
var article models.Article
err := c.Bind(&article)
if err != nil {
Expand All @@ -106,24 +105,27 @@ func (a *HttpArticleHandler) Store(c echo.Context) error {
return c.JSON(http.StatusCreated, article)
}

func (a *HttpArticleHandler) Delete(c echo.Context) error {
// Delete will delete article by given param
func (a *ArticleHandler) Delete(c echo.Context) error {
idP, err := strconv.Atoi(c.Param("id"))
if err != nil {
return c.JSON(http.StatusNotFound, models.ErrNotFound.Error())
}
id := int64(idP)
ctx := c.Request().Context()
if ctx == nil {
ctx = context.Background()
}

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

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

return c.NoContent(http.StatusNoContent)
}

func getStatusCode(err error) int {

if err == nil {
return http.StatusOK
}
Expand Down
40 changes: 22 additions & 18 deletions article/delivery/http/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"testing"
"time"

articleHttp "github.com/bxcodec/go-clean-arch/article/delivery/http"
"github.com/bxcodec/go-clean-arch/article/mocks"
"github.com/bxcodec/go-clean-arch/models"
"github.com/bxcodec/faker"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/bxcodec/faker"
articleHttp "github.com/bxcodec/go-clean-arch/article/delivery/http"
"github.com/bxcodec/go-clean-arch/article/mocks"
"github.com/bxcodec/go-clean-arch/models"
)

func TestFetch(t *testing.T) {
Expand All @@ -36,14 +37,14 @@ func TestFetch(t *testing.T) {

rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
handler := articleHttp.HttpArticleHandler{
handler := articleHttp.ArticleHandler{
AUsecase: mockUCase,
}
handler.FetchArticle(c)
err = handler.FetchArticle(c)
require.NoError(t, err)

responseCursor := rec.Header().Get("X-Cursor")
assert.Equal(t, "10", responseCursor)

assert.Equal(t, http.StatusOK, rec.Code)
mockUCase.AssertExpectations(t)
}
Expand All @@ -60,14 +61,14 @@ func TestFetchError(t *testing.T) {

rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
handler := articleHttp.HttpArticleHandler{
handler := articleHttp.ArticleHandler{
AUsecase: mockUCase,
}
handler.FetchArticle(c)
err = handler.FetchArticle(c)
require.NoError(t, err)

responseCursor := rec.Header().Get("X-Cursor")
assert.Equal(t, "", responseCursor)

assert.Equal(t, http.StatusInternalServerError, rec.Code)
mockUCase.AssertExpectations(t)
}
Expand All @@ -84,18 +85,19 @@ func TestGetByID(t *testing.T) {
mockUCase.On("GetByID", mock.Anything, int64(num)).Return(&mockArticle, nil)

e := echo.New()
req, err := http.NewRequest(echo.GET, "/article/"+strconv.Itoa(int(num)), strings.NewReader(""))
req, err := http.NewRequest(echo.GET, "/article/"+strconv.Itoa(num), strings.NewReader(""))
assert.NoError(t, err)

rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("article/:id")
c.SetParamNames("id")
c.SetParamValues(strconv.Itoa(num))
handler := articleHttp.HttpArticleHandler{
handler := articleHttp.ArticleHandler{
AUsecase: mockUCase,
}
handler.GetByID(c)
err = handler.GetByID(c)
require.NoError(t, err)

assert.Equal(t, http.StatusOK, rec.Code)
mockUCase.AssertExpectations(t)
Expand Down Expand Up @@ -127,10 +129,11 @@ func TestStore(t *testing.T) {
c := e.NewContext(req, rec)
c.SetPath("/article")

handler := articleHttp.HttpArticleHandler{
handler := articleHttp.ArticleHandler{
AUsecase: mockUCase,
}
handler.Store(c)
err = handler.Store(c)
require.NoError(t, err)

assert.Equal(t, http.StatusCreated, rec.Code)
mockUCase.AssertExpectations(t)
Expand All @@ -148,18 +151,19 @@ func TestDelete(t *testing.T) {
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(""))
req, err := http.NewRequest(echo.DELETE, "/article/"+strconv.Itoa(num), strings.NewReader(""))
assert.NoError(t, err)

rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("article/:id")
c.SetParamNames("id")
c.SetParamValues(strconv.Itoa(num))
handler := articleHttp.HttpArticleHandler{
handler := articleHttp.ArticleHandler{
AUsecase: mockUCase,
}
handler.Delete(c)
err = handler.Delete(c)
require.NoError(t, err)

assert.Equal(t, http.StatusNoContent, rec.Code)
mockUCase.AssertExpectations(t)
Expand Down
Loading

0 comments on commit ffdaa8a

Please sign in to comment.