Merge pull request #15 from davidlick/add-delete-produce
Add DELETE /produce/{produceCode} endpoint
This commit was merged in pull request #15.
This commit is contained in:
@@ -14,6 +14,9 @@ func (s *server) produceGroup(r chi.Router) {
|
||||
r.Route("/produce", func(r chi.Router) {
|
||||
r.Get("/", s.handleGetAllProduce)
|
||||
r.Post("/", s.handleAddProduce)
|
||||
r.Route("/{produceCode}", func(r chi.Router) {
|
||||
r.Delete("/", s.handleDeleteProduce)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -56,3 +59,24 @@ func (s *server) handleGetAllProduce(w http.ResponseWriter, r *http.Request) {
|
||||
s.writeSuccess(ctx, w, items, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *server) handleDeleteProduce(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
produceCode := chi.URLParam(r, "produceCode")
|
||||
if produceCode == "" {
|
||||
s.writeError(ctx, w, ErrUnrecognizedCode, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := s.produceSvc.Remove(produce.Item{
|
||||
Code: produceCode,
|
||||
})
|
||||
if err != nil {
|
||||
s.writeError(ctx, w, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
s.writeSuccess(ctx, w, nil, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -10,6 +12,7 @@ import (
|
||||
|
||||
"github.com/Rhymond/go-money"
|
||||
"github.com/davidlick/supermarket-api/internal/produce"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -142,3 +145,68 @@ func TestServer_handleGetAllProduce(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_handleDeleteProduce(t *testing.T) {
|
||||
tests := []struct {
|
||||
test string
|
||||
produceCode string
|
||||
expectFunc func(mockProduceSvc *MockProduceService)
|
||||
assertFunc func(t *testing.T, w *httptest.ResponseRecorder)
|
||||
}{
|
||||
{
|
||||
test: "it should respond no content when successful",
|
||||
produceCode: "test-code",
|
||||
expectFunc: func(mockProduceSvc *MockProduceService) {
|
||||
mockProduceSvc.EXPECT().Remove(produce.Item{Code: "test-code"}).Return(nil)
|
||||
},
|
||||
assertFunc: func(t *testing.T, w *httptest.ResponseRecorder) {
|
||||
assert.Equal(t, http.StatusNoContent, w.Code)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "it should respond internal server error if adding to service fails",
|
||||
produceCode: "test-code",
|
||||
expectFunc: func(mockProduceSvc *MockProduceService) {
|
||||
mockProduceSvc.EXPECT().Remove(produce.Item{Code: "test-code"}).Return(errors.New("test error"))
|
||||
},
|
||||
assertFunc: func(t *testing.T, w *httptest.ResponseRecorder) {
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "it should respond bad request if no produce code is supplied",
|
||||
expectFunc: func(mockProduceSvc *MockProduceService) {},
|
||||
assertFunc: func(t *testing.T, w *httptest.ResponseRecorder) {
|
||||
assert.Equal(t, http.StatusBadRequest, 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.MethodDelete, fmt.Sprintf("/v1/produce/%s", tc.produceCode), nil)
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add("produceCode", tc.produceCode)
|
||||
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, rctx)
|
||||
|
||||
r = r.WithContext(ctx)
|
||||
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.handleDeleteProduce)
|
||||
handler.ServeHTTP(w, r)
|
||||
|
||||
tc.assertFunc(t, w)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user