Merge pull request #14 from davidlick/get-all-produce
Add GET /produce endpoint
This commit was merged in pull request #14.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
func (s *server) produceGroup(r chi.Router) {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Route("/produce", func(r chi.Router) {
|
||||
r.Get("/", s.handleGetAllProduce)
|
||||
r.Post("/", s.handleAddProduce)
|
||||
})
|
||||
})
|
||||
@@ -42,3 +43,16 @@ func (s *server) handleAddProduce(w http.ResponseWriter, r *http.Request) {
|
||||
s.writeSuccess(ctx, w, nil, http.StatusCreated)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *server) handleGetAllProduce(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
items, err := s.produceSvc.All()
|
||||
if err != nil {
|
||||
s.writeError(ctx, w, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
s.writeSuccess(ctx, w, items, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -81,3 +81,64 @@ func TestServer_handleAddProduce(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_handleGetAllProduce(t *testing.T) {
|
||||
tests := []struct {
|
||||
test string
|
||||
expectFunc func(mockProduceSvc *MockProduceService)
|
||||
assertFunc func(t *testing.T, w *httptest.ResponseRecorder)
|
||||
}{
|
||||
{
|
||||
test: "it should successfully get all produce",
|
||||
expectFunc: func(mockProduceSvc *MockProduceService) {
|
||||
mockProduceSvc.EXPECT().All().Return([]produce.Item{
|
||||
{Code: "code-1", Name: "name-1", Price: money.New(101, "USD")},
|
||||
{Code: "code-2", Name: "name-2", Price: money.New(202, "USD")},
|
||||
{Code: "code-3", Name: "name-3", Price: money.New(303, "USD")},
|
||||
}, nil)
|
||||
},
|
||||
assertFunc: func(t *testing.T, w *httptest.ResponseRecorder) {
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
b, err := ioutil.ReadAll(w.Body)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "[{\"code\":\"code-1\",\"name\":\"name-1\",\"price\":{\"amount\":101,\"currency\":\"USD\"}},{\"code\":\"code-2\",\"name\":\"name-2\",\"price\":{\"amount\":202,\"currency\":\"USD\"}},{\"code\":\"code-3\",\"name\":\"name-3\",\"price\":{\"amount\":303,\"currency\":\"USD\"}}]\n", string(b))
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "it should respond internal server error if adding to service fails",
|
||||
expectFunc: func(mockProduceSvc *MockProduceService) {
|
||||
mockProduceSvc.EXPECT().All().Return(nil, errors.New("test error"))
|
||||
},
|
||||
assertFunc: func(t *testing.T, w *httptest.ResponseRecorder) {
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.test, func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/v1/produce", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
noopLogger := logrus.New()
|
||||
noopLogger.SetOutput(ioutil.Discard)
|
||||
|
||||
mockProduceSvc := NewMockProduceService(ctrl)
|
||||
tc.expectFunc(mockProduceSvc)
|
||||
|
||||
s := NewServer(3000, noopLogger, "test", mockProduceSvc)
|
||||
|
||||
handler := http.HandlerFunc(s.handleGetAllProduce)
|
||||
handler.ServeHTTP(w, r)
|
||||
|
||||
tc.assertFunc(t, w)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user