Add available bookings scripts and results

This commit is contained in:
2024-11-18 15:40:17 +01:00
parent 9308f5471a
commit 0d75c1c035
5 changed files with 166 additions and 0 deletions

30
sql/add_valid_booking.sql Normal file
View File

@@ -0,0 +1,30 @@
-- available bookings - task 3
-- changes:
-- changed BookingDate (Reserved) data type from date to datetime, because I assumed that Little Lemon
-- allows wants bookings per day and table - a table is considered blocked for 2 hours
-- added CustomerID and EmployeeID input parameters
-- noqa: disable=CP03,LT02,RF03
--
create procedure AddValidBooking (
in Reserved datetime, in TableNumber int, in CustomerID int, in EmployeeID int
)
begin
start transaction;
if TableIsFree(Reserved, TableNumber) then
insert into Bookings (Reserved, TableNumber, CustomerID, EmployeeID) values (
Reserved, TableNumber, CustomerID, EmployeeID
);
commit;
select concat(
"Table ", TableNumber,
" was free - booking accepted (BookingID=", (select last_insert_id()), ')'
) as BookingStatus;
else
rollback;
select concat(
"Table ", TableNumber,
" is already booked - booking cancelled"
) as BookingStatus;
end if;
end

View File

@@ -0,0 +1,97 @@
# Task 1
> source ./example_bookings.sql
> select * from `Bookings` where `BookingID` between 11 and 14
+-----------+---------------------+-------------+------------+------------+
| BookingID | Reserved | TableNumber | CustomerID | EmployeeID |
+-----------+---------------------+-------------+------------+------------+
| 11 | 2022-10-14 18:00:00 | 5 | 1 | 1 |
| 12 | 2022-11-12 19:30:00 | 3 | 3 | 1 |
| 13 | 2022-10-11 18:45:00 | 2 | 2 | 1 |
| 14 | 2022-10-13 19:15:00 | 2 | 1 | 1 |
+-----------+---------------------+-------------+------------+------------+
# Task 2
> delimiter $
> source ./table_is_free.sql
> source ./check_booking.sql
> delimiter ;
> call CheckBooking("2022-11-12 18:15:00", 3, @IsFree);
+---------------------------+
| BookingStatus |
+---------------------------+
| Table 3 is already booked |
+---------------------------+
> select @IsFree;
+---------+
| @IsFree |
+---------+
| 0 |
+---------+
> call CheckBooking("2022-11-12 17:15:00", 3, @IsFree);
+-----------------+
| BookingStatus |
+-----------------+
| Table 3 is free |
+-----------------+
> select @IsFree;
+---------+
| @IsFree |
+---------+
| 1 |
+---------+
# Task 3
> delimiter $
> source ./add_valid_booking.sql
> delimiter ;
> call AddValidBooking("2022-11-12 18:15:00", 3, 4, 1);
+-----------------------------------------------+
| BookingStatus |
+-----------------------------------------------+
| Table 3 is already booked - booking cancelled |
+-----------------------------------------------+
> select * from `Bookings`
+-----------+---------------------+-------------+------------+------------+
| BookingID | Reserved | TableNumber | CustomerID | EmployeeID |
+-----------+---------------------+-------------+------------+------------+
| 1 | 2024-11-14 18:00:00 | 1 | 3 | 1 |
| 2 | 2024-11-14 19:30:00 | 2 | 2 | 1 |
| 3 | 2024-11-14 18:45:00 | 3 | 4 | 1 |
| 4 | 2024-11-14 19:15:00 | 4 | 1 | 2 |
| 5 | 2024-11-14 18:30:00 | 5 | 6 | 2 |
| 6 | 2024-11-14 19:00:00 | 6 | 5 | 2 |
| 7 | 2024-11-15 18:15:00 | 1 | 7 | 5 |
| 8 | 2024-11-15 19:45:00 | 2 | 8 | 5 |
| 9 | 2024-11-15 18:30:00 | 3 | 9 | 2 |
| 10 | 2024-11-15 19:00:00 | 4 | 10 | 5 |
| 11 | 2022-10-14 18:00:00 | 5 | 1 | 1 |
| 12 | 2022-11-12 19:30:00 | 3 | 3 | 1 |
| 13 | 2022-10-11 18:45:00 | 2 | 2 | 1 |
| 14 | 2022-10-13 19:15:00 | 2 | 1 | 1 |
+-----------+---------------------+-------------+------------+------------+
> call AddValidBooking("2022-11-12 17:15:00", 3, 4, 1);
+----------------------------------------------------+
| BookingStatus |
+----------------------------------------------------+
| Table 3 was free - booking accepted (BookingID=15) |
+----------------------------------------------------+
> select * from `Bookings`
+-----------+---------------------+-------------+------------+------------+
| BookingID | Reserved | TableNumber | CustomerID | EmployeeID |
+-----------+---------------------+-------------+------------+------------+
| 1 | 2024-11-14 18:00:00 | 1 | 3 | 1 |
| 2 | 2024-11-14 19:30:00 | 2 | 2 | 1 |
| 3 | 2024-11-14 18:45:00 | 3 | 4 | 1 |
| 4 | 2024-11-14 19:15:00 | 4 | 1 | 2 |
| 5 | 2024-11-14 18:30:00 | 5 | 6 | 2 |
| 6 | 2024-11-14 19:00:00 | 6 | 5 | 2 |
| 7 | 2024-11-15 18:15:00 | 1 | 7 | 5 |
| 8 | 2024-11-15 19:45:00 | 2 | 8 | 5 |
| 9 | 2024-11-15 18:30:00 | 3 | 9 | 2 |
| 10 | 2024-11-15 19:00:00 | 4 | 10 | 5 |
| 11 | 2022-10-14 18:00:00 | 5 | 1 | 1 |
| 12 | 2022-11-12 19:30:00 | 3 | 3 | 1 |
| 13 | 2022-10-11 18:45:00 | 2 | 2 | 1 |
| 14 | 2022-10-13 19:15:00 | 2 | 1 | 1 |
| 15 | 2022-11-12 17:15:00 | 3 | 4 | 1 |
+-----------+---------------------+-------------+------------+------------+

