Created
September 19, 2019 11:10
-
-
Save erinburns/2c42e0df0ddc81bfe0a62fa67d185539 to your computer and use it in GitHub Desktop.
Ruby code sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppointmentsController < ApplicationController | |
before_action :authenticate_user! | |
before_action :set_appointment, only: [:approve, :decline] | |
def create | |
service = Service.find(params[:service_id]) | |
if current_user == service.user | |
flash[:alert] = "You cannot book your own service." | |
elsif current_user.stripe_id.blank? | |
flash[:alert] = "Please update your payment method." | |
return redirect_to payment_method_path | |
else | |
appt_start = Date.parse(appointment_params[:appt_start]) | |
start_time = Time.parse(appointment_params[:start_time]) | |
end_time = Time.parse(appointment_params[:end_time]) | |
# appt_end = Date.parse(appointment_params[:appt_end]) | |
# days = (appt_end - appt_start).to_i + 1 | |
duration = (end_time - start_time)/ 3600 # convert duration to hours by dividing by seconds | |
@appointment = current_user.appointments.build(appointment_params) | |
@appointment.service = service | |
@appointment.price = service.price | |
@appointment.total = service.price * duration | |
@appointment.save | |
charge(service, @appointment) | |
flash[:notice] = "Booked Successfully!" | |
end | |
redirect_to service | |
end | |
def your_appointments | |
@services = current_user.services | |
end | |
# list of services logged in user purchased | |
def appointment_list | |
@appts = current_user.appointments.order(appt_start: :asc) | |
end | |
# list of services other users purchased from the logged in user | |
def client_list | |
@services = current_user.services | |
end | |
# destroy/ cancel a booking | |
def destroy | |
appointment = Appointment.find(params[:id]) | |
service = appointment.service | |
# send notification email | |
if current_user != appointment.user # if current user is not the guest | |
AppointmentMailer.cancelled_by_vendor(appointment.user, service, appointment).deliver_later | |
else | |
AppointmentMailer.cancelled_by_client(service.user.email, service, appointment).deliver_later | |
end | |
appointment.destroy | |
flash[:alert] = "This booking has been cancelled." | |
redirect_back fallback_location: request.referer | |
end | |
private | |
def set_appointment | |
@appointment = Appointment.find(params[:id]) | |
end | |
def appointment_params | |
params.require(:appointment).permit(:appt_start, :start_time, :end_time, :price, :total) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment