<commit_before>module V1
  class ReservationsController < ApplicationController
    before_action :find_reservation, only: %i(destroy show update update_item)

    def create
      item_type = ItemType.find_by name: params.require(:item_type)
      start_time = DateTime.iso8601 params.require(:start_time)
      end_time = DateTime.iso8601 params.require(:end_time)
      item = item_type.find_available start_time, end_time
      if item.present?
        reservation = item.reserve! from: start_time,
                                    to: end_time,
                                    creator: @service
        render json: reservation
      else render nothing: true, status: :unprocessable_entity
      end
    end

    def destroy
      @reservation.destroy
      render nothing: true
    end

    def index
      deny_access! and return unless @service.can_read? @reservation.item_type
      item_type = ItemType.find_by name: params.require(:item_type)
      if item_type.present?
        start_datetime = DateTime.iso8601 params.require(:start_time)
        end_datetime = DateTime.iso8601 params.require(:end_time)
        reservations = item_type.reservations
                                .during start_datetime, end_datetime
        reservations = reservations.map do |reservation|
          { start_time: reservation.start_datetime.iso8601,
            end_time: reservation.end_datetime.iso8601 }
        end
        render json: reservations
      else render nothing: true, status: :not_found
      end
    end

    def show
      render json: @reservation
    end

    def update
      params.require(:reservation).permit(:start_time, :end_time)
      if @reservation.update datetime_interpolated_changes
        render nothing: true
      else render json: { errors: @reservation.errors.full_messages },
                  status: :unprocessable_entity
      end
    end

    def update_item
      if @reservation.item.update data: params.require(:data)
        render nothing: true
      else render json: { errors: @reservation.item.errors.full_messages },
                  status: :unprocessable_entity
      end
    end

    private

    def find_reservation
      @reservation = Reservation.find_by id: params.require(:id)
      render nothing: true, status: :not_found unless @reservation.present?
      deny_access! and return unless @service.can_edit? @reservation
    end

    def datetime_interpolated_changes
      changes = {}
      reservation = params[:reservation]
      if reservation[:start_time].present?
        changes[:start_datetime] = DateTime.iso8601 reservation[:start_time]
      end
      if reservation[:end_time].present?
        changes[:end_datetime] = DateTime.iso8601 reservation[:end_time]
      end
      changes
    end
  end
end
<commit_msg>Reduce assignment branch condition size for ResCont#index<commit_after>module V1
  class ReservationsController < ApplicationController
    before_action :find_reservation, only: %i(destroy show update update_item)
    before_action :find_item_type, only: :index

    def create
      item_type = ItemType.find_by name: params.require(:item_type)
      start_time = DateTime.iso8601 params.require(:start_time)
      end_time = DateTime.iso8601 params.require(:end_time)
      item = item_type.find_available start_time, end_time
      if item.present?
        reservation = item.reserve! from: start_time,
                                    to: end_time,
                                    creator: @service
        render json: reservation
      else render nothing: true, status: :unprocessable_entity
      end
    end

    def destroy
      @reservation.destroy
      render nothing: true
    end

    def index
      start_datetime = DateTime.iso8601 params.require(:start_time)
      end_datetime = DateTime.iso8601 params.require(:end_time)
      reservations = @item_type.reservations
                               .during start_datetime, end_datetime
      reservations = reservations.map do |reservation|
        { start_time: reservation.start_datetime.iso8601,
          end_time: reservation.end_datetime.iso8601 }
      end
      render json: reservations
    end

    def show
      render json: @reservation
    end

    def update
      params.require(:reservation).permit(:start_time, :end_time)
      if @reservation.update datetime_interpolated_changes
        render nothing: true
      else render json: { errors: @reservation.errors.full_messages },
                  status: :unprocessable_entity
      end
    end

    def update_item
      if @reservation.item.update data: params.require(:data)
        render nothing: true
      else render json: { errors: @reservation.item.errors.full_messages },
                  status: :unprocessable_entity
      end
    end

    private

    def find_item_type
      deny_access! and return unless @service.can_read? @reservation.item_type
      @item_type = ItemType.find_by name: params.require(:item_type)
      render nothing: true, status: :not_found unless @item_type.present?
    end

    def find_reservation
      @reservation = Reservation.find_by id: params.require(:id)
      render nothing: true, status: :not_found unless @reservation.present?
      deny_access! and return unless @service.can_edit? @reservation
    end

    def datetime_interpolated_changes
      changes = {}
      reservation = params[:reservation]
      if reservation[:start_time].present?
        changes[:start_datetime] = DateTime.iso8601 reservation[:start_time]
      end
      if reservation[:end_time].present?
        changes[:end_datetime] = DateTime.iso8601 reservation[:end_time]
      end
      changes
    end
  end
end

{'fraction_non_alphanumeric': 0.07096434217460039, 'fraction_numerical': 0.011593184612682241, 'mean_word_length': 2.6175349428208388, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2757404', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1540, 'n_words': 383}