This is a personal project in which it is implemented a REST API service, written in Javascript for Node.js, Express.js framework, MongoDB and Docker containers in order to make seats reservations.
Firsly, you have to install docker and docker-compose in your machine.
Next you have to install the dependencies i the project directory by typing:
sudo npm install
When installation finished, you can run the project through docker-compose inside the project directory:
sudo docker-compose up --build
You cain always check your docker containers status by typing:
sudo docker ps -a
To complete the user registration your should make a post request at the localhost:4001/auth/register with body:
{
"email": "[email protected]",
"password": "your-password",
"username": "your-username",
"profile": {
"firtName": "firstname",
"lastName": "lastname"
}
}The API will respond with the succesful message, a JWT and with the user profile as we can see below:
{
"message": "User successfully created!",
"token": "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MGM4ZWQxOTg4ZTg3NDAwMzNkMmJlZjciLCJ1c2VybmFtZSI6InhpcnN0b3MiLCJlbWFpbCI6InhyaXN0b3NAZ21haWwuY29tIiwicm9sZSI6Ik1lbWJlciIsImlhdCI6MTYyMzc4MDYzMywiZXhwIjoxNjU1MzE2NjMzfQ.mZ3p5krfTBjrdQ1OT_7PZ51jM25FoH1m_4u8B4sr9F0",
"user": {
"_id": "60c8ed1988e8740033d2bef7",
"username": "your-username",
"email": "[email protected]",
"role": "Member"
}
}To make a user login you should make a post request at the localhost:4001/auth/login with the body:
{
"email": "[email protected]",
"password": "your-password"
}The API in this case will repond with the same answer as we saw in the registration but with a different success message:
{
"message": "Login successful",
"token": "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MGM4ZmQ0YmVhZGRhZDAwMjA2ZDcwMTciLCJ1c2VybmFtZSI6InRoZW9sb2dvdSIsImVtYWlsIjoidGhlb2xvZ291QGdtYWlsLmNvbSIsInJvbGUiOiJNZW1iZXIiLCJpYXQiOjE2MjM3OTI0NzcsImV4cCI6MTY1NTMyODQ3N30.XKbNU2DgOjyiqh67gsWPVciPWFh2kfcfXlAoP_DkQYI",
"user": {
"_id": "60c8fd4beaddad00206d7017",
"username": "your-username",
"email": "[email protected]",
"role": "Member"
}
}This is a reservation-based schema where the user can pick their own seats in a day session, and the seats are reserved until the user either checks out the cart or the cart expires.
| Schema Attribute | Description |
|---|---|
| name | Name of the shop |
| price | The reservation price |
| seatsAvailable | The number of seats left for that day session |
| seats | The 2d array seat map, showing which seats have been booked |
| reservations | Any current reservations for this session that are in a cart |
The most interesting field is the seats field, which contains the map of the shop/restaurant. It’s worth noticing that although we have picked a completely uniform seat layout, this schema would allow us to create any layout we wish.
Making a GET request in the endpoint localhost:4001/sessions we can get the history of all sessions and the API respose will be:
{
"_id": "60c91ec72c1cfa0020df364a",
"name": "Shop-Restaurant",
"price": 9,
"seatsAvailable": 113,
"seats": [
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
],
"reservations": [],
"createdAt": "2021-06-15T21:42:31.171Z",
"updatedAt": "2021-06-15T21:42:31.171Z",
"__v": 0
}We can also fetch sessions by date and Id:
- Date -> localhost:4001/sessions?date=2020-07-19
- Id -> localhost:4002/sessions/5f22cfbf5f8e9f0f761699bc
Notice that the seats field mirrors the seating layout. In a session, it’s used to keep track of what seats have been reserved. The reservations array is an array of current reservations for these sessions before the carts have been checked out.
The shopping cart is fairly straight forward. Let’s look at an example cart with a single reservation.
{
"state": "inactive",
"_id": "60c8fd4beaddad00206d7018",
"owner": "60c8fd4beaddad00206d7017",
"total": 0,
"reservations": [ {
"sessionId" : ObjectId("5500632d2dc02be024ba5c66"),
"seats" : [ [ 1, 5 ], [ 1, 6 ], [ 1, 7 ] ],
"price" : 9,
"total" : 27,
} ],
"createdAt": "2021-06-15T19:19:39.362Z",
"updatedAt": "2021-06-15T19:19:39.362Z",
"__v": 0
}| Schema Attribute | Description |
|---|---|
| state | The current state of the cart, one of active, inactive |
| owner | The user who owns that cart |
| total | The total price of the cart |
| reservations | An array of reservation documents for sessions. Each document includes the sessionId, the seats reserved, the price per seat and the total for that reservation |
The most interesting field here is the reservations field, which contains all the current reservations for the cart. Each reservation is for a specific daysession .