<commit_before>class Api::TimeslotsController < SecuredController
  def index
    @timeslots = Timeslot.all.includes(:tutor, :student)
  end

  def create
    timeslot = Timeslot.new(timeslot_params)
    if timeslot.save
      render plain: { message: "success" }
      current_user.track_event("created-timeslot")
    else
      render plain: { message: "fail" }
    end
  end

  def update
    timeslot = Timeslot.find_by(id: safe_params[:id])
    timeslot.student_id = current_user.id
    if timeslot.save
      render plain: { message: 'success' }
      timeslot.send_tutor_scheduling_email
      timeslot.send_student_scheduling_email
      current_user.track_event("booked-tutor")
    else
      render plain: { message: 'fail' }
    end
  end

  def cancel
    timeslot = Timeslot.find_by(id: safe_params[:id])
    student = timeslot.student
    timeslot.student_id = nil
    if (current_user.id == timeslot.tutor.id || current_user.id == student.id) && timeslot.save
      timeslot.send_tutor_cancel_email(student)
      timeslot.send_student_cancel_email(student)
      render plain: { message: 'success' }
    else
      render plain: { message: 'fail' }
    end
  end

  private
  def safe_params
    params.permit(:start, :id)
  end

  def timeslot_params
    params.permit(:start).merge(tutor_id: current_user.id)
  end
end<commit_msg>Add destroy route to api<commit_after>class Api::TimeslotsController < SecuredController
  def index
    @timeslots = Timeslot.all.includes(:tutor, :student)
  end

  def create
    timeslot = Timeslot.new(timeslot_params)
    if timeslot.save
      render plain: { message: "success" }
      current_user.track_event("created-timeslot")
    else
      render plain: { message: "fail" }
    end
  end

  def update
    timeslot = Timeslot.find_by(id: safe_params[:id])
    timeslot.student_id = current_user.id
    if timeslot.save
      render plain: { message: 'success' }
      timeslot.send_tutor_scheduling_email
      timeslot.send_student_scheduling_email
      current_user.track_event("booked-tutor")
    else
      render plain: { message: 'fail' }
    end
  end

  def cancel
    timeslot = Timeslot.find_by(id: safe_params[:id])
    student = timeslot.student
    timeslot.student_id = nil
    if (current_user.id == timeslot.tutor.id || current_user.id == student.id) && timeslot.save
      timeslot.send_tutor_cancel_email(student)
      timeslot.send_student_cancel_email(student)
      render plain: { message: 'success' }
    else
      render plain: { message: 'fail' }
    end
  end

  def destroy
    @timeslot = Timeslot.find(params[:id])
    if current_user == @timeslot.tutor && @timeslot.student.nil?
      @timeslot.destroy
      render plain: { message: 'success' }
    else
      render plain: { messag: 'fail' }
    end
  end

  private
  def safe_params
    params.permit(:start, :id)
  end

  def timeslot_params
    params.permit(:start).merge(tutor_id: current_user.id)
  end
end
{'fraction_non_alphanumeric': 0.09935789117945251, 'fraction_numerical': 0.0, 'mean_word_length': 3.1690140845070425, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '966335', 'n_tokens_mistral': 991, 'n_tokens_neox': 964, 'n_words': 203}