diff --git a/internal/http/produce.go b/internal/http/produce.go index 4885e18..f73ba53 100644 --- a/internal/http/produce.go +++ b/internal/http/produce.go @@ -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 +} diff --git a/internal/http/produce_test.go b/internal/http/produce_test.go index a147fb7..033b349 100644 --- a/internal/http/produce_test.go +++ b/internal/http/produce_test.go @@ -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) + }) + } +}