Using what you have learned about Express, Express Routing, RESTful routing, CRUD operations, and Mongoose, use the Mongoose ODM to implement full CRUD functionality on an API that has data relationships that are bouth 1:M and N:M.
How we make it work:
- we will build an express server that makes mongoose CRUD requests to a Mongo db.
There will be 3 models in total: Movies
, Ratings
and CastMembers
.
1 Movie can have many Ratings (1:M
), because you cannot review more that one film at once, and many Movies
can may many Castmembers
(N:M
) because actors can appear in many film roles.
This deliverable is broken up into three parts.
- In part one, you will create an express app and the
Movie
model, with no relattions. This part will need amovies
controller -
- In the seond part, you will create a rating
Schema
andratings
routes. You can accomplish this part with either mongo refs or embedded documents.
- In the seond part, you will create a rating
- The third part covers Many to Many, and you will create a
CastMember
schema andcastMember
controller for your express app.
Creating the API
- The model we will be using is movies, so we will be making a movie API.
Movie Schema:
column name | type |
---|---|
title | string |
description | string |
- Your API should be accessible via five routes:
Example API Routes:
Method | Action | URL | Functionality |
---|---|---|---|
GET | index | /movie | list all movies |
POST | create | /movie | add a new movie |
GET | detail/show | /movie/:id | show one movie |
PUT / PATCH | update | /movie/:id | update one movie |
DELETE | delete | /movie/:id | delete one movie |
You will need to:
- Fork and clone this repository!
- Run
npm init
to start your node project. - Create a
.gitignore
and add everything to ignore - Create your Express App
- Add Mongoose functionality!
Recommended Workflow:
- Stub out your routes. For the purposes of initial declaration,
res.send("test")
to make sure you're hitting them. - Link your model to your server.
- Update your routes to "do something" and make the magic happen!
Adding ratings to your Movie API.
Movies have a 1:M relationship with ratings -- one Movie can have many ratings
- We will be creating another mongoose model for ratings, it will be an embedded in the Movie. Movie ratings can have 0 - 10 stars and a comment.
Rating Schema:
column name | type |
---|---|
stars | integer |
comment | string |
Movie Schema:
column name | type |
---|---|
_id | integer |
title | string |
description | string |
ratings | [Rating] |
- Your API needs these routes either updated or added:
Example API Routes:
Method | Action | URL | Functionality |
---|---|---|---|
GET | index | /movie | list all movies and incldue thier ratings |
POST | create | /movie/:id/rating or /movie/:movieId/rating | add a new rating to a movie |
PUT / PATCH | update | /rating/:id | update one rating |
DELETE | delete | /rating/:id | delete one rating |
You will need to:
- Create an embedded document in the Movies model.
- Add routes to your express app to interact with the embedded model.
Recommended Workflow:
- Embed your Ratings model in your Movies model.
- Stub out your routes. For the purposes of initial declaration,
res.send("test")
to make sure you're hitting them. - Update your routes to "do something" and make the magic happen!
- All content is licensed under a CC-BY-NC-SA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].