10
sql/check_booking.sql Normal file
View File

@@ -0,0 +1,10 @@
-- available bookings - task 2
-- changes:
-- changed BookingDate (Reserved) data type from date to datetime, because I assumed that Little Lemon
-- allows wants bookings per day and table - a table is considered blocked for 2 hours
-- noqa: disable=CP03,LT02,RF03
create procedure CheckBooking (in Reserved datetime, in TableNumber int, out IsFree boolean) --
begin
set IsFree = TableIsFree(Reserved, TableNumber);
select concat("Table ", TableNumber, " is ", if(IsFree, "free", "already booked")) as BookingStatus;
end

12
sql/example_bookings.sql Normal file
View File

@@ -0,0 +1,12 @@
-- available bookings - task 1
-- changes:
-- changed BookingDate (Reserved) data type from date to datetime, because I assumed that Little Lemon
-- allows wants bookings per day and table - a table is considered blocked for 2 hours
-- changed BookingID range from 1-4 to 11-14, because there are already 10 Bookings in my test data set
-- added arbitrary EmployeeID that corresponds to the Employee that handled the booking process
-- noqa: disable=CP03
insert into Bookings (BookingID, Reserved, TableNumber, CustomerID, EmployeeID) values
(11, '2022-10-14 18:00:00', 5, 1, 1),
(12, '2022-11-12 19:30:00', 3, 3, 1),
(13, '2022-10-11 18:45:00', 2, 2, 1),
(14, '2022-10-13 19:15:00', 2, 1, 1);

17
sql/table_is_free.sql Normal file
View File

@@ -0,0 +1,17 @@
-- available bookings - task 2
-- changes:
-- changed BookingDate (Reserved) data type from date to datetime, because I assumed that Little Lemon
-- allows wants bookings per day and table - a table is considered blocked for 2 hours
-- noqa: disable=CP03,LT02,RF03
create function TableIsFree(Reserved datetime, TableNumber int)
returns boolean
deterministic
begin
return (select not exists (
select BookingID
from Bookings as B
where
abs(timestampdiff(second, B.Reserved, Reserved)) < 2 * 60 * 60
and B.TableNumber = TableNumber
));
end