module Mks module Edm class EquipmentCategoriesController < ApplicationController before_action :set_equipment_category, only: %i[show update] def index data = EquipmentCategory.where(equipment_type_id: params[:id]) render json: Mks::Common::MethodResponse.success_response(data) end def nodes equipment_categories = EquipmentCategory.where(equipment_type_id: params[:id]) categories = equipment_categories.map do |ec| { data: { id: ec.id, code: ec.code, name: ec.name, description: ec.description, type: 'EC' }, leaf: false } end render json: categories end def show render json: Mks::Common::MethodResponse.success_response(@equipment_category) end def create @equipment_category = EquipmentCategory.new(equipment_category_params) if @equipment_category.save response = Mks::Common::MethodResponse.success_response(@equipment_category, 'Equipment category created successfully!') render json: response, status: :created, location: @equipment_category else response = Mks::Common::MethodResponse.failure_response(@equipment_category) render json: response, status: :unprocessable_entity end end def update if @equipment_category.update(equipment_category_params) response = Mks::Common::MethodResponse.success_response(@equipment_category, 'Equipment category updated successfully!') render json: response else response = Mks::Common::MethodResponse.failure_response(@equipment_category) render json: response, status: :unprocessable_entity end end private def set_equipment_category @equipment_category = EquipmentCategory.find(params[:id]) end def equipment_category_params params.require(:equipment_category).permit(:code, :name, :description, :equipment_type_id) end end end end