require_dependency 'application_controller' module Logistics module Core class TransportersController < ApplicationController protect_from_forgery with: :null_session before_action :set_transporter, only: [:show, :update, :destroy] # GET /transporters # GET /transporters.json def index @transporters = Transporter.where.not("name" => "Reference Transporter") respond_to do |format| response = Mks::Common::MethodResponse.new(true,nil,@transporters,nil,nil) format.json {render json: response} end end def lookup ids = RouteRate.select(:transporter_id) @transporters = Transporter.order(:name).select("id, name").where("id" => ids).where.not("name" => "Reference Transporter") response = Mks::Common::MethodResponse.new(true, nil, @transporters, nil, nil) render json: response end # POST /transporters # POST /transporters.json def create @transporter = Transporter.new(transporter_params) respond_to do |format| if @transporter.valid? @existing_check = Transporter.where("lower(name) = ?", "#{params['transporter']['name'].downcase}") if @existing_check.count == 0 if @transporter.save response = Mks::Common::MethodResponse.new(true, "Transporter record successfully saved!", @transporter, nil, nil) end else response = Mks::Common::MethodResponse.new(false, "Transporter record already exists with the same detail!", nil, nil) end else errors = Mks::Common::Util.error_messages @transporter, "Transporter" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, 0) end format.json {render json: response} end end # PATCH/PUT /transporters/1 # PATCH/PUT /transporters/1.json def update @old_transporter = Transporter.find(params[:id]) @update_transporter = Transporter.new(transporter_params) respond_to do |format| if @old_transporter.valid? @duplicate_check = Transporter.where("lower(name) = ?", "#{params['transporter']['name'].downcase}") if @duplicate_check.count == 0 || @duplicate_check.ids[0] == @old_transporter.id if @old_transporter.update(transporter_params) response = Mks::Common::MethodResponse.new(true, "Transporter record successfully updated!", @old_transporter, nil, nil) end else response = Mks::Common::MethodResponse.new(false, "Transporter record already exists with the same details!", nil, nil, nil) end else errors = Mks::Common::Util.error_messages @update_transporter, "Transporter" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, 0) end format.json {render json: response} end end private def set_transporter @transporter = Transporter.find(params[:id]) end def transporter_params params.require(:transporter).permit(:name, :postal_code, :city, :country_id, :telephone, :fax, :email, :contact_person, :contact_telephone) end end end end