/blog-api

Primary LanguageJavaScript

Blog API

A Hashnode inspired blog API to practice PostgreSQL and learn Prisma ORM.

All passwords are hashed with bcrypt.js before they are stored in the database. JWT tokens are used to login and protect user data.

Run Locally

Follow these steps to run a local instance of this API. (You will need node, npm, and PostgreSQL already installed.)

Installation

  1. Clone the project
  git clone git@github.com:LamiSaadat/blog-api.git
  1. Create a new database in pgadmin.

  2. Install dependencies

  npm install
  1. Run migrations
  npm run migrate
  1. Set environment variables:

Rename .env_sample to .env and change the placeholder values.

  DATABASE_URL=<postgresql://USER:PASSWORD@HOST:PORT/DATABASE>
  ACCESS_TOKEN_SECRET=<your secret>
  PORT=<a port number>
  1. Start the server
  npm start

Features

  • Register a user
  • Login a user
  • View all published posts
  • View all user profiles
  • View all followers of a user
  • Logged in users can
    • Create posts
    • Create draft posts only visible to creator
    • Edit own posts
    • Delete own posts
    • Comment on posts
    • Like, and then unlike posts
    • Follow/Unfollow other users

Project Demo

Blog.API.mov

API Reference

Register a user

  POST /user/signup

Body

{
"firstName": "Rory",
"lastName": "Gilmore",
"email": "rory@gmail.com",
"password": "123"
}

Login a user

  POST /user/login

Body

{
"email": "rory@gmail.com",
"password": "123"
}

View own account details

  POST /user/account

View a user profile

  POST /user/:id/profile

Follow a user

  POST /user/:id/follow

Unfollow a user

  POST /user/:id/unfollow

Find all followers for a user

  POST /user/:id/followers

Get all published posts

  GET /posts/feed

Create a post

  POST /posts/create

Body

{
"title": "test post title 4",
"content": "test post content",
"authorEmail": "givemecoffee@gmail.com",
"published": false
}

Edit a post

  PATCH /posts/:id

Body

{
"title": "edited title",
"content": "edited content",
"published": true
}

Get drafted/unpublished posts

  GET /posts/drafts

Delete a post

  DELETE /posts/:id

Comment on a post

  POST /posts/:id/comment

Body

{
"comment": "sample comment",
}

Like a post

  POST /posts/:id/like

Body

{
"like": true
}

Unlike a post

  POST /posts/:id/unlike

Lessons Learned

  • Good documentation saves a lot of time and tears
  • Learned about implicit vs explicit many-to-many relations which helped with creating the like and follow systems which was a struggle
  • Learned to apply auth with jwt
  • Learned to use pgadmin
  • Learned Prisma migrations, and editing tables and creating new migrations

Tech Stack

Node, Express, PostgreSQL, Prisma

Acknowledgements