Objective:
Build a simple RESTful API using Node.js, Express.js, and MongoDB to manage a movie reservation system.
Requirements:
-
Movie Listing Endpoint An API endpoint to retrieve a list of available movies. Each movie should have a title and a list of available time slots with their capacities.
-
Check Availability Endpoint An API endpoint to check the availability of a specific time slot for a movie. The endpoint should take the movie ID and time slot ID as parameters and return the availability (remaining capacity) for that time slot.
-
Reserve Time Slot Endpoint An API endpoint to reserve a time slot for a movie. The endpoint should take the movie ID, time slot ID, and the number of people to reserve for.
-
Ensure that the reservation does not exceed the available capacity of the time slot.
-
Update the booked count for the reserved time slot.
- Install Node js
- Install the MongoDB community edition or open an account in MongoDB Atlas > create a free tier db and get the connection string
- install dependencies, go to the root folder, and type
npm install
- Create a
.env
file at the root directory and add the URI to your mongo db instance
DB_URI =
Run the server with npm run start
You will see the following in the console:
::: server started on port 8000 :::
::: Connected to the database successfully :::
| for ease of use, I made a seed
function that will drop the collection and instantiate new Data each time the server starts
curl --location 'localhost:8000/v1/movies/'
Get the movie id
and time slot id
from the List All Movies
endpoint
curl --location 'localhost:8000/v1/movies/<movie id>/timeslots/<time slot id>/availability'
Get the movie id
and time slot id
from the List All Movies
endpoint
curl --location --request PUT 'localhost:8000/v1/movies/<movie id>/timeslots/<time slot id>/reserve' \
--header 'Content-Type: application/json' \
--data '{
"numberOfPeople": 10
}'
npm run test