Express + PG - Users / Companies / Jobs
Part I - Users
-
Create a table for users, each user should have a:
- first_name
- last_name
- photo
-
Here is what a user object looks like:
{ "id": 1, "first_name": "Michael", "last_name": "Hueter", "email": "michael@rithmschool.com", "photo": "https://avatars0.githubusercontent.com/u/13444851?s=460&v=4", "company_id": 1, // MANY-TO-ONE with Companies --> THIS IS IMPLEMENTED IN THE NEXT SECTION "jobs": [2, 3] // MANY-TO-MANY with Jobs --> THIS IS IMPLEMENTED IN THE FINAL SECTION }
-
Create an API that has the following five routes:
POST /users- this should create a new userGET /users- this should return a list of all the user objectsGET /users/:id- this should return a single user found by itsidPATCH /users/:id- this should update an existing user and return the updated userDELETE /users/:id- this should remove an existing user and return the deleted user
-
BONUS - add a frontend that allows for seeing all the users, creating new users and deleting users. Do not worry about any kind of authentication/authorization.
-
BONUS - add front-end functionality for updating users. This will involve writing quite a bit more jQuery to accomplish this task.
-
BONUS - Use vanilla JavaScript instead of jQuery.
Part II - Companies
Before you continue, make sure you have completed the exercises in the previous section. This exercise builds off of the previous exercise.
Create a table for companies, each company should have a:
-
name
-
logo
-
Next, add a column to your users table called
current_company_idwhich is a foreign key that references the companies table. In this relationship, one company has many users, and each user belongs to a single company. Make sure then when a company is deleted, all of the users associated with that company are deleted also. -
Create an API that has the following five routes:
POST /companies- this should create a new companyGET /companies- this should return a list of all the company objectsGET /companies/:id- this should return a single company found by its id and it should include all of the ids of users who work therePATCH /companies/:id- this should update an existing company and return the updated companyDELETE /companies/:id- this should remove an existing company and return the deleted company
-
Here is what a company object looks like:
{ "id": 1, "name": "Rithm School", "logo": "https://avatars3.githubusercontent.com/u/2553776?s=400&u=18c328dafb508c5189bda56889b03b8b722d5f22&v=4", "users": [1, 2], // array of user IDs who work there. ONE-TO-MANY with Users "jobs": [2, 3] // array of job IDs listed by the company. ONE-TO-MANY with Jobs --> THIS IS IMPLEMENTED IN THE FINAL SECTION }
Part III - Jobs
Before you continue, make sure you have completed the exercises in the previous sections. This exercise builds off of the previous exercise.
-
Add a table for
jobs, each job should have a:- title
- salary
- equity
- company_id
-
jobshas a one to many relationship withcompanieswhich means there is a foreign key in the jobs table that references the companies table. In this relationship, one company has many jobs, and each job belongs to a single company. Make sure then when a company is deleted, all of the jobs associated with that company are deleted also. -
jobsis also a many to many relationship withusers, because a user can apply to many jobs. This means you'll also have to create a join table for these two associations. You can call that tablejobs_usersand it should contain ajob_idanduser_id. -
Make sure your application has the following routes:
POST /jobs- this route creates a new jobGET /jobs- this route should list all of the jobs.GET /jobs/:id- this route should show information about a specific jobPATCH /jobs/:id- this route should let you update a job by its IDDELETE /jobs/:id- this route lets you delete a job posting
-
Here is what a job object looks like:
{ "title": "Software Engineer", "salary": "100000", "equity": 4.5, "company_id": 1 }
Part IV - User Authentication + Authorization
Before you continue, make sure you have completed at least Part I and Part II above
-
Add a column in the
userstable calledusername. This column should have a type oftextand should be unique and never be null. -
Add a column in the
userstable calledpassword. This column should have a type oftextand should never be null. The column should store a hashed password using bcrypt. Make sure that when a user is created and updated, the password is stored securely. -
Add a new route
/users/auth. This route accepts a POST request with a username and password, and it returns a JWT if the username exists and the password is correct. The JWT should store the id of the logged in user. -
Protect the following routes and make sure only a user who has logged in can use them:
GET /usersGET /users/:idGET /jobsGET /jobs/:idGET /companiesGET /companies/:id
-
Protect the following routes and make sure they are only accessible by the user with the correct id.
PATCH /users/:idDELETE /users/:id
Part V - Company Auth
-
Add a column in the
companiestable calledhandle. This column should have a type oftextand should be unique and never be null. -
Add a column in the
companiestable calledpassword. This column should have a type oftextand should never be null. The column should store a hashed password using bcrypt. Make sure that when a company is created and updated, the password is stored securely. -
Add a new route
/companies/auth. This route accepts a POST request with a company'shandleandpassword, and it returns a JWT if the handle exists and the password is correct. The JWT should store the id of the logged in company. -
Allow logged in companies to see the following routes (these are all the routes logged in users can see):
GET /usersGET /users/:idGET /jobsGET /jobs/:idGET /companiesGET /companies/:id
-
Protect the following routes and make sure they are only accessible by the company with the correct id.
PATCH /companies/:idDELETE /companies/:id
-
Protect the following routes so that only companies can post jobs, and posted jobs can only be edited and deleted by the company that created them.
POST /jobsPATCH /jobs/:idDELETE /jobs/:id
Part VI - Testing + Validation
Before you continue, make sure you have completed at least Part I and Part II of this exercise
- Make sure that there is validation each time a
useris created or updated. - Make sure that there is validation each time a
companyis created or updated. - Make sure that there is validation each time a
jobis created or updated. - Add tests for your
users,companies, andjobsroute.
Solutions
To get any of these solutions running locally:
- Fork/clone the repository
cdinto a foldernpm installpsql < schema.sqlnodemonornode app.js