types(en+) return update instead of changing

This commit is contained in:
Theo Technicguy 2023-08-18 20:01:36 +02:00
parent 4112c07807
commit 71d2d5bf61
Signed by: theo.technicguy
GPG Key ID: 3BC661201BCA53D9
7 changed files with 47 additions and 21 deletions

View File

@ -159,7 +159,8 @@ func putProductById(w http.ResponseWriter, r *http.Request) {
return
}
if err = lgc.UpdateProduct(&newProduct); err != nil {
product, err := lgc.UpdateProduct(&newProduct)
if err != nil {
var status int
if err == types.ErrIDNotFound {
status = http.StatusNotFound
@ -173,7 +174,7 @@ func putProductById(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(newProduct)
json.NewEncoder(w).Encode(product)
}
func deleteProductById(w http.ResponseWriter, r *http.Request) {

View File

@ -127,14 +127,15 @@ func putTagById(w http.ResponseWriter, r *http.Request) {
tag.Id = uint(id)
if err = lgc.UpdateProductTag(tag); err != nil {
newTag, err := lgc.UpdateProductTag(tag)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(apiError{http.StatusInternalServerError, err.Error()})
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(tag)
json.NewEncoder(w).Encode(newTag)
}
func deleteTagById(w http.ResponseWriter, r *http.Request) {

View File

@ -188,8 +188,9 @@ func TestUpdateProductTag(t *testing.T) {
for _, tag := range testProductTags {
mock.On("SelectProductTagById", tag.Id).Return(tag, nil)
mock.On("UpdateProductTag", tag).Return(nil)
err := lgc.UpdateProductTag(tag)
actual, err := lgc.UpdateProductTag(tag)
assert.NoError(t, err)
assert.Equal(t, tag, actual)
}
}
@ -199,9 +200,10 @@ func TestUpdateProductTagNotFound(t *testing.T) {
for _, tag := range testProductTags {
mock.On("SelectProductTagById", tag.Id).Return(&types.Tag{}, nil)
err := lgc.UpdateProductTag(tag)
actual, err := lgc.UpdateProductTag(tag)
require.Error(t, err)
assert.ErrorIs(t, err, types.ErrIDNotFound)
assert.Equal(t, new(types.Tag), actual)
}
}
@ -211,9 +213,10 @@ func TestUpdateProductTagSelectError(t *testing.T) {
for _, tag := range testProductTags {
mock.On("SelectProductTagById", tag.Id).Return(nil, assert.AnError)
err := lgc.UpdateProductTag(tag)
actual, err := lgc.UpdateProductTag(tag)
require.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
assert.Nil(t, actual)
}
}
@ -224,9 +227,10 @@ func TestUpdateProductTagUpdateError(t *testing.T) {
for _, tag := range testProductTags {
mock.On("SelectProductTagById", tag.Id).Return(tag, nil)
mock.On("UpdateProductTag", tag).Return(assert.AnError)
err := lgc.UpdateProductTag(tag)
actual, err := lgc.UpdateProductTag(tag)
require.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
assert.Nil(t, actual)
}
}

View File

@ -199,9 +199,10 @@ func TestUpdateProduct(t *testing.T) {
for _, p := range testProducts {
mock.On("SelectProductById", p.Id).Return(p, nil)
mock.On("UpdateProduct", p).Return(nil)
err := lgc.UpdateProduct(p)
mock.On("UpdateProduct", p).Return(p, nil)
actual, err := lgc.UpdateProduct(p)
assert.NoError(t, err)
assert.Equal(t, p, actual)
}
}
@ -212,9 +213,10 @@ func TestUpdateProductNotFound(t *testing.T) {
for _, p := range testProducts {
mock.On("SelectProductById", p.Id).Return(nil, types.ErrIDNotFound)
mock.On("UpdateProduct", p).Return(nil)
err := lgc.UpdateProduct(p)
actual, err := lgc.UpdateProduct(p)
require.Error(t, err)
assert.ErrorIs(t, err, types.ErrIDNotFound)
assert.Equal(t, new(types.Product), actual)
}
}
@ -224,9 +226,10 @@ func TestUpdateProductSelectError(t *testing.T) {
for _, p := range testProducts {
mock.On("SelectProductById", p.Id).Return(nil, assert.AnError)
err := lgc.UpdateProduct(p)
actual, err := lgc.UpdateProduct(p)
require.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
assert.Nil(t, actual)
}
}
@ -237,9 +240,10 @@ func TestUpdateProductUpdateError(t *testing.T) {
for _, p := range testProducts {
mock.On("SelectProductById", p.Id).Return(p, nil)
mock.On("UpdateProduct", p).Return(assert.AnError)
err := lgc.UpdateProduct(p)
actual, err := lgc.UpdateProduct(p)
require.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
assert.Nil(t, actual)
}
}

View File

@ -104,9 +104,17 @@ func (m *mockedDataSource) SelectProductsLike(ps *types.ProductSearch) ([]*types
}
}
func (m *mockedDataSource) UpdateProduct(p *types.Product) error {
func (m *mockedDataSource) UpdateProduct(p *types.Product) (*types.Product, error) {
args := m.Called(p)
return args.Error(0)
switch v := args.Get(0).(type) {
case *types.Product:
return v, args.Error(1)
case nil:
return nil, args.Error(1)
default:
return nil, ErrConversion
}
}
func (m *mockedDataSource) DeleteProduct(id uint) (*types.Product, error) {
@ -189,9 +197,17 @@ func (m *mockedDataSource) SelectProductTagsLikeName(name string) ([]*types.Tag,
}
}
func (m *mockedDataSource) UpdateProductTag(tag *types.Tag) error {
func (m *mockedDataSource) UpdateProductTag(tag *types.Tag) (*types.Tag, error) {
args := m.Called(tag)
return args.Error(0)
switch v := args.Get(0).(type) {
case *types.Tag:
return v, args.Error(1)
case nil:
return nil, args.Error(1)
default:
return nil, ErrConversion
}
}
func (m *mockedDataSource) DeleteProductTag(id uint) (*types.Tag, error) {

View File

@ -71,7 +71,7 @@ type Datasource interface {
// This means that taggings also have to be updated. The product
// pointer is then updated.
// The ID can never be updated.
UpdateProduct(*Product) error
UpdateProduct(*Product) (*Product, error)
// DeleteProduct completely removes the product from the product
// segment of the data source persistant storage. This means that
@ -115,7 +115,7 @@ type Datasource interface {
// The ID can never be updated.
// Product tags have unique names. Duplicates are expected to be
// rejected.
UpdateProductTag(*Tag) error
UpdateProductTag(*Tag) (*Tag, error)
// DeleteProductTag completely removes the product tag from the
// product tag segment of the data source persistant storage.

View File

@ -47,7 +47,7 @@ type LogicProvider interface {
// UpdateProduct checks if the product ID exists. If
// it does, it updates the product with the ID to match
// the data passed. Otherwise, it returns ErrIDNotFound.
UpdateProduct(*Product) error
UpdateProduct(*Product) (*Product, error)
// DeleteProduct checks if the product ID exists. If it
// does, it requests deletion of the product to the data
@ -81,7 +81,7 @@ type LogicProvider interface {
// If it does, it updates the product tag with the ID to
// match the data passed. Otherwise, it returns
// ErrIDNotFound.
UpdateProductTag(*Tag) error
UpdateProductTag(*Tag) (*Tag, error)
// DeleteProductTag checks if the product tag ID exists.
// If it does, it requests deletion of the product tag