#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=salon --tuples-only -c"
echo -e "\n~~~~~ MY SALON ~~~~~"
echo -e "\nWelcome to My Salon, how can I help you?\n"
BOOK_SERVICE() {
# Display message if provided
if [[ $1 ]]; then
echo -e "\n$1"
fi
# Fetch and display available services
SERVICES=$($PSQL "SELECT service_id, name FROM services")
echo "$SERVICES" | while read SERVICE_ID BAR SERVICE_NAME; do
echo -e "$SERVICE_ID) $SERVICE_NAME"
done
# Read service selected by user
read SERVICE_ID_SELECTED
# Check if input is a number
if [[ ! $SERVICE_ID_SELECTED =~ ^[0-9]+$ ]]; then
BOOK_SERVICE "I could not find that service. What would you like today?"
else
# Check if the selected service is available
SERVICE_AVAILABILITY=$($PSQL "SELECT service_id FROM services WHERE service_id=$SERVICE_ID_SELECTED")
# If service is not available, prompt the user again
if [[ -z $SERVICE_AVAILABILITY ]]; then
BOOK_SERVICE "I could not find that service. What would you like today?"
else
echo -e "\nWhat's your phone number?"
# Read customer phone number
read CUSTOMER_PHONE_INPUT
# Fetch customer name using phone number
CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone='$CUSTOMER_PHONE_INPUT'")
# If customer does not exist, ask for the name
if [[ -z $CUSTOMER_NAME ]]; then
echo -e "\nI don't have a record for that phone number, what's your name?"
# Read new customer name
read CUSTOMER_NAME
# Insert new customer into the database
INSERT_NEW_CUSTOMER=$($PSQL "INSERT INTO customers(name, phone) VALUES('$CUSTOMER_NAME', '$CUSTOMER_PHONE_INPUT')")
fi
# Fetch service name using the selected service ID
SERVICE_NAME=$($PSQL "SELECT name FROM services WHERE service_id=$SERVICE_ID_SELECTED")
# Format customer name for display
CUSTOMER_NAME_FORMATTED=$(echo $CUSTOMER_NAME | sed 's/ //g')
# Format customer name for display
SERVICE_NAME_FORMATTED=$(echo $SERVICE_NAME | sed 's/ //g')
# Ask for appointment time
echo -e "\nWhat time would you like your $SERVICE_NAME_FORMATTED, $CUSTOMER_NAME_FORMATTED?"
read SERVICE_TIME_INPUT
# Get customer ID
CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE_INPUT'")
# Insert appointment into the database
INSERT_APPOINTMENT=$($PSQL "INSERT INTO appointments(time, customer_id, service_id) VALUES('$SERVICE_TIME_INPUT', $CUSTOMER_ID, $SERVICE_ID_SELECTED)")
# Confirm the appointment
echo -e "\nI have put you down for a $SERVICE_NAME_FORMATTED at $SERVICE_TIME_INPUT, $CUSTOMER_NAME_FORMATTED.\n"
fi
fi
}
BOOK_SERVICE
Database looks like this
salon=> \d appointments
Table "public.appointments"
Column | Type | Collation | Nullable | Default
----------------+----------------------+-----------+----------+-----------------------------------------------------
appointment_id | integer | | not null | nextval('appointment_appointment_id_seq'::regclass)
time | character varying(5) | | not null |
customer_id | integer | | not null |
service_id | integer | | not null |
Indexes:
"appointment_pkey" PRIMARY KEY, btree (appointment_id)
Foreign-key constraints:
"appointment_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
"appointment_service_id_fkey" FOREIGN KEY (service_id) REFERENCES services(service_id)
salon=>
Table "public.customers"
Column | Type | Collation | Nullable | Default
-------------+-----------------------+-----------+----------+------------------------------------------------
customer_id | integer | | not null | nextval('customers_customer_id_seq'::regclass)
name | character varying(30) | | not null |
phone | character varying(15) | | not null |
Indexes:
"customers_pkey" PRIMARY KEY, btree (customer_id)
"customers_phone_key" UNIQUE CONSTRAINT, btree (phone)
Referenced by:
TABLE "appointments" CONSTRAINT "appointment_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
Table "public.services"
Column | Type | Collation | Nullable | Default
------------+-----------------------+-----------+----------+----------------------------------------------
service_id | integer | | not null | nextval('services_service_id_seq'::regclass)
name | character varying(30) | | not null |
price | integer | | not null |
Indexes:
"services_pkey" PRIMARY KEY, btree (service_id)
"services_name_key" UNIQUE CONSTRAINT, btree (name)
Referenced by:
TABLE "appointments" CONSTRAINT "appointment_service_id_fkey" FOREIGN KEY (service_id) REFERENCES services(service_id)
salon=>
I tried increasing time out in test/utils.js and also a code from someone that they had mentioned passed the test but both didn’t worked out.