Skip to content

pasha-log/react-jobly-backend

Repository files navigation

Jobly Backend

This is the Express backend for React Jobly Frontend.

Tools used:

  • Express
  • Postgres
  • morgan
  • cors
  • Authentication/validation: bcrypt, JSON Web Token, JSON Schema

Getting started

Install all dependencies with npm i
The project uses PostgreSQL as its RDBMS. To create and seed the test and development databases, simply run the following command:

psql < jobly.sql

To run this project:

node server.js

This will start the API server on localhost:3001/

To run the tests (make sure jest is installed, it is not listed as a dependency):

jest -i

The entirey of this project is a RESTFUL JSON API.

Routes

/auth

Authorization and authentication is performed using JSON Web Tokens. Send your JWT as bearer tokens in the authorization header of all of your requests sent to the API.

POST /auth/register

This route is used to create new users in the database. Include the following in the payload of your POST request, all fields are required:

{
    "username": "string, max 30 chars",
    "password": "string, min 5 chars, max 20 chars",
    "firstName": "string, max 30 chars",
    "lastName": "string, max 30 chars",
    "email": "string, email format, min 6 chars, max 60 chars"
}

This will create a new entry in the users table of the database, with a hashed and salted password. The server will respond with the auth token for the newly registered user.

POST /auth/token

This route is used to get another auth token. Send the username and password of the user you want to authenticate as in your request payload. If the credentials are valid, the server will respond with an auth token.

{ "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiJjb25ncmF0dWxhdGlvbnMiLCJob3dldmVyIjoibm90IGEgdmVyeSByZXdhcmRpbmcgc2VjcmV0LCBpcyBpdD8ifQ.PpjeIMk-mILsVOG-eLDcgmrmkXyNTnXRt01bFMfbtSU" }

/companies

Routes corresponding with the companies table in the database. Each company uses its handle as its primary key, and thus its identifier in the routes.

GET /companies

Auth Required: None

This endpoint will respond with a list of all companies in the database, with the following format:

{
    "companies": [
        {
            "handle": "valve",
            "name": "Valve Corporation",
            "description": "Will eventually release HL3",
            "numEmployees":1000,
            "logoUrl":"https://valvesoftware.com/logo.png"
        },
        ...
    ]
}

You can filter your results by passing parameters to the query string. The following parameters are available:

  • minEmployees: Company's numEmployees must be equal to or greater than this number.
  • maxEmployees: Company's numEmployees must be equal to or less than this number.
  • name: Company must have this string appearing somewhere in its name, case insensitive.

E.g. /companies?name=micro&maxEmployees=300 will return a list of all companies where "micro" appears somwhere in their company name, and that have no more than 300 employees.

GET /companies/:handle

Auth Required: None

This endpoint will respond with more in depth data on a given company, including a list of all current job offerings. E.g. /companies/valve:

{
    "company": {
        "handle": "valve",
        "name": "Valve Corporation",
        "description": "Will eventually release HL3",
        "numEmployees": 1000,
        "logoUrl": "https://www.valvesoftware.com/logo.png",
        "jobs": [
            {
                "id": 20,
                "title": "Milkman",
                "salary": 60000,
                "equity":"0.3",
                "companyHandle":"valve"
            },
            ...
        ]
    }
}

POST /companies

Auth Required: Admin

Creates a new entry in the companies table of the database. Include the data matching the following schema in the payload of your request, name, handle, and description are required:

{
    "handle": "valve", 
    "name": "Valve Corporation", 
    "description": "Will eventually release HL3",
    "numEmployees": 1000,
    "logoUrl": "http://valvesoftware.com/logo.png"
}

PATCH /companies/:handle

Auth Required: Admin

Update the data of an existing company in the database. Include any of the following fields in the payload of your request:

{
    "name": "string, max 30 chars",
    "description": "string",
    "numEmployees": "integer, min 0",
    "logoUrl": "string, URI format"
}

Will respond with a JSON object reflecting the update company data.

DELETE /companies/:handle

Auth Required: Admin

Delete the given entry from the companies table in the database. Will respond with a JSON object containing a message on successful deletion.


/jobs

GET /jobs

Auth Required: None

Will respond with a JSON object listing all of the current jobs in the database. It should be noted that equity is returned as a string in order to preserve precision.

{
    "jobs":
        [
            {
                "id": 20,
                "title": "Milkman",
                "salary": 60000,
                "equity":"0.3",
                "companyHandle":"valve"
            },
            ...
        ]
}

Like companies, the results of this route can also be filtered using the query string. The following parameters are available:

  • minSalary: Job's salary must be equal to or greater than this number.
  • hasEquity: Job's equity must be greater than 0 if this parameter is set to true.
  • title: Job must have this string appearing somewhere in its title, case insensitive.

