Skip to content

Commit

Permalink
feat: add context (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxcodec authored May 9, 2018
1 parent 7e2d3a2 commit e75a240
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 227 deletions.
40 changes: 29 additions & 11 deletions article/delivery/http/article_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"net/http"
"strconv"

Expand All @@ -14,6 +15,9 @@ import (
validator "gopkg.in/go-playground/validator.v9"
)

type ResponseError struct {
Message string `json:"message"`
}
type HttpArticleHandler struct {
AUsecase articleUcase.ArticleUsecase
}
Expand All @@ -22,13 +26,15 @@ func (a *HttpArticleHandler) FetchArticle(c echo.Context) error {

numS := c.QueryParam("num")
num, _ := strconv.Atoi(numS)

cursor := c.QueryParam("cursor")

listAr, nextCursor, err := a.AUsecase.Fetch(cursor, int64(num))
ctx := c.Request().Context()
if ctx == nil {
ctx = context.Background()
}
listAr, nextCursor, err := a.AUsecase.Fetch(ctx, cursor, int64(num))

if err != nil {
return c.JSON(getStatusCode(err), err.Error())
return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
}
c.Response().Header().Set(`X-Cursor`, nextCursor)
return c.JSON(http.StatusOK, listAr)
Expand All @@ -39,10 +45,15 @@ func (a *HttpArticleHandler) GetByID(c echo.Context) error {
idP, err := strconv.Atoi(c.Param("id"))
id := int64(idP)

art, err := a.AUsecase.GetByID(id)
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), err.Error())
return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
}
return c.JSON(http.StatusOK, art)
}
Expand All @@ -68,23 +79,31 @@ func (a *HttpArticleHandler) Store(c echo.Context) error {
if ok, err := isRequestValid(&article); !ok {
return c.JSON(http.StatusBadRequest, err.Error())
}
ctx := c.Request().Context()
if ctx == nil {
ctx = context.Background()
}

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

if err != nil {
return c.JSON(getStatusCode(err), err.Error())
return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
}
return c.JSON(http.StatusCreated, ar)
}
func (a *HttpArticleHandler) Delete(c echo.Context) error {
idP, err := strconv.Atoi(c.Param("id"))
id := int64(idP)
ctx := c.Request().Context()
if ctx == nil {
ctx = context.Background()
}

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

if err != nil {

return c.JSON(getStatusCode(err), err.Error())
return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
}
return c.NoContent(http.StatusNoContent)
}
Expand Down Expand Up @@ -113,7 +132,6 @@ 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)
Expand Down
10 changes: 5 additions & 5 deletions article/delivery/http/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestFetch(t *testing.T) {
mockListArticle = append(mockListArticle, &mockArticle)
num := 1
cursor := "2"
mockUCase.On("Fetch", cursor, int64(num)).Return(mockListArticle, "10", nil)
mockUCase.On("Fetch", mock.Anything, cursor, int64(num)).Return(mockListArticle, "10", nil)

e := echo.New()
req, err := http.NewRequest(echo.GET, "/article?num=1&cursor="+cursor, strings.NewReader(""))
Expand All @@ -52,7 +52,7 @@ func TestFetchError(t *testing.T) {
mockUCase := new(mocks.ArticleUsecase)
num := 1
cursor := "2"
mockUCase.On("Fetch", cursor, int64(num)).Return(nil, "", models.INTERNAL_SERVER_ERROR)
mockUCase.On("Fetch", mock.Anything, cursor, int64(num)).Return(nil, "", models.INTERNAL_SERVER_ERROR)

e := echo.New()
req, err := http.NewRequest(echo.GET, "/article?num=1&cursor="+cursor, strings.NewReader(""))
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestGetByID(t *testing.T) {

num := int(mockArticle.ID)

mockUCase.On("GetByID", int64(num)).Return(&mockArticle, nil)
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(""))
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestStore(t *testing.T) {
j, err := json.Marshal(tempMockArticle)
assert.NoError(t, err)

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

e := echo.New()
req, err := http.NewRequest(echo.POST, "/article", strings.NewReader(string(j)))
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestDelete(t *testing.T) {

num := int(mockArticle.ID)

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

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

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

Loading

2 comments on commit e75a240

@mylifeafterthat
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi iman, what a reason using context?

@bxcodec
Copy link
Owner Author

@bxcodec bxcodec commented on e75a240 May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
I've told the reason here #10
To simplify, context will helpful when we're working in concurrent and have many go routine calls project.

Please sign in to comment.