-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalon.sh
executable file
·69 lines (51 loc) · 2.31 KB
/
salon.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=salon --tuples-only -c"
echo -e "\nWelcome to Elite Barbers! How can I help?\n"
function MENU {
if [[ $1 ]]; then
echo -e "\n$1"
fi
echo -e "\nPlease choose a service:\n"
HAIRSTYLES=$($PSQL "SELECT service_id, name FROM services ORDER BY service_id")
echo "$HAIRSTYLES" | while read SERVICE_ID BAR SERVICE_NAME; do
echo "$SERVICE_ID) $SERVICE_NAME"
done
read SERVICE_ID_SELECTED
# Ensure valid service ID is selected
SERVICE_EXISTS=$($PSQL "SELECT service_id FROM services WHERE service_id = $SERVICE_ID_SELECTED")
if [[ -z $SERVICE_EXISTS ]]; then
MENU "Invalid selection. Please choose a valid service."
else
CREATE_APPOINTMENT
fi
}
function CREATE_APPOINTMENT {
echo -e "\nPlease enter your phone number:"
read CUSTOMER_PHONE
# Ensure phone number is valid
while [[ ! $CUSTOMER_PHONE =~ ^[0-9-]+$ ]]; do
echo -e "\nInvalid input. Please enter a valid phone number:"
read CUSTOMER_PHONE
done
# Check if customer exists
CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone = '$CUSTOMER_PHONE'")
CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone = '$CUSTOMER_PHONE'")
# If customer does not exist, ask for name and insert
if [[ -z $CUSTOMER_ID ]]; then
echo -e "\nNew customer detected. What is your name?"
read CUSTOMER_NAME
INSERT_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(name, phone) VALUES('$CUSTOMER_NAME', '$CUSTOMER_PHONE')")
CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone = '$CUSTOMER_PHONE'")
echo -e "\nWelcome, $CUSTOMER_NAME! We are happy to have you."
else
CUSTOMER_NAME=$(echo $CUSTOMER_NAME | xargs)
echo -e "\nWelcome back, $CUSTOMER_NAME!"
fi
echo -e "\nWhat time would you like to book your appointment?"
read SERVICE_TIME
# Insert new appointment
INSERT_APPOINTMENT=$($PSQL "INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_ID, $SERVICE_ID_SELECTED, '$SERVICE_TIME')")
SERVICE_NAME=$($PSQL "SELECT name FROM services WHERE service_id = $SERVICE_ID_SELECTED")
echo -e "\nI have put you down for a $(echo $SERVICE_NAME | xargs) at $SERVICE_TIME, $CUSTOMER_NAME."
}
MENU