module Mks module Edm class EquipmentItemsController < ApplicationController before_action :set_equipment_item, only: %i[show update] def index data = EquipmentItem.where(equipment_id: params[:id]) render json: Mks::Common::MethodResponse.success_response(data) end def nodes equipment_items = EquipmentItem.where(equipment_id: params[:id]) items = equipment_items.map do |ei| { data: { id: ei.id, code: ei.code, name: ei.name, description: ei.description, type: 'EC' }, leaf: true } end render json: items end def show render json: Mks::Common::MethodResponse.success_response(@equipment_item) end def create @equipment_item = EquipmentItem.new(equipment_item_params) if @equipment_item.save response = Mks::Common::MethodResponse.success_response(@equipment_item, 'Equipment item created successfully!') render json: response, status: :created, location: @equipment_item else response = Mks::Common::MethodResponse.failure_response(@equipment_item) render json: response, status: :unprocessable_entity end end def update if @equipment_item.update(equipment_item_params) response = Mks::Common::MethodResponse.success_response(@equipment_item, 'Equipment item updated successfully!') render json: response else render json: Mks::Common::MethodResponse.failure_response(@equipment_item), status: :unprocessable_entity end end def filter search = "%#{params[:search].downcase}%" equipment_items = EquipmentItem.joins(:equipment) .where('lower(mks_edm_equipment_items.name) LIKE ? OR lower(mks_edm_equipments.name) LIKE ?', search, search) render json: ApplicationRecord.as_json(equipment_items) end private def set_equipment_item @equipment_item = EquipmentItem.find(params[:id]) end def equipment_item_params params.require(:equipment_item).permit(:code, :name, :description, :serial_number, :brand, :item_model, :purchase_price, :purchase_date, :tax, :equipment_id, :current_location_id) end end end end