Giter Club home page Giter Club logo

blog_api's Introduction

Introduction

Blog API made with typescript, express & PostgreSQL with all the basic features and more !

Technologies

  • Nodejs
  • Expressjs
  • Typescript
  • PostgreSQL
  • Redis

 

File Structure

GitHub Logo

Basic commands

git clone https://github.com/leoantony72/blog_api
npm install

 

PostgreSQL Setup

You have to install Postgres Yourself🙃

Start Postgres server❗

sudo service postgresql start

 

After installing get into postgres CLI

CREATE DATABASE blog; //This will create the database

 

Next we have to setup TABLES❗

\C blog //this will take you inside blog db

 

Copy tables From blogapi.sql file(src->models->blog_api.sql)❗

\i FILE PATH

 

OR YOU CAN COPY/PASTE FROM blogapi.sql

CREATE TABLE users(
    userid VARCHAR(11) NOT NULL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    passwordHash text NOT NULL,
    user_role VARCHAR(25) NOT NULL,
    sessionid text ,
    registeredAt TIMESTAMP NOT NULL
);

CREATE INDEX idx_userid ON users(userid);

CREATE TABLE authors(
    id VARCHAR(11) NOT NULL PRIMARY KEY,
    username VARCHAR(50),
    profile_image BYTEA NULL
);

CREATE TABLE post(
    post_id VARCHAR(11) NOT NULL PRIMARY KEY,
    title VARCHAR(100) NOT NULL,
    meta_title VARCHAR(100) NULL,
    slug VARCHAR(100) NOT NULL UNIQUE,
    summary VARCHAR(100) NULL,
    content TEXT NULL DEFAULT NULL,
    published VARCHAR(20) NULL,
    publishedAt TIMESTAMP NOT NULL,
    author_id VARCHAR(11) NULL REFERENCES authors(id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);

CREATE TABLE savedpost(
    id VARCHAR(11) NOT NULL PRIMARY KEY,
    userid VARCHAR(11) NOT NULL REFERENCES users(userid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    post_id VARCHAR(11) NOT NULL REFERENCES post(post_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);

CREATE TABLE category(
    id VARCHAR(11) NOT NULL PRIMARY KEY,
    title VARCHAR(75) NOT NULL,
    meta_title VARCHAR(100) NULL DEFAULT NULL,
    slug VARCHAR(100) NOT NULL
);

CREATE INDEX idx_category_parent on category(id);

CREATE TABLE post_category(
    post_id VARCHAR(11) NOT NULL REFERENCES post(post_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE ,
    category_id VARCHAR(11) NOT NULL REFERENCES category(id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);
CREATE INDEX idx_post_post ON post_category(post_id ASC);
CREATE INDEX idx_post_category ON post_category(category_id ASC);


WITH THIS DB SETUP IS FINISHED😸

 

Setting Up .ENV File😈

These are the env variabes of database❗

PORT = 3000
DB_USER = test
DB_PASSWORD = password
DB_HOST = localhost
DB_PORT = 5432
DB_DATABASE = blog
SESSION_SECRET = secret for session //use a strong one
SESSION_MAXAGE = 3600000 * 60 * 10
GMAIL_USER = example
GMAIL_PASS = password

 

Image Folder

Create images Folder in Root dir, Inside Images Folder create post_banner Folder❗

 

Start Server❗

npm run dev -L //RUNNNING IN LOCALHOST:3000

The Server is now Running✔

 

Api Routes

GET POSTS

http://localhost:3000/api/posts //get all posts

 

GET POST BY ID

http://localhost:3000/api/post/:id //get post by ID

 

GET POST BY CATEGORY

http://localhost:3000/api/posts/category/:category //get post by category

 

GET POST BY AUTHOR

http://localhost:3000/api/posts/author/leo //get post by author name

 

REGISTER/LOGIN/LOGOUT (POST request) 🔥

  • Register User🙍‍♀️

http://localhost:3000/api/auth/register
{
  "username": "example",
  "email": "[email protected]",
  "password": "password183$",
  "confirmpassword": "password183$"//password are hashed before storing in db
}

 

  • Login User
http://localhost:3000/api/auth/login  //Login users
{
  "username":"Leoantony72",
  "password":"test1234"
}

 

  • Logout Users
http://localhost:3000/api/logout

 

  • Forgot Password
http://localhost:3000/api/auth/forgotpassword
{
    "success": "Email Sent"
}

Sends 📧 to provided user, Also checks if user exist in db  

User Roles

USER AND ADMIN

Users cannot create posts,updatepost etc... , Only Admin users can Create post & modify post

By default role is USER ,After registering a user you need go to db to change Role to ADMIN ❗❗

⭕I RECOMMEND USING POSTBIRD (GUI) It is simple to use

 

Create New Post & Modify

USE POSTMAN FOR TESTING❗

  • New post (post req)
http://localhost:3000/api/admin/upload //create new post

WE ALSO HAVE TO UPLOAD IMAGE SO CHOOSE *FORM DATA* IN POSTMAN ❗❗

GitHub Logo

{
  "post_id": "hapefdmrktf" //IF SUCCESS IT WILL RETURN A POSTID
}

 

  • Update Post (PUT req)
http://localhost:3000/api/admin/post/hapefdmrktf //update post

GitHub Logo

"post updated" //if success you will get this

 

  • Delete Post (DELETE req)
http://localhost:3000/api/admin/post/hapefdmrktf //Delete post by post ID
"post deleted" //if success you will get this

 

Search Post

Search post by title/meta title

http://localhost:3000/api/search/attack on titan //Search post by post ID
{
        "post_id": "itlfosfntlj",
        "title": "cool boi",
        "meta_title": "attack on titan\r\n",
        "slug": "testfgggfgr",
        "summary": "res",
        "content": "rser",
        "published": "published",
        "publishedat": "2021-07-19T06:02:55.380Z",
        "author_id": "1",
        "image": "1626674575348.jpeg"
    }

 

Save Post

you need to login to use this feature

  • Add saved post (POST req)
http://localhost:3000/api/savedpost/itlfosfntlj //save post
{
  "success": "Post saved" // if success you will get this
}

 

  • Delete saved post (DELETE req)
http://localhost:3000/api/savedpost/:id //delete saved post
{
  "success": "Post deleted" // if success you will get this
}

 

  • Get saved post (GET req)
http://localhost:3000/api/savedpost //Get saved post
//you will get the post if success
[
  {
    "post_id": "7383e359ddb",
    "title": "cool boi",
    "meta_title": "leo",
    "slug": "nofsea",
    "summary": "testsef",
    "content": "test",
    "published": "published",
    "publishedat": "2021-08-04T22:34:30.985Z",
    "author_id": "1",
    "image": "1628116470935.jpeg"
  }
]

Some Additional Info

- Added Forgot Password

- included session & cookies

- Caching for Posts(/posts,/post/:id)

- Email verification,if not verified you cannot login

- You can only upload .png/.jpg/.jpeg file below 5mb ❗

- Passwords and tokens are hashed & stored in database

- Added input Validation in Login/register/forgotpassword

  • Dependencies

GitHub Logo

This marks the end of the documentaion...

Any future updates and additional info will be added

blog_api's People

Contributors

leoantony72 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.