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 instructions
git clone git@github.com:lucas-da-silva/project-blogs-api.git
cd project-blogs-api
docker-compose up -d
docker exec -it blogs_api bash
npm install
npm start
You can use Thunder Client or Insomnia (or whatever) to check API routes.
docker-compose down
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.
POST /login
: returns atoken
;
The request body should follow the format below
{
"email": "lewishamilton@gmail.com",
"password": "123456"
}
POST /user
: adds a newuser
to the database and returns atoken
;
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 databaseusers
; -
GET /user/:id
: returns theuser
based on the databaseid
if it exists;
The following endpoint needs a valid token in the request
header
, within theAuthorization
key;
DELETE /user/me
: delete you from the database, based on theid
inside yourtoken
;
POST /categories
: adds a newcategory
to the database;
The request body should follow the format below
{
"name": "Typescript"
}
GET /categories
: returns all databasecategories
;
All
/post
endpoints require a valid token in theheader
of the request, under theAuthorization
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 blogposts
,user
owner and databasecategories
; -
GET /post/:id
: returns the blogpost
based on the databaseid
if it exists; -
PUT /post/:id
: alters apost
in the database if it exists, to change you have to be theowner
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 blogpost
based on databaseid
if it exists; -
GET /post/search?q=:searchTerm
: fetch all blogposts
based onq
from the database, if it exists;
I used the bcrypt library to encrypt
user passwords
in the database.