diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 2c6eb11..7332dda 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -23,8 +23,34 @@ def create end end + def destroy + return require_authentication unless current_user + + return product_not_found unless find_product + + product_name = @product.name + request.session['flash'] = if @product.destroy + "Product '#{product_name}' deleted" + else + 'Delete failed' + end + + [[], 302, { 'location' => '/products' }] + end + + private + def find_product + product_id = request.params['id'] + @product = Product.find_by(id: product_id) + end + + def product_not_found + request.session['flash'] = 'Product not found' + [[], 302, { 'location' => '/products' }] + end + def product_params request.params.slice( 'name', diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index a826b82..9bfd36f 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -22,6 +22,7 @@