The goal of this exercise is to create a basic web application using Express. You should be able to use basic Mongoose methods to Create, Read, Update, and Delete documents in a collection in the database.
The app will allow a user to keep track of their drones collection. Users should be able to see a list of their existing drones (which you will seed using previously gained knowledge), add new ones to their collection, update them as well as delete them when you don't use them anymore.
- 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.
This project has already a familiar structure: there are bin, models, views, routes, config and public folders, and app.js file with lots of middleware that "glue" different parts of this application to be able to work together as one.
After forking and cloning the project, you will have to install all the dependencies:
$ npm install
To run the app:
$ npm run dev
Now you are ready to start. 🚀
The first step is to create the Drone
model and seed some initial drones in our database.
The Drone
model should have:
name
- String (name of the drone model, like General Atomics MQ-9 Reaper)propellers
- Number (amount of propellers, like 4)maxSpeed
- Number (meters per second for the drone's maximum speed, like 18)
Steps you should follow in this iteration:
- Go to the
Drone.model.js
file in themodels
folder. Use Mongoose schema and make sure that the Drone model has all the properties listed above. Hint: Don't forget to export the Drone model. - In the
seeds/drones.seeds.js
file:- Create an array of 3 objects, each with
name
,propellers
andmaxSpeed
as our initial drones. - Establish a connection to the database. You can use the same code in
db/index.js
. - Once the database connection is established, call the
Drone
model's.create()
method with the array as an argument. - If the
.create()
method successfully creates the drones collection, output (using console.log()) how many drones have been created. In case, the seeding of the database fails, catch the error and output it.
- Create an array of 3 objects, each with
- Run the seed file with
node
to seed your database. - Check if the drones collection is successfully created through the Compass and check the output in the terminal.
Hint 1: In case you are struggling with drones' characteristics, you can use this array in your seed file:
const drones = [
{ name: "Creeper XL 500", propellers: 3, maxSpeed: 12 },
{ name: "Racer 57", propellers: 4, maxSpeed: 20 },
{ name: "Courier 3000i", propellers: 6, maxSpeed: 18 }
];
Hint 2: Don't forget to close the connection with the database after you have seeded the database. You are familiar with mongoose.connection.close()
approach, but you can also check the .disconnect()
Mongoose method. Click here to search through Mongoose docs.
Now that the drones are in the database, you can start working with them. Let's display a list of all the drones.
Here is the route you will be using:
Route | HTTP Verb | Description |
---|---|---|
/drones |
GET | Show all drones |
Steps you should follow in this iteration:
- Find the
/drones
GET route inroutes/drones.js
. - Use the Mongoose
.find()
method to retrieve all the drones. Display all the drones on thedrones/list.hbs
view. Make sure you catch the error and output it to the terminal. - In the
drones/list.hbs
file, use a#each
loop to display tags with each drone'sname
,propellers
, andspeed
. - Add the link that goes to
/drones
route in thelayout.hbs
file to easier navigate to the list of drones.
Now that we have a list of drones, you can focus your attention on saving new drones to the database.
Here are the routes you will be using:
Route | HTTP Verb | Description |
---|---|---|
/drones/create |
GET | Show a form to create a drone |
/drones/create |
POST | Save a drone to the database |
Steps you should follow in this iteration:
- Find the
/drones/create
GET route inroutes/drones.js
and render thedrones/create-form.hbs
view. - The
create-form.hbs
should have the form that will submit on/drones/create
POST route. The form should have all the fields necessary to create a new drone. - Locate the
/drones/create
POST route inroutes/drones.js
and usingreq.body
get all the info user submitted through the form. Use this info to create a new drone in the database in the drones collection. Make sure you redirect to/drones
if the new drone is successfully created. If there is an error, render again the view so the user can try again to create a drone.
You can create and list (read) drones, so it is time to proceed to make sure you can update existing drones.
Here are the routes you will be using:
Route | HTTP Verb | Description |
---|---|---|
/drones/:id/edit |
GET | Show a form to update a drone |
/drones/:id/edit |
POST | Save updated drone to the database |
Steps you should follow in this iteration:
- Under each drone information on the
list.hbs
, add a link to the edit page (pointing to the/drones/<oneDroneId>/edit
). When clicked, the link should take you to the 404 page (still), but the URL should have a format similar to this one:/drones/123Hgt5y4500Ux8/edit
. - Find the
/drones/:id/edit
GET route inroutes/drones.js
and render thedrones/update-form.hbs
view. Make sure you get the right drone from the database using the availableid
(hint:.findById()
) and pass the drone object to the view. - The
update-form.hbs
should have the pre-filled form with the values of the previously passed drone object. - Locate the
/drones/:id/edit
POST route inroutes/drones.js
and usingreq.body
get all the info user submitted through the form. Use this info to update the existing drone in the database. Make sure you redirect to/drones
if the new drone is successfully updated. If there is an error, render again the view so the user can try again to update a drone. (Hint: You can use.findByIdAndUpdate()
Mongoose method.)
You have CRU out of CRUD. It is time to allow users to remove drones they don't need anymore.
Here is the route you will be using:
Route | HTTP Verb | Description |
---|---|---|
/drones/:id/delete |
POST | Delete a specific drone from the database |
Steps you should follow in this iteration:
- Under each drone information on the
list.hbs
, add a small form with action to/drones/<oneDroneId>/delete
and method POST. After clicking on the Delete button, this form should submit to the action route. - Find the
/drones/:id/delete
POST route inroutes/drones.js
and using.findByIdAndDelete()
(or.findByIdAndRemove()
), destroy the document with the given ID from the database.
Our app should be pretty ugly right now if you (correctly) focused on the back-end during this exercise. To be a fully functioning web app, we need to add some styles.
In your layout require bootstrap, and add some very basic styles to make our drones app look "ready" for production.
Happy coding!