# frozen_string_literal: true

class ProductsController < ApplicationController
  before_action :set_product, only: %i[show edit update destroy]

  def index
    products = Product.all
    products = products.search(params[:search][:query]) if params.dig(:search, :query).present?
    set_page_and_extract_portion_from products, ordered_by: { name: :asc, id: :desc }
  end

  def show
  end

  def new
    @product = Product.new
  end

  def create
    @product = Product.new(product_params)

    if @product.save
      redirect_to @product, status: :see_other, notice: "Saved"
    else
      render :new, status: :unprocessable_entity
    end
  end

  def edit
  end

  def update
    if @product.update(product_params)
      redirect_to @product, status: :see_other, notice: "Saved"
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @product.destroy
    redirect_to products_url, status: :see_other, notice: "Deleted"
  end

  private

    def set_product
      @product = Product.find(params[:id])
    end

    def product_params
      params.require(:product).permit(:name, :repository)
    end
end

