require_dependency 'application_controller' module Logistics module Core class RouteLookupsController < ApplicationController before_action :set_route_lookup, only: [:show, :edit, :update, :destroy] # GET /route_lookups # GET /route_lookups.json def index @route_lookups = RouteLookup.all respond_to do |format| response = Mks::Common::MethodResponse.new(true, nil, @route_lookups, nil, nil) format.json {render json: response} end end # POST /route_lookups # POST /route_lookups.json def create @route_lookup = RouteLookup.new(route_lookup_params) respond_to do |format| if @route_lookup.valid? @existing_check = RouteLookup.where("lower(code) = ? and lower(name) = ?","#{params['route_lookup']['code'].downcase}", "#{params['route_lookup']['name'].downcase}") if @existing_check.count == 0 if @route_lookup.save response = Mks::Common::MethodResponse.new(true, "Route Lookup Successfully saved!", @route_lookup, nil, nil) end else response = Mks::Common::MethodResponse.new(false, "Route Lookup record already exists with the same details!", nil, nil, nil) end else errors = Mks::Common::Util.error_messages @route_lookup, "Route Lookup" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, 0) end format.json {render json: response} end end # PATCH/PUT /route_lookups/1 # PATCH/PUT /route_lookups/1.json def update @old_route_lookup = set_route_lookup @update_route_lookup = RouteLookup.new(route_lookup_params) respond_to do |format| if @update_route_lookup.valid? @duplicate_check = RouteLookup.where("lower(name)= ?", "#{params['route_lookup']['name'].downcase}") if @duplicate_check.count == 0 || @duplicate_check.ids[0] == @old_route_lookup.id if @old_route_lookup.update(route_lookup_params) response = Mks::Common::MethodResponse.new(true, "Route Lookup Successfully Updated!", @old_route_lookup, nil, nil) end else response = Mks::Common::MethodResponse.new(false, "Route Lookup record already exists with the same details!", nil, nil, nil) end else errors = Mks::Common::Util.error_messages @update_route_lookup, "Route Lookup" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, 0) end format.json {render json: response} end end private def set_route_lookup @route_lookup = RouteLookup.find(params[:id]) end def route_lookup_params params.require(:route_lookup).permit(:code, :name) end end end end