Today you are going to take an app that "works" without a database, and integrate a database into it.
Remember the Ticket Booker? 😬
Currently, the backend is generating the seats for the theater. Let's migrate that to a database: MongoDB.
yarn install
to get all of the dependencies.
yarn start
to start both the server and the FE app. You don't need to run 2 terminals for this.
Get the seats into a database > collection. Each seat
should be a document in the collection.
You could use a util function like the one you created in the last workshop...
Keep in mind, that under normal circumstances, the FE should not be affected by any changes you make here. Meaning it shouldn't break because you change the code in the backend. Be mindful of what the FE expects as data as well as how you are storing it in the database.
Don't forget to provide an _id
to each document. You could do that before getting rid of the code that currently generates the seats. Each seat should look something like this.
{
_id: 'A-1',
price: 225,
isBooked: false
}
Once you've migrated the data, it's time to get rid of the code that is generating the seats, and write a function that queries the database to retrieve all of the seats.
Inside of handlers.js
, there is an empty function called getSeats
that should replace the anonymous function that is called at the /api/seat-availability
endpoint. Don't forget to require it.
In handlers.js
, write a function that will handle booking the seat. It needs to update the database.
While you're at it, remove the anonymous function that is called at /api/book-seat
and create a proper function in the handlers.js
file.
If you verify the data that is being sent to the server when a user buys a ticket, you will notice that there is some data that we are currently not handling: fullName
and email
.
What should we do with that?
There are two options that come to mind:
- Create a new collection that contains all of the user documents, along with a reference to the seat they booked.
- Add that data to the seat document directly.
Both are viable and it really depends on what will be done with the data. It's totally up to you!
🟡 - Minimally complete workshop (75%) - 🟡
There is talk of creating an admin panel to manage the bookings. The FE is not built yet, but it would be good to enable the required functionality now in the BE.
Administrators would like to be able to
- view the bookings. that's already done (exercise 1)
- delete a booking, i.e. make the seat available for purchase once more.
- update the name or email of customer.
You will need to test this implementation with Insomnia.
You could always try to improve the FE as well. There some benefit/difficulty in adding features to code you didn't write...
🟢 - Complete workshop (100%) - 🟢