module Suppliers
  class SectionElementsController < Suppliers::ApplicationController
    def index
      @section_elements = SectionElement.for_supplier(current_supplier)
      render json: @section_elements
    end

    def create
      @section_element.supplier = current_supplier
      if @section_element.save
        render json: @section_element
      else
        render json: {errors: @section.errors}, status: :unprocessable_entity
      end
    end

    def update
      if @section_element.update_attributes section_element_params
        render json: @section_element
      else
        render json: {errors: @section.errors}, status: :unprocessable_entity
      end
    end

    def destroy
      @section_element.destroy
      head :no_content
    end

    private

    def section_element_params
      params.require(:section_element).permit %i[name svg dpm box_width box_height position_x position_y rotation svg_element_id section_id]
    end
  end
end
