module Mks module Edm class EquipmentsController < ApplicationController before_action :set_equipment, only: %i[show update] def index data = Equipment.where(equipment_category_id: params[:id]) render json: Mks::Common::MethodResponse.success_response(data) end def nodes equipments = Equipment.where(equipment_category_id: params[:id]) data = equipments.map do |eq| { data: { id: eq.id, code: eq.code, name: eq.name, description: eq.description, type: 'EQ' }, leaf: false } end render json: data end def show render json: Mks::Common::MethodResponse.success_response(@equipment) end def create @equipment = Equipment.new(equipment_params) if @equipment.save response = Mks::Common::MethodResponse.success_response(@equipment, 'Equipment created successfully!') render json: response, status: :created, location: @equipment else response = Mks::Common::MethodResponse.failure_response(@equipment) render json: response, status: :unprocessable_entity end end def update if @equipment.update(equipment_params) render json: Mks::Common::MethodResponse.success_response(@equipment, 'Equipment updated successfully!') else render json: Mks::Common::MethodResponse.failure_response(@equipment), status: :unprocessable_entity end end def filter search = "%#{params[:search].downcase}%" equipments = Equipment.joins(:equipment_category) .where('lower(mks_edm_equipments.name) LIKE ? OR lower(mks_edm_equipment_categories.name) LIKE ?', search, search) render json: ApplicationRecord.json(equipments) end private def set_equipment @equipment = Equipment.find(params[:id]) end def equipment_params params.require(:equipment).permit(:code, :name, :description, :minimum_acquisition_time, :equipment_category_id) end end end end