[
{
"username": "iamauser",
"password": "randompassword"
},
{
"username": "pambeesly",
"password": "anotherrandompassword"
},
{
"username": "dwightschrute",
"password": "somethingrandom"
}
]- Register a new user
- username required (must be a string | unique)
- password required (must be a string)
- phone required (must be a string)
What you send:
{
"username": "iamauser",
"password": "randompassword",
"phone": "7131234567"
}What you receive:
{
"message": "You have successfully created an account with username iamauser"
}- Login
- username and password required
- returns the following:
- message: { "Welcome back iamauser" }
- token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImlhbWF1c2VyIiwiaWF0IjoxNjM2ODYyMDY5LCJleHAiOjE2MzY5NDg0Njl9.fhVnkCzPDA5kubS1fo3mj57AEZcon267qH7dQ5Rk7rU"
- user_id: 1
What you send:
{
"username": "iamauser",
"password": "randompassword"
}What you receive:
{
"message": "Welcome back iamauser",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImlhbWF1c2VyIiwiaWF0IjoxNjM2ODYyMDY5LCJleHAiOjE2MzY5NDg0Njl9.fhVnkCzPDA5kubS1fo3mj57AEZcon267qH7dQ5Rk7rU",
"user_id": 1
}RESTRICTED ENDPOINT
- Get an array of users for authenticated user
- requires valid token in authorization header to access
What you receive:
[
{
"phone": "7131234567",
"user_id": 1,
"username": "iamauser"
},
{
"phone": "8321234567",
"user_id": 2,
"username": "iamausertoo"
}
]RESTRICTED ENDPOINT
- Get information for a specific user
- requires valid token in authorization header to access
What you receive:
{
"phone": "7131234567",
"user_id": 1,
"username": "iamauser"
}RESTRICTED ENDPOINT
- Update authenticated user's phone number or password
- requires valid token in authorization header to access
What you send:
{
"phone": 2811234567,
"password": "newrandompassword"
}What you receive:
{
"message": "Your submission has been updated"
}RESTRICTED ENDPOINT
- Get an array of plants belong to the logged in / authenticated user
- requires valid token in authorization header to access
What you receive:
[
{
"image_url": "marble-pothos.png",
"light_requirement": "Thrives in medium to low indirect light. Not suited for intense, direct sun.",
"nickname": "Marble Queen Pothos",
"plant_id": 1,
"species": "Epipremnum aureum",
"user_id": 1,
"water_frequency": "Water every 1-2 weeks"
},
{
"image_url": "snake-plant2.png",
"light_requirement": "Thrives in medium to bright indirect light, but can tolerate low indirect light.",
"nickname": "Snake Plant Laurentii",
"plant_id": 2,
"species": "Dracaena trifasciata",
"user_id": 1,
"water_frequency": "Water every 2-3 weeks"
},
{
"image_url": "zz.png",
"light_requirement": "Thrives in medium to low indirect light. Can tolerate bright indirect light. Not suited for intense, direct sun.",
"nickname": "ZZ Plant",
"plant_id": 3,
"species": "Zamioculcas",
"user_id": 1,
"water_frequency": "Water every 3-4 weeks"
}
]RESTRICTED ENDPOINT
- Get an array of all plants
- requires valid token in authorization header to access
What you receive:
[
{
"image_url": "marble-pothos.png",
"light_requirement": "Thrives in medium to low indirect light. Not suited for intense, direct sun.",
"nickname": "Marble Queen Pothos",
"plant_id": 1,
"species": "Epipremnum aureum",
"user_id": 1,
"water_frequency": "Water every 1-2 weeks"
},
{
"image_url": "snake-plant2.png",
"light_requirement": "Thrives in medium to bright indirect light, but can tolerate low indirect light.",
"nickname": "Snake Plant Laurentii",
"plant_id": 2,
"species": "Dracaena trifasciata",
"user_id": 1,
"water_frequency": "Water every 2-3 weeks"
},
{
"image_url": "zz.png",
"light_requirement": "Thrives in medium to low indirect light. Can tolerate bright indirect light. Not suited for intense, direct sun.",
"nickname": "ZZ Plant",
"plant_id": 3,
"species": "Zamioculcas",
"user_id": 1,
"water_frequency": "Water every 3-4 weeks"
},
{
"image_url": "ech.png",
"light_requirement": "Thrives in bright direct light, but can tolerate bright indirect light.",
"nickname": "Echeveria",
"plant_id": 4,
"species": "succulents",
"user_id": 3,
"water_frequency": "Water every 3-4 weeks"
}
]RESTRICTED ENDPOINT
- Get information for a specific plant
- requires valid token in authorization header to access
What you receive:
{
"image_url": "marble-pothos.png",
"light_requirement": "Thrives in medium to low indirect light. Not suited for intense, direct sun.",
"nickname": "Marble Queen Pothos",
"plant_id": 1,
"species": "",
"user_id": 1,
"water_frequency": "Water every 1-2 weeks"
}RESTRICTED ENDPOINT
- Add a new plant
- requires valid token in authorization header to access
- nickname required (must be a string)
- water_frequency required (must be a string)
What you send:
{
"image_url": "marble-pothos.png",
"light_requirement": "Thrives in medium to low indirect light. Not suited for intense, direct sun.",
"nickname": "Marble Queen Pothos",
"species": "Epipremnum aureum",
"water_frequency": "Water every 1-2 weeks"
}What you receive:
{
"image_url": "marble-pothos.png",
"light_requirement": "Thrives in medium to low indirect light. Not suited for intense, direct sun.",
"nickname": "Marble Queen Pothos",
"plant_id": 1,
"species": "Epipremnum aureum",
"user_id": 1,
"water_frequency": "Water every 1-2 weeks"
}RESTRICTED ENDPOINT
- Update an existing plant
- requires valid token in authorization header to access
- nickname required (must be a string)
- water_frequency required (must be a string)
What you send:
{
"image_url": "marble-pothos.png",
"light_requirement": "Thrives in medium to low indirect light. Not suited for intense, direct sun.",
"nickname": "Marble Queen Pothos",
"species": "Epipremnum aureum",
"water_frequency": "Water every 1-2 weeks"
}What you receive:
{
"image_url": "updated-marble-pothos.png",
"light_requirement": "Thrives in medium to low indirect light. Not suited for intense, direct sun.",
"nickname": "Marble Queen Pothos",
"plant_id": 1,
"species": "Epipremnum aureum",
"user_id": 1,
"water_frequency": "Water every 1-2 weeks"
}RESTRICTED ENDPOINT
- Delete an existing plants
- requires valid token in authorization header to send
- required information:
- plant_id (integer)
What you receive:
{
"message": "Plant id 1 has been deleted"
}The following tutorial explains how to set up this project using PostgreSQL and Heroku.
- PostgreSQL, pgAdmin 4 and Heroku CLI installed in your local machine.
- A Heroku app with the Heroku PostgreSQL Addon added to it.
- Development and testing databases created with pgAdmin 4.
- Create a new repository using this template, and clone it to your local.
- Create a
.envfile and follow the instructions insideknexfile.js. - Fix the scripts inside
package.jsonto use your Heroku app.
- start: Runs the app in production.
- server: Runs the app in development.
- migrate: Migrates the local development database to the latest.
- rollback: Rolls back migrations in the local development database.
- seed: Truncates all tables in the local development database, feel free to add more seed files.
- test: Runs tests.
- deploy: Deploys the main branch to Heroku.
The following scripts NEED TO BE EDITED before using: replace YOUR_HEROKU_APP_NAME
- migrateh: Migrates the Heroku database to the latest.
- rollbackh: Rolls back migrations in the Heroku database.
- databaseh: Interact with the Heroku database from the command line using psql.
- seedh: Runs all seeds in the Heroku database.
-
Figure out the connection to the database and deployment before writing any code.
-
If you need to make changes to a migration file that has already been released to Heroku, follow this sequence:
- Roll back migrations in the Heroku database
- Deploy the latest code to Heroku
- Migrate the Heroku database to the latest
-
If your frontend devs are clear on the shape of the data they need, you can quickly build provisional endpoints that return mock data. They shouldn't have to wait for you to build the entire backend.
-
Keep your endpoints super lean: the bulk of the code belongs inside models and other middlewares.
-
Validating and sanitizing client data using a library is much less work than doing it manually.
-
Revealing crash messages to clients is a security risk, but during development it's helpful if your frontend devs are able to tell you what crashed.
-
PostgreSQL comes with fantastic built-in functions for hammering rows into whatever JSON shape.
-
If you want to edit a migration that has already been released but don't want to lose all the data, make a new migration instead. This is a more realistic flow for production apps: prod databases are never migrated down. We can migrate Heroku down freely only because there's no valuable data from customers in it. In this sense, Heroku is acting more like a staging environment than production.
-
If your fronted devs are interested in running the API locally, help them set up PostgreSQL & pgAdmin in their machines, and teach them how to run migrations in their local. This empowers them to (1) help you troubleshoot bugs, (2) obtain the latest code by simply doing
git pulland (3) work with their own data, without it being wiped every time you roll back the Heroku db. Collaboration is more fun and direct, and you don't need to deploy as often.
