- Create a MONGODB_URL variable in the .env file.
- Install all requirements.
- Inspect the projects models and api directory.
- Read the task fully.
- Import the
Relations Course Task.postman_collection.json
into Postman to help speed testing along.
A course has many lectures. E.g. A course like React
, has many lectures
dedicated for many topics, like useState
, react-query
, react-router-dom
. So we will try to create this type relationship by doing the following:
- Setup
- Create a course and a lecture using postman.
- Create a relation between the
Lecture
model and theCourse
model:- In the
db/models/Lecture.js
add a property calledcourse
, where thetype
is a mongooseObjectId
that references theCourse
model
- In the
- Create add a route and controller function that establishes a relation:
- In the
api/lectures
Add a route withpost
as the method and/:lectureId
as the url - Create a controller called
addToCourse
in theapi/lectures/lectures.controllers.js
file and export it. - In the controller, update the lecture found in the
req.lecture
by setting thecourse
property to equal the course property in thereq.body
.
- In the
- Now that we have a relationship in the lecture finished, we now need to add many lectures to the
Course
model:- In
db/models/Course.js
add a property calledlectures
, assign it andempty array
. - Inside the
empty array
, add an object where the key calledtype
is set to a mongooseObjectId
and the keyref
references theLecture
model.
- In
- Back in the lecture controllers file:
- Import the
Course
model. - Inside the
addToCourse
function, use theCourse
model and thefindByIdAndUpdate
query to find the course using the id in the request body, then push the lecture id to the the courses list of the tag. - return an empty response with the status set to 204.
- Import the
- Test your endpoint in postman:
- Setup a request to use the endpoints you created earlier, and send
JSON
with the key ofcourse
set to a value of an id of acourse
that exists.
- Setup a request to use the endpoints you created earlier, and send
- Lets populate the list of courses and the list of lectures.
- In
lectures.controllers.js
, update the find method with populate to retrieve details on the course property. Test it in Postman using both adding a course to an existing lecture. - In the
courses.controllers.js
, update the find method with populate to retrieve details on the lectures property.
- In
A course can have many tags. E.g. A course like React
, has many tags
such as frontend
, javascript
, library
. And a tag can have many courses, e.g. a tag of frontend
can have css
, html
, javascript
, react
as associated courses.So we will try to create this type relationship by doing the following:
- Setup
- Create a course and a tag using postman.
- Create a relation between the
Course
model and theTag
model:- In
db/models/Course.js
add a property calledtags
, assign it andempty array
. - Inside the
empty array
, add an object where the key calledtype
is set to a mongooseObjectId
and the keyref
references theTag
model.
- In
- Create a relation between the
Tag
model and theCourse
model:- In
db/models/Tag.js
add a property calledcourses
, assign it andempty array
. - Inside the
empty array
, add an object where the key calledtype
is set to a mongooseObjectId
and the keyref
references theCourse
model.
- In
- Create add a route and controller function that establishes a relation:
- In the
api/courses
Add a route withpost
as the method and/:courseId
as the url. - Create a controller called
addTagToCourse
in theapi/courses/courses.controllers.js
file and export it. - In the controller, update the course found in the
req.course
by setting pushing thereq.body.tag
into thetags
property of thereq.course
. - Import the
Tag
model into thecourses.controllers.js
. - Inside the
addTagToCourse
function, use theTag
model and thefindByIdAndUpdate
query to find the tag using the id in the request body, then push the course id of the course to the the courses list of the tag. - return an empty response with the status set to 204.
- In the
- Test your endpoints in postman:
- Setup a request to use the endpoint you created earlier, and send
JSON
with the key oftag
set to a value of an id of atag
that exists.
- Setup a request to use the endpoint you created earlier, and send
- Lets populate the list of courses and the list of tags.
- In
tags.controllers.js
, update the find method with populate to retrieve details on the courses property. - In the
courses.controllers.js
, update the find method by chaining an extra populate to retrieve details on the tags property. - Test it in Postman using both adding an existing tag to a course.
- In
- When populating the list of lectures in the
courses.controllers.js
, only select the id and the name of the lecture. - When populating the list of a lectures for details of the course, only select the course name.
- Update the
createLecture
and theupdateLectureById
in thelectures.controller
to add a relation to thecourse
if it was in thereq.body
. - Modify the
deleteLectureById
to remove a lecture id from thecourse
list when a lecture is deleted.
- When populating the list of courses in the
courses.controllers.js
, only select the id and the name of the tags. - When populating the list of a tags only select the course name.
- Update the
createCourse
and theupdateCourseById
in thelectures.controller
to add a relations to thetags
if it was in thereq.body
, try doing this is a list of tags. - Modify the
deleteCourseById
to remove a course id from thetags
the course has relations with when after a course gets deleted.