fix
This commit is contained in:
parent
cf57b921a7
commit
74d421788a
|
@ -45,14 +45,16 @@ func getTagById(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
tag, err := lgc.GetProductTagByID(uint(id))
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(apiError{http.StatusInternalServerError, err.Error()})
|
||||
return
|
||||
} else if err == types.ErrIDNotFound {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(apiError{http.StatusNotFound, ErrNoProductTagWithId.Error()})
|
||||
var status int
|
||||
if err == types.ErrIDNotFound {
|
||||
status = http.StatusNotFound
|
||||
} else {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(apiError{status, err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -63,14 +65,16 @@ func getTagByName(w http.ResponseWriter, r *http.Request) {
|
|||
name := mux.Vars(r)["name"]
|
||||
|
||||
tag, err := lgc.GetProductTagByName(name)
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(apiError{http.StatusInternalServerError, err.Error()})
|
||||
return
|
||||
} else if err == types.ErrIDNotFound {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(apiError{http.StatusNotFound, ErrNoProductTagWithName.Error()})
|
||||
var status int
|
||||
if err == types.ErrIDNotFound {
|
||||
status = http.StatusNotFound
|
||||
} else {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(apiError{status, err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -125,12 +129,23 @@ func putTagById(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
tag.Id = uint(id)
|
||||
if id != uint64(tag.Id) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(apiError{http.StatusBadRequest, ErrMismatchingIds.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
newTag, err := lgc.UpdateProductTag(tag)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(apiError{http.StatusInternalServerError, err.Error()})
|
||||
var status int
|
||||
if err == types.ErrIDNotFound {
|
||||
status = http.StatusNotFound
|
||||
} else {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(apiError{status, err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -149,8 +164,15 @@ func deleteTagById(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
tag, err := lgc.DeleteProductTag(uint(id))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(apiError{http.StatusInternalServerError, err.Error()})
|
||||
var status int
|
||||
if err == types.ErrIDNotFound {
|
||||
status = http.StatusNotFound
|
||||
} else {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(apiError{status, err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -13,15 +13,15 @@ import (
|
|||
"git.licolas.net/hoffman/server/types"
|
||||
)
|
||||
|
||||
func (l *logic) checkProductExists(id uint) error {
|
||||
func (l *logic) checkProductExists(id uint) (*types.Product, error) {
|
||||
product, err := l.dsrc.SelectProductById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
} else if product.Id == 0 {
|
||||
return types.ErrIDNotFound
|
||||
return &types.Product{}, types.ErrIDNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
return product, nil
|
||||
}
|
||||
|
||||
func (l *logic) CreateProduct(product *types.Product) (err error) {
|
||||
|
@ -48,16 +48,16 @@ func (l *logic) GetProductsLike(search *types.ProductSearch) ([]*types.Product,
|
|||
}
|
||||
|
||||
func (l *logic) UpdateProduct(newProduct *types.Product) (*types.Product, error) {
|
||||
if err := l.checkProductExists(newProduct.Id); err != nil {
|
||||
return nil, err
|
||||
if product, err := l.checkProductExists(newProduct.Id); err != nil {
|
||||
return product, err
|
||||
}
|
||||
|
||||
return l.dsrc.UpdateProduct(newProduct)
|
||||
}
|
||||
|
||||
func (l *logic) DeleteProduct(id uint) (*types.Product, error) {
|
||||
if err := l.checkProductExists(id); err != nil {
|
||||
return nil, err
|
||||
if product, err := l.checkProductExists(id); err != nil {
|
||||
return product, err
|
||||
}
|
||||
|
||||
return l.dsrc.DeleteProduct(id)
|
||||
|
|
|
@ -58,8 +58,8 @@ func (l *logic) GetProductTagsLikeName(name string) ([]*types.Tag, error) {
|
|||
}
|
||||
|
||||
func (l *logic) UpdateProductTag(tag *types.Tag) (*types.Tag, error) {
|
||||
if err := stripTag(l.checkTagExistsByID(tag.Id)); err != nil {
|
||||
return nil, err
|
||||
if tag, err := l.checkTagExistsByID(tag.Id); err != nil {
|
||||
return tag, err
|
||||
}
|
||||
|
||||
return l.dsrc.UpdateProductTag(tag)
|
||||
|
|
|
@ -187,7 +187,7 @@ func TestUpdateProductTag(t *testing.T) {
|
|||
|
||||
for _, tag := range testProductTags {
|
||||
mock.On("SelectProductTagById", tag.Id).Return(tag, nil)
|
||||
mock.On("UpdateProductTag", tag).Return(nil)
|
||||
mock.On("UpdateProductTag", tag).Return(tag, nil)
|
||||
actual, err := lgc.UpdateProductTag(tag)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tag, actual)
|
||||
|
@ -226,7 +226,7 @@ func TestUpdateProductTagUpdateError(t *testing.T) {
|
|||
|
||||
for _, tag := range testProductTags {
|
||||
mock.On("SelectProductTagById", tag.Id).Return(tag, nil)
|
||||
mock.On("UpdateProductTag", tag).Return(assert.AnError)
|
||||
mock.On("UpdateProductTag", tag).Return(nil, assert.AnError)
|
||||
actual, err := lgc.UpdateProductTag(tag)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, assert.AnError)
|
||||
|
|
|
@ -211,8 +211,7 @@ func TestUpdateProductNotFound(t *testing.T) {
|
|||
lgc := logic.UseDatasource(mock)
|
||||
|
||||
for _, p := range testProducts {
|
||||
mock.On("SelectProductById", p.Id).Return(nil, types.ErrIDNotFound)
|
||||
mock.On("UpdateProduct", p).Return(nil)
|
||||
mock.On("SelectProductById", p.Id).Return(new(types.Product), nil)
|
||||
actual, err := lgc.UpdateProduct(p)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, types.ErrIDNotFound)
|
||||
|
@ -239,7 +238,7 @@ func TestUpdateProductUpdateError(t *testing.T) {
|
|||
|
||||
for _, p := range testProducts {
|
||||
mock.On("SelectProductById", p.Id).Return(p, nil)
|
||||
mock.On("UpdateProduct", p).Return(assert.AnError)
|
||||
mock.On("UpdateProduct", p).Return(nil, assert.AnError)
|
||||
actual, err := lgc.UpdateProduct(p)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, assert.AnError)
|
||||
|
|
Loading…
Reference in New Issue