Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context #9

Merged
merged 1 commit into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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