module Suppliers
  class SectionAreasController < Suppliers::ApplicationController
    def index
      @section_areas = SectionArea.for_supplier(current_supplier)
      render json: @section_areas
    end

    def create
      @section_area = SectionArea.new(section_area_params)
      @section_area.supplier = current_supplier
      if @section_area.save
        render json: @section_area
      else
        render json: {errors: @section.errors}, status: :unprocessable_entity
      end
    end

    def update
      @section_area = SectionArea.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      if @section_area.update_attributes section_area_params
        render json: @section_area
      else
        render json: {errors: @section.errors}, status: :unprocessable_entity
      end
    end

    def destroy
      @section_area= SectionArea.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      @section_area.destroy
      head :no_content
    end

    private

    def section_area_params
      params.require(:section_area).permit %i[title width height position_x position_y section_id rounded color]
    end
  end
end
