17 KiB
Agenda
We will try to cover most of these topics in today's sessions and the remaining in the next.
- Build the Home Page
- Map movies to theaters
- Build the shows components
- Build the seating arrangements
- Book tickets
- Make Payments
- Deploy It is going to be a bit challenging, advanced, but very interesting session covering topics that are asked very frequently in interviews.
So let's start.
Build the Home Page
Whenwever you login to the home page you must see what all movies are playing and then you want to book that particular movie.
But before moving that as we left a part in shows component , basically the working of the delete button.
Asking the question: Have you attempted the homework of making the delete button? No
Lets quickly write the code for the delete button, lets add the route of the "delete-show". Basically on hitting the endpoint "/delete-show", will await the shows model and find by id to delete by getting the showID and then respectively shows the sucess message or the respective error.
For this route, will go to the client and make the axios instance for this to delete-movies. So now will be writing the axios instance where we will bw hitting on the route "/api/theatres/delete-show" and sending the id in the payload.
Now will be writing the handledelete method that will be accepting the id and will be calling the deleteshow and id will taken as payload and the getdata()
gets called or else the error message. and just put this handledelete in the onClick.
Now just demonstarate, by inserting the show name, date, time, movie, ticket_price, and total_seats. Now will be trying the delete button.
Lets start with home page,
Start by Discussing the Wireframe of The Homepage
- In the home page, will be search part for searching the movies name.
- Then will be adding the posters of the different movies which will be clickable that goes to the some another page.
- On the other page , that will have the details of the movies and below that there will details of the theaters and in the nother space there will be date picker. The available theaters with the selected movie and date/time will be shown with name and time.
- When we will click on the theaters components, that will consist of the seating arrangements and then the users will be able to select the particular seat.
- Then after clicking on the Book Ticket button which will go to the another page that will show the movies details with seat, amount, and the make payment button.
- On clicking the make_payment button that will take us to the stripe portal to make the payments.
Let's start changing the index.js file as follows:
First of all add the input space for searching the movie name and give classname as search-input.
- Add the height and border-radius to the search-input to give some styling.
- Now as you see in the mongodb in the movies we are having the information of the movies stored.
- To get the details of the movies to add them in the cards that will be done using the get_all_movies route.
- To start building the card, lets divide it int row and col using antd.
- In the row, there will be movies title, seach space and the column properties.
- Now will bw creating the two states one for the movies and other for the searchText using useState.
- Define the method of getdata for the getting all the movies by using loader and dispatch by useDispatch.
- To call the getData() method , use the useEffect and will get all the movies. Also import the HideLoading and ShowLoading from the loaderSlice. Also the message from the antd.
- For the searchText , will be adding the onChange on the event that will use the setSearchText method with the event's target value. And basically the search for the particular text by after converting to the lower case is done with the movies title.
Mapping of the Theater and Shows
As there are multiple theaters, with multiple shows and multile movies, then how to map the correct movies details, lets discuss this:
For example in the shows at PVR Superplex having the show of the movie Captain America. Let's suppose they are having shows of the movies at 3 PM and 9PM so are they will be displayed two times.
Do they show they show the PVR Superplex haing two shows one at the 3pm and the other at 9pm. Or do they show like PVR Superplex two times with differently. The first one is the better approach like in the below image.
Like one theater one time, and then we can add on the shows of different time as above.
We have to use the route parameter for the different routes for the different movies by sending the movies id.
Lets import the useNavigate from the react-router-dom, use this in the navigate method where we are changing the movie_id. Also import moment.
Hence the route changes as we click on the movie as seen in the route we got the movie id along with the date. Same as we observer on the different websites like amazon.
So now we have to show the movies details along with the show details let start,
- We have to take a another component, names as TheatreForMovies.
- In the app.js whenever there will be route of type movie with id, then we will go to this component of theatresForMovies.
- First of all we have to just build the movies_Details and then the date picker.
- Using the movie id we can get all the data using the function
getData()
.
title: Map Movies to the theaters description: Explanation of all the related components and the routes for the mapping page of the movies with theaters. duration: 2100 card_type: cue_card
Map Movies to the theaters
Get a movie by ID route Lets create the route for this, go to the movie-route.js by just passing the id, so by simply attaching the movie_id with the axiosInstance we can get the id using the route and api call.
Here's a explanation of the code:
-
Imports:
- The code imports necessary modules and functions from various libraries such as Ant Design (
antd
), React (react
), React Router (react-router-dom
), and others.
- The code imports necessary modules and functions from various libraries such as Ant Design (
-
React Hooks:
- The
useParams
anduseNavigate
hooks are used from React Router to access the URL parameters and navigate between routes, respectively. - The
useState
anduseEffect
hooks are used to manage component state and perform side effects when the component mounts.
- The
-
Redux Dispatch:
- The
useDispatch
hook is used to get the Redux dispatch function, which will be used later to dispatch actions.
- The
-
Data Fetching with getData Function:
- The
getData
function is defined as an asynchronous function. It is responsible for fetching movie data by its ID from an API using theGetMovieById
function from theapicalls/movies
module. - While fetching data, a loading spinner is displayed using
dispatch(ShowLoading())
. - After receiving a response, it checks if the response was successful. If successful, it sets the movie data and displays a success message; otherwise, it displays an error message.
- Finally, it hides the loading spinner using
dispatch(HideLoading())
.
- The
-
Render Movie Information:
- The movie information is displayed in JSX within a conditional rendering block (
movie && ...
) to ensure that the component renders only whenmovie
data is available. - It displays various details about the movie, such as title, language, duration, release date, and genre. The
moment
library is used to format the release date.
- The movie information is displayed in JSX within a conditional rendering block (
Addition of the date picker using moment:
- The component will render a section that includes a label "Select Date" and a date picker input field.
- The date picker will have a minimum date restriction set to the current date. Users won't be able to select a date earlier than today's date.
- When a user interacts with the date picker and selects a new date, an onChange event will be triggered.
- Additionally, the code uses the navigate function (likely from React Router) to navigate to a new URL. This new URL includes query parameters with the movie ID (params.id) and the selected date. For example, if the user selects a date of "2023-09-20" for a movie with an ID of "123," the URL might become something like "
/movie/123?date=2023-09-20
." - The change in the URL, including the selected date as a query parameter, can trigger a different route in your application or update the content displayed on the page.
- This enables your application to react to the selected date and possibly show movie-related information for the chosen date.
Now start creating threaters and shows running on them:
Lets start by getting the theater, using the id of the theater for example if the bruce having two 2 theaters with JAWAN movies and the Maek has 1 theater with Captain America then if the user selecting the JAWAN movies how many theater should be given?
Asking Question So exactly the 2 theaters should be given which are havin the movie JAWAN.
Lets get all the unique theaters which have the shows of a movie
It's an Express.js route handler that aims to retrieve all unique theaters that have shows of a specific movie on a given date. Here's the explanation:
-
Route Definition:
- This code defines an Express.js POST route at the endpoint
/get-all-theatres-by-movie
. It expects the request to be authenticated using theauthMiddleware
middleware.
- This code defines an Express.js POST route at the endpoint
-
Request Parsing:
- In the route handler, it extracts the
movie
anddate
parameters from the request body using destructuring:const { movie, date } = req.body;
.
- In the route handler, it extracts the
-
Fetching Shows:
- It then queries the database to find all the shows of a specific movie on the given date using the
Show
model. Thefind
method is used with themovie
anddate
parameters. - Additionally, it uses
.populate('theatre')
to populate thetheatre
field in theshows
collection with corresponding theater information.
- It then queries the database to find all the shows of a specific movie on the given date using the
-
Finding Unique Theaters:
- The code initializes an empty array called
uniqueTheatre
to store unique theater data.
- The code initializes an empty array called
-
Sending Response:
- The route handler sends a JSON response to the client, indicating that the request was successful (
success: true
). - The
message
field provides a success message, stating "Unique Data Fetched." - The
data
field in the response is set to theuniqueTheatre
array, which contains the unique theater information.
- The route handler sends a JSON response to the client, indicating that the request was successful (
-
Error Handling:
- In case an error occurs during the process (e.g., a database query error), the catch block is executed. It sends a JSON response indicating the failure (
success: false
) and includes an error message in themessage
field.
- In case an error occurs during the process (e.g., a database query error), the catch block is executed. It sends a JSON response indicating the failure (
The provided code is a JavaScript function that exports a function named GetTheatresByMovie
. This function is likely used to make an HTTP POST request to a specific API endpoint in order to get theaters that are showing a particular movie on a given date. Here's an explanation of the code:
-
export const GetTheatresByMovie = async (payload) => { ... }:
- This line exports a function called
GetTheatresByMovie
as a named export from the module. - The function is defined as an asynchronous function, indicating that it can perform asynchronous operations, such as making HTTP requests.
- This line exports a function called
-
try { ... } catch (err) { ... }:
- The function is wrapped in a try-catch block, which is used for error handling. It attempts to perform the specified operation within the try block and catches any errors that may occur.
-
const response = await axiosInstance.post('/api/theatres/get-all-theatres-by-movie', payload);:
- Inside the try block, the code uses the
axiosInstance
(presumably an Axios instance configured for making HTTP requests) to send an HTTP POST request to the/api/theatres/get-all-theatres-by-movie
endpoint. - The
payload
variable is expected to contain the data to be sent as the request body. This likely includes information about the movie and the date.
- Inside the try block, the code uses the
-
return response.data;:
- If the HTTP request is successful (i.e., the server responds with a valid JSON response), the function returns the
data
property of the response. This typically contains the data returned by the server, such as theater information.
- If the HTTP request is successful (i.e., the server responds with a valid JSON response), the function returns the
-
return err.response:
- If an error occurs during the HTTP request (e.g., a network error or a server error), the catch block returns the
response
property of theerr
object.
- If an error occurs during the HTTP request (e.g., a network error or a server error), the catch block returns the
The provided code appears to be a JavaScript function named getTheatres
. This function is designed to fetch theater information related to a specific movie and date, similar to the previous code you shared. Here's an explanation of this code:
-
const getTheatres = async () => { ... }:
- This function is defined as an asynchronous function and does not take any arguments.
-
try { ... } catch (err) { ... }:
- The function is wrapped in a try-catch block for error handling. It attempts to perform the specified operations within the try block and catches any errors that may occur.
-
dispatch(ShowLoading());:
- Before making the API request, it dispatches an action (likely a Redux action) to show a loading spinner or loading indicator, indicating that data is being fetched.
-
const response = await GetTheatresByMovie({ date, movie: params.id });:
- Inside the try block, it calls the
GetTheatresByMovie
function, passing an object as the payload. This object includes thedate
andmovie
parameters required for the API request.params.id
is presumably the movie ID. - The function is awaited, meaning it will wait for the HTTP request to complete and return a response.
- Inside the try block, it calls the
-
console.log(response.data);:
- It logs the
data
property of the response to the console. This typically contains the data returned by the server, which in this case, should be theater information related to the specified movie and date.
- It logs the
-
Response Handling:
- If the response indicates success (
response.success
istrue
), it updates the component state with the theater data usingsetTheatres(response.data)
and displays a success message usingmessage.success(response.message)
. - If the response indicates failure (
response.success
isfalse
), it displays an error message usingmessage.error(response.message)
.
- If the response indicates success (
-
dispatch(HideLoading());:
- Finally, it dispatches an action (likely a Redux action) to hide the loading spinner or loading indicator, indicating that the data fetching process has completed.
-
Error Handling:
- In case an error occurs during the API request (e.g., network error or server error), the catch block is executed. It hides the loading indicator, and it displays an error message using
message.error(err.message)
.
- In case an error occurs during the API request (e.g., network error or server error), the catch block is executed. It hides the loading indicator, and it displays an error message using
Code for the Unique Theaters:
Route handler for fetching all unique theaters that have shows of a specific movie on a given date. Here's an explanation:
-
Route Definition:
- This code defines an Express.js POST route at the endpoint
/get-all-theatres-by-movie
. It expects the request to be authenticated using theauthMiddleware
middleware.
- This code defines an Express.js POST route at the endpoint
-
Request Parsing:
- In the route handler, it extracts the
movie
anddate
parameters from the request body using destructuring:const { movie, date } = req.body;
.
- In the route handler, it extracts the
-
Fetching Shows:
- It then queries the database (likely using an ORM like Mongoose) to find all the shows of a specific movie on the given date. The
Show
model is used to query the shows, and thepopulate
method is used to retrieve associated theater information for each show:const shows = await Show.find({ movie, date }).populate('theatre');
.
- It then queries the database (likely using an ORM like Mongoose) to find all the shows of a specific movie on the given date. The
-
Finding Unique Theaters:
-
The code initializes an empty array called
uniqueTheatre
to store unique theater data. -
It iterates through each
show
in theshows
array using aforEach
loop. -
For each
show
, it checks if there is already a theater with the same_id
in theuniqueTheatre
array. -
If no matching theater is found (
!theatre
), it filters all shows with the same theater_id
and pushes a new object into theuniqueTheatre
array. This object combines the theater data with an array of shows for that theater.
-
-
Response:
- Finally, the route handler sends a response to the client. If successful, it sends a JSON response with
success
, a success message, and theuniqueTheatre
array containing theater information and associated shows.
- Finally, the route handler sends a response to the client. If successful, it sends a JSON response with
-
Error Handling:
- In case of an error during the process (e.g., database query error), it sends a JSON response indicating the failure with an error message.
This route essentially retrieves all theaters that are showing a particular movie on a specific date, eliminating duplicates, and presents the data in a structured format, making it easier for clients to work with unique theater information and associated show details.