Everyone likes celebrities, right? Well, even if you don't, now is your chance to create your own, better, fictional celebrities! Let's create an Express app with all the basic CRUD actions that will allow the user to create their own celebrities and edit them as they see fit.
The user should be able to:
1. See the list of celebrities
2. See the details of a celebrity
3. Add new celebrities
4. Update existing celebrities
5. Delete celebrities
But wait! That's not all!
Once we have our celebrities, we need something for them to do!
Let's make up some movie ideas for our celebrities to star in.
That means we'll need all the basic CRUD actions for movies as well.
The user should be able to:
6. See the list of movies.
7. See the details of a movie.
8. Add new movies.
9. Update existing movies.
10. Delete movies.\
- Fork this repo
- Clone this repo
- Upon completion, run the following commands:
$ git add .
$ git commit -m "done"
$ git push origin master
- Create Pull Request so your TAs can check up your work.
After forking and cloning the project, you will have to add a .env file:
PORT=3000
And you have to install all the dependencies:
$ npm installNow you are ready to start 🚀
Once we have generated our Express app, our first step is to create the Celebrity model and seed some initial celebrities in our database.
The Celebrity model should have:
name- String (like Tom Cruise, Beyonce, Daffy Duck, etc.)occupation- String (what the celebrity does, why they are famous. For example actor, singer, comedian, or you can put unknown if your celebrity is someone like Kim Kardashian)catchPhrase- String (every celebrity needs a good catch phrase. Well maybe not all of them have one in real life, but all of our celebrities will have a catch phrase. This can be pretty much anything)
- Create the
celebrity.jsmodel file in themodels/folder. - In the
celebrity.jsmodel file:- Create the
Celebritymodel with the schema. - Create the celebrity schema with
name,occupationandcatchPhrase. - Export the
Celebritymodel.
- Create the
- Create the
seeds.jsfile in thebin/folder. - In
seeds.jsfile:- Create an array of 3 objects, each with
name,occupationandcatchPhrasefor our initial celebrities. - Call the
Celebritymodel'screatemethod with the array as argument. - In the
createmethod's callback, display feedback.
- Create an array of 3 objects, each with
- Run the seed file with
nodeto seed your database. - Check the database with the
mongocommand to confirm that your data was saved.
Now that we've got some celebrities in the database, we can start working with them in our Express app. Let's display a list of all the celebrities.
Here's the route we will be using:
| Route | HTTP Verb | Description |
|---|---|---|
/celebrities |
GET | Show all celebrities |
- Locate the
/celebritiesGET route inroutes/celebrities.js. - In the route callback:
- Call the
Celebritymodel'sfindmethod to retrieve all the celebrities. - If there's an error, call the route's
nextfunction and return the error. - If there isn't an error, render the
celebrities/indexview. - Pass the array of celebrities into the view as a variable.
- Call the
- Create the
celebrities/folder insideviews/. - Create the
index.hbsview file inside theviews/celebrities/folder. - In the
views/celebrities/index.hbsview file:- Add an
<h2>tag for the page's heading. - Use a
forEachloop to display tags with each celebrity'sname.
- Add an
- In the
views/index.hbs(homepage) file:- Add a link that goes to the
/celebritiesroute.
- Add a link that goes to the
We've got a list of celebrities that displays each of their name, but what if we want to see the other details? In our views/celebrities/index.hbs view with our list of celebrities, let's add links so that the user can click on any celebrity's name, and go to a page specifically for that celebrity. On this page, we will show all the details of that celebrity.
Here's the route we will be using:
| Route | HTTP Verb | Description |
|---|---|---|
/celebrities/:id |
GET | Show a specific celebrity |
- Create the
/celebrities/:idGET route inroutes/celebrities.js. - In the route callback:
- Call the
Celebritymodel'sfindOneorfindByIdmethod to retrieve the details of a specific celebrity by its id. - If there's an error, call the route's
nextfunction and return the error. - If there isn't an error, render the
celebrities/showview. - Pass the variable with the celebrity's details into the view.
- Call the
- Create the
show.hbsview file inside theviews/celebrities/folder. - In the
views/celebrities/show.hbsview file:- Add an
<h2>for the page's heading. - Display tags with the celebrity's
name,occupationandcatchPhrase.
- Add an
- In the
views/celebrities/index.hbsview file:- As part of the loop that displays each celebrity's name, add a link that goes to the
/celebrities/:idroute with the:idreplaced by the actual celebrity's id.
- As part of the loop that displays each celebrity's name, add a link that goes to the
Now that we have a list of celebrities, as well as a personalized details page for each celebrity, let's make it so the user can add new celebrities to the database
| Route | HTTP Verb | Description |
|---|---|---|
/celebrities/new |
GET | Show a form to create a celebrity |
/celebrities |
POST | Send the data from the form to this route to create the celebrity and save to the database |
- Locate the
/celebrities/newGET route inroutes/celebrities.js: - In that route's callback:
- Render the
celebrities/newview.
- Create the
new.hbsview file inside theviews/celebritiesfolder - In the
views/celebrities/new.hbsview file:- Add an
<h2>for the page's heading. - Add a
<form>tag that makes a POST request to/celebrities. - Add
<input>tags inside the form so the user can fill in values for each attribute of the celebrity. Make an input forname,occupation, andcatchPhrase - Add a
<button>tag in the form so the user can submit the form once they are done filling it out.
- Add an
- Locate the
/celebritiespost route inroutes/celebrities.js. - In that route's callback:
- Create an object with keys for
name,occupation, andcatchPhrase. - Values for those keys should come from the form (
req.bodyis the object full of the values from the form) - Create an instance of the
Celebritymodel with the object you made in the previous step - Call the
savemethod to save the new celebrity to the database - If there is an error, render the
celebrities/newview so the user can try again. - If there is no error, redirect to the page with the list of celebrities
- Create an object with keys for
- In the
views/celebrities/index.hbsview file:- Add a link that goes to the page you just created with the form to create a new celebrity.
Now that we have a list of celebrities, a celebrity details page, and a page to create new celebrities, we only have 2 features left to implement: editing celebrities and deleting them. Since deleting is simpler, let's start with that.
| Route | HTTP Verb | Description |
|---|---|---|
/celebrities/:id/delete |
POST | Delete a specific celebrity |
- In the
views/celebrities/index.hbsfile:- As part of the loop, add a
<form>tag that makes a POST request tocelebrities/:id/deletewhere the:idis replaced by the actual ID of each celebrity. - Add a
<button>tag inside the form so that it can be submitted.
- As part of the loop, add a
- Create the
/celebrities/:id/deletePOST route in yourroutes/celebrities.jsfile - In that route's callback:
- Use the
Celebritymodel'sfindByIdAndRemovemethod to delete the celebrity by its ID. - If there's an error, call the route's
nextfunction and return the error - If there is no error, redirect to the list of celebrities page.
- Use the
Final piece of our CRUD puzzle: editing existing celebrities.
Here are the routes we will be using:
| Route | HTTP Verb | Description |
|---|---|---|
/celebrities/:id/edit |
GET | Show a form to edit a celebrity |
/celebrities/:id |
POST | Send the data from the form to this route to update and save the celebrity from the database |
- Create the
/celebrities/:id/editGET route inroutes/celebrities.js. - In that route's callback:
- Call the
Celebritymodel’sfindOneorfindByIdmethod to retrieve a specific celebrity by its id. - If there's an error, call the route's
nextfunction and return the error. - If there isn't an error, render the
celebrities/editview. - Pass the variable with the celebrity’s details into the view.
- Call the
- Create the
edit.hbsview file inside theviews/celebrities/folder. - In the
views/celebrities/edit.hbsview file:- Add an
<h2>tag for the page's heading. - Add a
<form>tag that makes a POST request to/celebrities/:idwith the:idreplaced by the actual celebrity's id. - Add
<input>tags inside the form for each attirbute of the celebrity.- Bonus: When you render the edit form, make sure each of the input fields is pre-filled with the current value of the attribute for that celebrity
- Add a
<button>tag inside the form so that the user can submit the form once they are done editing.
- Add an
- Locate the
/celebrities/:idPOST route in theroutes/celebrities.jsfile. - In that route's callback:
- Create an object with keys for each attribute of a celebrity (celebrity has 3 attributes. What were they again? Look back and review if you forgot.)
- Values for those keys should come from the form submission (
req.body). - Call the
Celebritymodel’supdatemethod and use the celebrity's id to specify which celebrity we are updating. Also, use the object you just created with the updated attributes for the celebrity and pass this object into theupdatemethod as the second argument. - If there is an error retrieving that celebrity, call the route's
nextfunction and return the error - If there is no error, redirect back to the list of celebrities.
At this point, we have implemented all the basic CRUD actions for the Celebrity model in our app. Nice work!
Now that we've done all this good work, it's time to do it all over again, but for the Movie model. After all, what's the point of having all these celebrities if we can't make up fake movies to cast them in?
We are going to create a Movie model and implement all the same CRUD actions for this model as well. Don't worry, it's really much easier the second time around.
Now when we've started all this good work, let's keep up strong and build all the routes for the Movie model. But first, let's create the Movie model.
The Movie model should have:
title- Stringgenre- Stringplot- Stringcast- Array of object IDs referencing the celebrity model (basically, the array of celebrities' IDs)
Go back and review what you did to create the celebrity model. You'll need to create a file for the model, and in that file, you'll need to create a schema for the model as well. Don't forget, you have to module.exports the Movie model.
Okay, the next step is to make it so the user can add new movies to the database.
| Route | HTTP Verb | Description |
|---|---|---|
/movies/new |
GET | Show a form to create a movie |
/movies |
POST | Send the data from the form to this route to create the movie and save it to the database |
Review how you did this for the celebrity model.
- Create 2 new routes, one to render page with the form on it, and one to send the data to after the form is filled out
- In the GET route that displays the form to create a new movie (which renders the
movies/newview), make sure you pass all the celebrities from your database so your users can choose which ones are in the cast of the movie you're just creating (hint: You will have to use select multiple tag)
- In the GET route that displays the form to create a new movie (which renders the
- Make sure the form is making a POST request to the other route you just created (
/movies) - In your post route, create an object with all the info you just received from the form. (Remember,
req.body) - Use this object to create a new movie in the database and redirect back to the page with your list of all movies
- Make sure to add a link to the form on the movies index page so the user can easier navigate
Now that we've got some movies in the database, let's make a page where we list all our movies, just like we did with the celebrity model.
Here's the route we will be using:
| Route | HTTP Verb | Description |
|---|---|---|
/movies |
GET | Show all movies |
Go back and review how you did this for the celebrity model. You'll need to:
- Create a GET route that will render the file in which we will display movies (
movies/indexview) - Use a database query to retrieve all the movies from your database and render the view
- Use a
{{#each}}loop to display all your movie titles on that page - Add a link to the page you just created on the home page so the user can navigate to it.
We've got a list of all movies that displays each of their titles, but what if we want to see the other details? In our movies/index view with our list of movies, let's add links so that the user can click on any movie's title, and go to a details page of each movie. On this page, we will show all the details of that movie.
Here's the route we will be using:
| Route | HTTP Verb | Description |
|---|---|---|
/movies/:id |
GET | Show a specific movie |
- We need
/:idpart to change dynamically as we click on different movies' titles. This being said, as part of the loop that displays each movie's title, add a link that goes to the/movies/:idroute with the:idreplaced by the actual movie's id 🔑 - Create the
/movies/:idGET route inroutes/movies.js. - In the route:
- On the
Moviemodel callfindOne()orfindById()method to retrieve the details of a specific movie by its ID- Don't forget you have
castas the array of celebrity IDs, and we need topopulate()in order to get the full data about the celebrities 🎯
- Don't forget you have
- If everything is fine (.then()), render the
movies/showview and pass the variable with the movie's details into the view - If there's an error, catch it.
- On the
- In the
views/movies/show.hbsview file:- Add an
<h2>for the page's heading. - Display tags with the movie's
title,genre,plotandcast.
- Add an
Now that we have a list of movies, a movie details page, and a page to create new movies, we only have 2 features left to implement: editing celebrities and deleting them. Since deleting is simpler, let's start with that.
| Route | HTTP Verb | Description |
|---|---|---|
/movies/:id/delete |
POST | Delete a specific movie |
- In the
views/movies/show.hbsfile:- Add a
<form>tag that makes a POST request to/movies/:id/deletewhere the:idis replaced by the actual ID of the movie. - Add a
<button>tag inside the form so that it can be submitted.
- Add a
- Create the
/movies/:id/deletePOST route in yourroutes/movies.jsfile - In the route:
- Use the
Moviemodel'sfindByIdAndRemove()method to delete the specific movie by its ID. - If everything is good (
.then()), redirect to the list of movies page - If there's an error, catch it
- Use the
Final piece of our CRUD puzzle: editing existing movies.
Here are the routes we will be using:
| Route | HTTP Verb | Description |
|---|---|---|
/movies/:id/edit |
GET | Show a form to edit a movie |
/movies/:id |
POST | Send the data from the form to this route to update the specific movie |
- Create the
/:id/editGET route inroutes/movies.js. - In that route:
- Call the
Moviemodel’sfindOne()orfindById()method to retrieve a specific movie by its ID - If everything is good, render the
movies/editview - Pass the variable with the movie's details into the view
- Call the
- In the
views/movies/edit.hbsview file:- Add an
<h2>tag for the page's heading. - Add a
<form>tag that makes a POST request to/movies/:idwith the:idreplaced by the actual movie's ID. - Add
<input>tags inside the form for each attribute of the movie.- Hint: When you render the edit form, make sure each of the input fields is pre-filled with the current value of the attribute for that movie
- Add a
<button>tag inside the form so that the user can submit the form once they are done editing. - BONUS: Make the current cast members selected so the user knows who is in the cast currently.
- Add an
- Create
/movies/:idPOST route in theroutes/movies.jsfile - In that route:
- Create an object with
Moviemodel keys and it's values should come from the form submission (which isreq.body) - Now you can apply different methods -
update()orfindByIdAndUpdate()to find the movie and send the updated values to the database. - If there is no error, redirect back to the movie details page.
- Create an object with
Now you can come back to the bonus part related to the celebrity model 😉.
That's it! 🏆
Happy Coding! ❤️