E.g. /jobs?title=engineer&hasEquity=true&minSalary=90000 will return a list of all jobs where "engineer" appears somewhere in the job title, has an equity percentage greater than 0, and has a salary of at least 90000.

GET /jobs/:id

Auth Required: None

Will respond with more detailed information on the given job listing.

{
    "job": {
        "id": 20,
        "title": "Courier",
        "salary": 55000,
        "equity": "0.02",
        "company": { 
            "handle": "robco",
            "name": "RobCo Industries",
            "description": "Always wins",
            "numEmployees": 5,
            "logoUrl": "http://robco.com/logo.png"
            }
    }
}

GET /jobs/:id/applicants

Auth Required: Admin

Will respond with detailed information on the given job listing, including a list of all users that have applied for this job.

{
    "job": {
        "id": 20,
        "title": "Courier",
        "salary": 55000,
        "equity": "0.02",
        "companyHandle": "robco",
        "applicants": [
            {
                "username": "jd1911",
                "firstName": "John",
                "lastName": "Doe",
                "email": "[email protected]",
                "isAdmin": false
            },
            ...
        ]
    }
}

POST /jobs

Auth required: Admin

Will create a new entry in the jobs table of the database. Send the following in the payload of your request, title and companyHandle are required, and companyHandle must be a valid reference to an existing company:

{
    "title": "new job",
    "salary": 40000,
    "equity":  0.2,
    "companyHandle": "valvee"
}

A successful request will be responded to with data reflecting the newly created job, including its unique id in the database.

PATCH /jobs/:id

Auth required: Admin

Will update an existing job entry in the database with new data. Send any of the following in the payload of your request:

{
    "title": "string",
    "salary": "integer, min 0",
    "equity": "number, min 0, max 1.0"
}

A successful request will be responded with data reflecting the up-to-date job information.

DELETE /jobs/:id

Auth required: Admin

Will delete the given entry from the jobs table in the database. Will respond with a JSON object containing a message on successful deletion.

{ "deleted" : 20 }

/users

GET /users

Auth required: Admin

Responds with a json object containing a list of all users in the database.

{
    "users": [
        {
            "username": "jd1911",
            "firstName": "John",
            "lastName": "Doe",
            "email": "[email protected]",
            "isAdmin": false
        },
        ...
    ]
}

GET /users/:username

Auth required: Same User or Admin

Responds with a json object with data on the given user. This route is only available to non-admins if their auth token proclaims themselves to be the requested user.

{
    "user": {
        "username": "jd1911",
        "firstName": "John" ,
        "lastName": "Doe",
        "email": "[email protected]",
        "isAdmin": false
        }
}

GET /users/:username/applications

Auth required: Same User or Admin

Responds with a json object with data on the given user, as well as all of the jobs that user has applied to.

{
    "user": {
        "username": "jd1911",
        "firstName": "John" ,
        "lastName": "Doe",
        "email": "[email protected]",
        "isAdmin": false,
        "applications": [
            {
                "id": 20,
                "title": "Milkman",
                "salary": 50000,
                "equity": "0.025",
                "companyHandle": "robco"
            },
            ...
        ]
    }
}

POST /users

Auth required: Admin

Creates a new user in the database. This is NOT the registration endpoint --- instead, this is only for admin users to add new users. The payload required for this route is the same schema as /auth/register, except that isAdmin can be included to create a new admin user. The route responds with the newly created user and an authentication token for them:

    {
       "user": { 
           "username" : "jd1988", 
           "firstName": "jane", 
           "lastName": "doe", 
           "email": "[email protected]", 
           "isAdmin": true 
           }, 
        "token" :  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiJjb25ncmF0dWxhdGlvbnMiLCJob3dldmVyIjoibm90IGEgdmVyeSByZXdhcmRpbmcgc2VjcmV0LCBpcyBpdD8ifQ.PpjeIMk-mILsVOG-eLDcgmrmkXyNTnXRt01bFMfbtSU"
    }

POST /users/:username/jobs/:id

Auth required: Same User or Admin

Creates an association between a given user and job (i.e., registers the user as an applicant for that job). A successful request with be responded to with a JSON object containing the id of the job that was applied to.

{ "applied" : 20 }

PATCH /users/:username

Auth required: Same User or Admin

Will update an existing user entry in the database with new data. Send any of the following in the payload of your request:

{
    "password": "string, min 5 chars, max 20 chars",
    "firstName": "string, max 30 chars",
    "lastName": "string, max 30 chars",
    "email" : "string, email format, min 6 chars, max 60 chars"
}

A successful request will be responded with data reflecting the up-to-date user information (barring password).

DELETE /users/:username

Auth required: Same User or Admin

Will delete the given user from the users table in the database. Will respond with a JSON object containing a message on successful deletion.

{ "deleted" : "jd1911" }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages