Coder Social home page Coder Social logo

adattivorestserver's Introduction

Adattivo RESTful Server Coding Challenge

BY: Steven Wagner

Set Up

  1. Clone this repository to your local machine git clone THIS-PROJECTS-URL NEW-PROJECTS-NAME
  2. cd into the cloned repository
  3. Make a fresh start of the git history for this project with rm -rf .git && git init
  4. Install the node dependencies npm install

Setting Up The Databases and .env File

  1. Create 2 postgreSQL databases:
  • One for test enviroment
  • One for a mock production/staging enviroment

More information can be viewed here postgreSQL Documentation

  1. Rename the example.env file with mv example.env .env

  2. Add values to .env file. Replace all bracketed code with appropriate values, utilizing the 2 databases you created:

PORT=8000
TEST_DB_URL=[url to your test database e.g."postgresql://user:password@localhost/databaseName"]
DB_URL=[url to your staging/production database e.g."postgresql://user:password@localhost/databaseName"]

DELETE_EMPLOYEE_PASSWORD=password

MIGRATION_DB_HOST=localhost
MIGRATION_DB_PORT=5432
MIGRATION_DB_NAME=[name of test database]
MIGRATION_DB_USER=[user who has database privilges for the test database]
MIGRATION_DB_PASS=[users password]

PROD_MIGRATION_DB_HOST=localhost
PROD_MIGRATION_DB_PORT=5432
PROD_MIGRATION_DB_NAME=[name of production database]
PROD_MIGRATION_DB_USER=[user who has database privileges for the production database]
PROD_MIGRATION_DB_PASS=[users password]

For the sake of simplicity, DELETE_EMPLOYEE_PASSWORD is defaulted to password, but can be changed to any value. Just make sure that the DELETE /api/employee/employee_id API request has the correct value in the Authorization header.

  1. Run npm run init-databases. This script will migrate the databases and seed it with intial data

  2. npm start will start the production API and endpoints can be called with a base URL that will most likely be http://localhost:8000

Scripts

Start the application npm start

Run the tests npm test

Start nodemon for the application npm run dev

Migrate test database npm run migrate

Migrate production database npm run migrate-production

Reset seed data in production database npm run seed-data This script will not migrate the database. Run npm run init-databases to migrate and seed data.

To migrate test and production database, and seed production database npm run init-databases

Using The API Endpoints

GET all employees

Endpoint: http://localhost:8000/api/all-employees/

Returns all ACTIVE employees.

Returns Ex:

[
    {
        "id": 1,
        "firstname": "Larry",
        "lastname": "Fine",
        "middleinitial": "A",
        "dateofbirth": "01/01/1925",
        "dateofemployment": "03/16/1969"
    },
    {
        "id": 2,
        "firstname": "Curly",
        "lastname": "Howard",
        "middleinitial": "B",
        "dateofbirth": "06/01/1925",
        "dateofemployment": "07/03/1947"
    }
]

GET employee by ID

Endpoint: http://localhost:8000/api/employee/:employee_id

employee_id param is the ID of the employee to GET

Returns a single employee by a unique ID.

Returns Ex:

{
    "id": 1,
    "firstname": "Larry",
    "lastname": "Fine",
    "middleinitial": "A",
    "dateofbirth": "01/01/1925",
    "dateofemployment": "03/16/1969"
}

POST new employee

Endpoint: http://localhost:8000/api/employee/

Adds a new employee.

Headers:

  • Content-Type: application/json

Body:

{
    "firstname": [Required][Must be less than 51 characters in length][API will auto capitalize],
    "lastname": [Required][Must be less than 51 characters in length][API will auto capitalize],
    "middleinitial": [Optinal][Must only be a letter A-Z|a-z or undefined, Must be 1 character in length][API will auto capitalize],
    "dateofemployment": [Required][Must be in MM/DD/YYYY format],
    "dateofbirth": [Optinal][Must be in MM/DD/YYYY format],
}

Returns Ex:

{
    "id": 7
}

Returns the id of the new employee

PATCH new employee

Endpoint: http://localhost:8000/api/employee/:employee_id

employee_id param is the ID of the employee to PATCH

Updates an existing employee. Must include at least one valid field in the body

Headers:

  • Content-Type: application/json

Body:

May include one or more of the following feilds:
{
    "firstname":[Must be less than 51 characters in length][API will auto capitalize],
    "lastname":[Must be less than 51 characters in length][API will auto capitalize],
    "middleinitial":[Must only be a letter A-Z|a-z, Must be 1 character in length][API will auto capitalize],
    "dateofemployment":[Must be in MM/DD/YYYY format],
    "dateofbirth":[Must be in MM/DD/YYYY format],
    "status":[Must be 'ACTIVE']
}

Returns Ex:

Returns 204 No Content

DELETE new employee

Endpoint: http://localhost:8000/api/employee/:employee_id

employee_id param is the ID of the employee to DELETE

Deletes an employee by ID.

Headers:

  • Authorization: password [Must have correct password. See DELETE_EMPLOYEE_PASSWORD variable in .env file]

Returns Ex:

Returns 204 No Content

Technologies Used

  • Node.js
  • Express.js
  • postgreSQL
  • knex
  • postgrator
  • Mocha
  • Chai
  • moment.js
  • xss

adattivorestserver's People

Contributors

steven-wagner avatar

Watchers

James Cloos avatar  avatar

adattivorestserver's Issues

GET by ID

endpoint should be {baseUrl}/employee/{employee_id} where the user_id is the id of the employee to be queried.

Should return all columns where employees.id = employee_id

Employee should not have same last, first, and middle initial

When new employee is posted validate that someone with the same exact name is not already in the list.

Perhaps send a message indicating that a person with this exact name exists and that it may be a duplicate. Give the user an option to set checkDuplicate to false;

Validate GET employee by ID

employee_id param should correspond to an actual employee ID

employees who have their status set to 'INACTIVE' should not be gettable.

Validate POST new employee

  • required fields are:

  • firstName,

  • LastName,

  • DateOfEmployment

  • FirstName and LastName can not be longer than 50 characters.

  • DateOfEmployment and DateOfBirth must be in mm/dd/yyyy format.

  • MiddleInital can only be 1 character and must be a letter.

DELETE employee

Endpoint should be {baseUrl}/api/employee/{employee_id}

The employee_id is the id of the employee to be deleted.

Must include a valid Authorization header. For the purpose of this project the test content for the auth header will be 'password'

If validated, employee's Status column should be updated to 'INACTIVE'

Validate DELETE employee

  • employee_id must exist
  • employee must not already have Status: 'INACTIVE'
  • Authorization header must be correct password

GET all employees

endpoint should be {baseUrl}/all-employees/

Should return all columns of all employees in the employees table.

POST new employee

endpoint should be {baseUrl}/new-employee/

request body should contain:

  • FirstName varchar(50) not null,
    
  • MiddleInitial char(1), (optional)
    
  • LastName varchar(50) not null,
    
  • DateOfBirth date, (optional) format for date is mm/dd/yyyy
    
  • DateOfEmployment date not null, format for date is mm/dd/yyyy
    
  • Status default is set to 'ACTIVE', options are 'ACTIVE' or 'INACTIVE' (optional)
    

Should return id of new employee if successful

Validate PATCH existing employee

  • employee_id must exist
  • FirstName and LastName can not be longer than 50 characters.
  • DateOfEmployment and DateOfBirth must be in mm/dd/yyyy format.
  • MiddleInital can only be 1 character and must be a letter.

PATCH update existing employee

Endpoint should be {baseUrl}/api/employee/employee_id

employee_id is the id of the user to be updated.

Request body fields (i.e anyone may update these columns) may include:

  • FirstName
    
  • MiddleInitial
    
  • LastName
    
  • DateOfBirth
    
  • DateOfEmployment
    
  • Status - options are 'ACTIVE' or 'INACTIVE'
    

returns 200 if success.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.