-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalon.sh
66 lines (47 loc) · 1.97 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
#!/bin/bash
PSQL="psql -X --username=freecodecamp --dbname=salon --tuples-only -c"
echo -e "\n~~~~~ Hair Salon ~~~~~\n";
MENU() {
if [[ $1 ]]
then
echo -e "\n$1\n"
fi
# show the service list
SERVICES=$($PSQL "SELECT service_id, name FROM services ORDER BY service_id")
echo "$SERVICES" | while read SERVICE_ID BAR NAME
do
echo "$SERVICE_ID) $NAME" ;
done
echo -e "\nPlease, select a service:"
read SERVICE_ID_SELECTED;
SERVICE_ID_SELECTED=$($PSQL "SELECT service_id FROM services WHERE service_id=$SERVICE_ID_SELECTED")
# if pick the wrong service
if [[ -z $SERVICE_ID_SELECTED ]]
then
# show the service list again
MENU "Sorry, that was not a valid Service ID."
else
# else, enter phone
echo -e "\nPlease, enter your phone number:"
read CUSTOMER_PHONE
# if phone does not exist, get name and insert data in customers
CUSTOMER_PHONE_COINCIDENCE=$($PSQL "SELECT phone FROM customers WHERE phone='$CUSTOMER_PHONE'")
if [[ -z $CUSTOMER_PHONE_COINCIDENCE ]]
then
echo -e "\nPlease, enter your name:"
read CUSTOMER_NAME
INSERT_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(phone, name) VALUES('$CUSTOMER_PHONE', '$CUSTOMER_NAME')")
fi
# get customer id and name
CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")
CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone='$CUSTOMER_PHONE'")
# get appointment time
echo -e "\nPlease, choose a time (FORMAT HH:MM) for your appointment:"
read SERVICE_TIME
# insert 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 | sed -r 's/^ *| *$//g') at $SERVICE_TIME, $(echo $CUSTOMER_NAME | sed -r 's/^ *| *$//g')."
fi
}
MENU;