require_dependency 'application_controller' module Logistics module Core class WeightRangesController < ApplicationController protect_from_forgery with: :null_session before_action :set_weight_range, only: [:update] # GET /weight_ranges # GET /weight_ranges.json def index weight_ranges = WeightRange.fetch_all response = Mks::Common::MethodResponse.new(true, nil, weight_ranges, nil, nil) render json: response end # POST /weight_ranges # POST /weight_ranges.json def create weight_range = WeightRange.new(weight_range_params) overlap = WeightRange.check_range_overlap weight_range if !overlap if weight_range.save response = Mks::Common::MethodResponse.new(true, "Weight Range saved successfully!", weight_range, nil, nil) else errors = Mks::Common::Util.error_messages weight_range, "Weight Range" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end else errors = ["Weight range information overlap with previous entry!"] response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end # PATCH/PUT /weight_ranges/1 # PATCH/PUT /weight_ranges/1.json def update weight_range = WeightRange.find(params[:id]) new_weight_range = WeightRange.new(weight_range_params) overlap_result = WeightRange.check_range_overlap_update new_weight_range, params[:id] if !overlap_result['overlap'] if weight_range.update(weight_range_params) response = Mks::Common::MethodResponse.new(true, "Weight Range updated successfully!", weight_range, nil, nil) else errors = Mks::Common::Util.error_messages weight_range, "Weight Range" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end else errors = ["Weight range information overlap with previous entry!"] response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end private def set_weight_range @weight_range = WeightRange.find(params[:id]) end def weight_range_params params.require(:weight_range).permit(:weight_from, :weight_to) end end end end