module Logistics module Core class CarriersController < ApplicationController before_action :set_carrier, only: [:update] def index @carrier = Carrier.all carrier_array =[] @carrier.each { |c| carrier_array.push({:id => c.id, :name => c.name, :email => c.email, :contact_telephone => c.contact_telephone, :carrier_type_id => c.carrier_type_id, :carrier_type_name => c.carrier_type.name}) } @response = Mks::Common::MethodResponse.new(true, nil, carrier_array, nil, nil) render json: @response end def create @carrier = Carrier.new(carrier_params) if @carrier.valid? @carrier.save @response = Mks::Common::MethodResponse.new(true, 'Carrier saved successfully !',nil,nil,nil); render json: @response else errors = Mks::Common::Util.error_messages @carrier, 'Carrier' @response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) render json: @response end end def update if @carrier.update_attributes(carrier_params) @carrier.save @response = Mks::Common::MethodResponse.new(true, 'Carrier updated successfully !', nil, nil, nil) render json: @response else errors = Mks::Common::Util.error_messages @carrier, 'Carrier' @response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) render json: @response end end def engaged_agents carrier_id = params[:carrier_id] engaged_agents_id = CarrierAgent.select(:agent_id).where('carrier_id' =>carrier_id) agents = Agent.where(id: engaged_agents_id) @response = Mks::Common::MethodResponse.new(true, nil, agents, nil, nil) render json: @response end def unengaged_agents carrier_id = params[:carrier_id] engaged_agents = CarrierAgent.select(:agent_id).where('carrier_id' => carrier_id) unengaged_agents = Agent.where.not(id: engaged_agents) @response = Mks::Common::MethodResponse.new(true, nil, unengaged_agents, nil, nil) render json: @response end private # Use callbacks to share common setup or constraints between actions. def set_carrier @carrier = Carrier.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def carrier_params params.require(:carrier).permit(:name, :email, :contact_telephone, :carrier_type_id) end end end end