Skip to content

Blog-san is a blog API, where users can create posts, comments and reply to comments.

License

Notifications You must be signed in to change notification settings

mtpontes/blog-san-api

Repository files navigation

Blog-san API

Blog-san is a simple REST API project, with the intention of practicing, CRUD, mapping and entity relationships. There, users can create an account, publish, comment and respond.

🛠️ Tecnologies

⚙️ Functionalities

  • User registration;
  • Authentication and authorization;
  • CRUD for publications and comments;
  • Public acces for readers, but without interactions with publications and other users;
  • Relationships between publications to comments and comments to comments;

📖 How to use

Documentation

The documentation can be accessed after deploying the application via the URL http://localhost:8080/swagger-ui/index.html#/

You can also import my set of requests into Postman. There you have all the endpoints with all the necessary URL parameters and body details to interact with the API.

Run In Postman

Examples

Note: All GET request endpoints are accessible without authentication.

By default, all users are created with the USER role, these users can only create comments on posts. To become an ADMIN and be able to create posts, you can use the system's default ADMIN user:

Default user ADMIN

  • login: root
  • password: root

This way you can authenticate as ADMIN to have the freedom to create posts and even other ADMINs in the system.


Register

To create posts and comments, you need to register:

POST: /auth/register
Content-Type: application/json

{
	"login": "newUser",
	"password": "newPassword",
	"name": "Example Name",
	"email": "[email protected]"
}

Login

After registering, you need to authenticate:

POST: /auth/login
Content-Type: application/json

{
	"login": "root",
	"password": "root",
}

Response:

{
  "token": "your_access_token"
}

This access token will be your 'free pass' to create posts and comments


Using the access token

After receiveing the successful access token, you need to include the header for your future requests. The access token must be passed as parte of the "Authorization" title.

Header example:

Authorization: Bearer your_access_token

Publication creation

POST: /publications

{
  "description": "Publication content",
  "imageLink": "link_for_image"
}

Response:

{
    "publicationId": 1,
    "userId": 1,
    "nameUser": "Your User Name",
    "description": "Publication content",
    "imageLink": "link_for_image",
    "date": "2024-02-10 19:13"
}

Comment creation

POST: /publications/{publicationId/comments

{
	"publicationId": 1,
	"text": "Comment example"
}

Response:

{
    "commentId": 1,
    "userId": 1,
    "text": "Comment example",
    "date": "2024-02-10 19:10",
    "edited": false
}

These are basic examples, and you can explore other endpoints as needed. Be sure to replace the dummy values with actual data from your development environment.

🚀 How to run

The application is configured to connect to MySQL via port 3306.

Environment variables:

Database

ENV DEFAULT VALUE DESCRIPTION
DB_USERNAME root Database username
DB_PASSWORD root Database password

Security

ENV DEFAULT VALUE DESCRIPTION
JWT_SECRET secret JWT token secret

Run

Clone this repository:

  git clone https://github.com/mtpontes/blog-san-api.git

Deploy with Docker

Prerequisites

  • Docker
  • Docker Compose

Deploy

Raise the containers:

docker-compose up --build