You have been hired to complete an API for a blog app. The previous developer hasn't shown up for work for a few days. The talk around this office is that he won the lottery but no one really knows for sure. It's kinda a mystery. Unfortunately Sherlock, that's not the puzzle you were hired to solve. Your challenge is to complete the blog app in time for your manager's demo to the CEO next week.
- The previous developer created a front-end client using jQuery and Fetch. And started a API using Node and Express.
- The API currently uses dummy data which is just an array of objects in Javascript. It is not wired up to a database yet.
- The company uses PostgreSQL as their database. And hosts their applications on Heroku.
- Your manager says the specs for the API were documented using Postman's documenter and can be found here: Blog App API.
- And you found the previous developer's todo list which list the tasks and notes that need to be completed.
-
Create Skeleton endpoints using dummy data
- GET list all stories - should return status 200 and an array of objects
- GET get a story by id - should return status 200 and an object
- POST (create) a story - should return status 201 with a location header and the new object
- PUT (update) a story - should return status 200 and the updated object
- DELETE a story - should return status 204 with no content
-
Create database
- Create local Postgres database named
blog-app
- Create a
stories
table with the following 3 columns:id
: an auto-incrementing integertitle
: regular text. Required.content
: regular text. Can be blank.
Note: save the CREATE TABLE and INSERT INTO queries in a file so they can be easily run again later. Examples:
- Local DB:
psql -f ./query.sql -U <username> -d blog-app
- ElephantSQL:
psql -f ./query.sql postgres://<USERNAME>:<PASSWORD>@<SERVER:PORT>/<DATABASE>
- Create local Postgres database named
-
Wire-up database to the endpoints. IOW, replace dummy data with real database calls.
- Add
knex
andpg
to the project - Update
config.js
with DB connection info - Import
knex
and database config into the router file - Update GET
/api/v1/stories
endpoint to useknex.select()...
- Update GET
/api/v1/stories/:id
endpoint to useknex.select()...
- Update POST
/api/v1/stories/
endpoint to useknex.insert()...
- Update PUT
/api/v1/stories/:id
endpoint to useknex.update()...
- Update DELETE
/api/v1/stories/:id
endpoint to useknex.del()...
- Add
-
Deploy to Heroku (see Deploying to Heroku)
- Install Heroku CLI app, if necessary
- Create app on Heroku
- Update git remote to point to Heroku
- Push app to Heroku. (Note: app won't work yet)
- Create a production database on Elephant SQL. Use
query.sql
from earlier to easily create the database - Configure Heroku config vars to use
DATABASE_URL
and the Elephant SQL connection string
Congrats! The boss is very impressed with your work and the quick turn-around. She is so impressed in fact that she is wondering if you could add authors
to the stories. You think about for a bit and decide it should be relatively easy to just update the api. But there is a wrinkle, that sales team is already using the first version and the changes might break their app. After pondering your options for a bit you remember you are using express router so you decide to create a /v2 route.
You create the following high-level tasks:
- Update the Database:
- Create an
authors
table- 3 Columns
id
: an autoincrementing integerusername
: regular text. Required.email
: regular text. Required.
- 3 Columns
- Add
author_id
foreign key column the stories table that references the authors table.
Note, when we delete a story, we do not want to delete the associate
author
, so choose the correctON DELETE
contraint.- Add a dummy users to the db and update stories to have authors.
- Create an
- Create a V2 router file
- Create a
/router/stories-router-v2.js
router file - Require the router file in
server.js
and "mount" usingapp.use()
- Create a
- Update the queries in the V2 endpoints to use
author
info.