require_dependency 'application_controller' module Logistics module Core class TruckTypeAssignmentsController < ApplicationController protect_from_forgery with: :null_session before_action :set_truck_type_assignment, only: [:update] # GET /truck_type_assignments # GET /truck_type_assignments.json def index @truck_type_assignments = TruckTypeAssignment.fetch_all respond_to do |format| response = Mks::Common::MethodResponse.new(true, nil, @truck_type_assignments, nil, nil) format.json {render json: response} end end # POST /truck_type_assignments # POST /truck_type_assignments.json def create @truck_type_assignment = TruckTypeAssignment.new(truck_type_assignment_params) respond_to do |format| if @truck_type_assignment.valid? @existing_check = TruckTypeAssignment.where("container_size_id = ? and truck_type = ?", "#{params['truck_type_assignment']['container_size_id']}", "#{params['truck_type_assignment']['truck_type']}") if @existing_check.count == 0 if @truck_type_assignment.save response = Mks::Common::MethodResponse.new(true, "Truck Type Assignment record successfully saved!",@truck_type_assignment, nil, nil) end else errors = ["Truck Type Assignment record already exists with the same detail!"] response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end else errors = Mks::Common::Util.error_messages @truck_type_assignment, "Truck Type Assignment" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, 0) end format.json {render json: response} end end # PATCH/PUT /truck_type_assignments/1 # PATCH/PUT /truck_type_assignments/1.json def update @old_truck_type_assignment = TruckTypeAssignment.find(params[:id]) @update_truck_type_assignment = TruckTypeAssignment.new(truck_type_assignment_params) respond_to do |format| if @update_truck_type_assignment.valid? @duplicate_check = TruckTypeAssignment.where("container_size_id = ? and truck_type = ?", "#{params['truck_type_assignment']['container_size_id']}", "#{params['truck_type_assignment']['truck_type']}") if @duplicate_check.count == 0 || @duplicate_check.ids[0] == @old_truck_type_assignment.id if @old_truck_type_assignment.update(truck_type_assignment_params) response = Mks::Common::MethodResponse.new(true, "Truck Type Assignment record successfully updated!", @old_truck_type_assignment, nil, nil) end else errors = ["Truck Type Assignment record already exists with the same detail!"] response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end else errors = Mks::Common::Util.error_messages @update_truck_type_assignment, "Truck Type Assignment" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, 0) end format.json {render json: response} end end private def set_truck_type_assignment @truck_type_assignment = TruckTypeAssignment.find(params[:id]) end def truck_type_assignment_params params.require(:truck_type_assignment).permit(:container_size_id, :truck_type) end end end end