/project-blogs-api

An API and a database were developed to produce content for a blog. Node.js and the sequelize package were used to make a CRUD of posts.

Primary LanguageJavaScript

Welcome to the Blogs API project repository!!

Introduction

I developed an API and a database to CRUD (Create, Read, Update, Delete) posts for a blog. The API follows REST principles.

In this application, you can create a user and delete it, login, register new categories and retrieve them, and the main thing, make a CRUD of posts. To do this, it is necessary to generate a token (login with an existing user).


Installation

Installation instructions

Clone the repository

git clone git@github.com:lucas-da-silva/project-blogs-api.git

Enter the repository

cd project-blogs-api

Climbing the containers (docker is needed)

docker-compose up -d

Entering the Node.js container

docker exec -it blogs_api bash

Install dependencies

npm install

Run the application (will create the database, tables and values for them)

npm start

You can use Thunder Client or Insomnia (or whatever) to check API routes.

To stop containers

docker-compose down

Aplication

The sequelize package is used to map the database entities, generate the connection and serve as the Model layer of the architecture used here, which is the MSC (Model, Service and Controller).

In addition to creating all the routes, I developed the migrations and the models, defining the table relationships in the models, which are 1:1, 1:N and N:N.

The jwt library was also used to generate a token, which is necessary to have complete access to the application, without a token, you cannot access the routes and manipulate the database.

There are multiple validations to perform a request, from middlewares, functions dedicated to validations and token validation, so pay attention to what is expected when making a request.

Login routes

  • POST /login: returns a token;
The request body should follow the format below
    {
      "email": "lewishamilton@gmail.com",
      "password": "123456"
    }

User routes

  • POST /user: adds a new user to the database and returns a token;

Keep the password, as it will be encrypted in the database;

The request body should follow the format below
    {
      "displayName": "Brett Wiltshire",
      "email": "brett@email.com",
      "password": "123456",
      "image": "http://4.bp.blogspot.com/_YA50adQ-7vQ/S1gfR_6ufpI/AAAAAAAAAAk/1ErJGgRWZDg/S45/brett.png"
    }
  • GET /user: returns all database users;

  • GET /user/:id: returns the user based on the database id if it exists;

The following endpoint needs a valid token in the request header, within the Authorization key;

  • DELETE /user/me: delete you from the database, based on the id inside your token;

Categories routes

  • POST /categories: adds a new category to the database;
The request body should follow the format below
    {
      "name": "Typescript"
    }
  • GET /categories: returns all database categories;

Post routes

All /post endpoints require a valid token in the header of the request, under the Authorization key.

  • POST /post: add a new blog post and link it to categories in your tables in the database;
The request body should follow the format below
    {
      "title": "Latest updates, August 1st",
      "content": "The whole text for the blog post goes here in this key",
      "categoryIds": [1, 2]
    }
  • GET /post: returns all database all blog posts, user owner and database categories;

  • GET /post/:id: returns the blog post based on the database id if it exists;

  • PUT /post/:id: alters a post in the database if it exists, to change you have to be the owner of the post (tokenId === user_id);

The request body should follow the format below
    {
      "title": "Latest updates, August 1st",
      "content": "The whole text for the blog post goes here in this key"
    }
  • DELETE /post/:id: delete a blog post based on database id if it exists;

  • GET /post/search?q=:searchTerm: fetch all blog posts based on q from the database, if it exists;


Extras

I used the bcrypt library to encrypt user passwords in the database.


Technologies used

Node.js Sequelize JWT/JSON Web Token Express MySQL Docker