Skip to content

prtpj1/project-blogs-api

Repository files navigation

23º Projeto: Blogs API

Header


Project DescriptionLearningRequirementsStacksHow to run the application


Descrição do ProjetoAprendizadoRequisitosTecnologias UtilizadasRodar a Aplicação


Project Description

I created this BackEnd project during my learning period at Trybe, where I developed an API and a database for content production for a blog.

This is a NodeJS application that uses the Sequelize package to perform CRUD operations on posts.

In this project, I learned and put into practice

  • Development of endpoints connected to the database following REST principles

According to the project requirements designated by Trybe, I learned how to

  • ✅ Create migrations for the entities User, Categories, BlogPosts and PostCategories
  • ✅ Create the User model in src/database/models/user.js with defined properties
  • ✅ Create the POST /login endpoint
  • ✅ Create the POST /user endpoint
  • ✅ Create the GET /user endpoint
  • ✅ Create the GET /user/:id endpoint
  • ✅ Create the Category model in src/database/models/category.js with defined properties
  • ✅ Create the POST /categories endpoint
  • ✅ Create the GET /categories endpoint
  • ✅ Create the BlogPost model in src/database/models/blogPost.js with defined properties and associations
  • ✅ Create the PostCategory model in src/database/models/postCategory.js with defined properties and associations
  • ✅ Create the GET /post endpoint
  • ❌ Create the POST /post endpoint
  • ❌ Create the GET post/:id endpoint
  • ❌ Create the PUT /post/:id endpoint
  • ❌ Create the DELETE post/:id endpoint
  • ❌ Create the DELETE /user/me endpoint
  • ❌ Create the GET post/search?q=:searchTerm endpoint

Note: In some projects, certain requirements were not completed due to the accelerated dynamics of the course, not because I didn't know how to do them. At the time, I just needed a little more time. I haven't decided yet whether to leave it this way to demonstrate my progress during my learning or to complete the missing requirements from the course projects. Feedback is welcome.

Stacks

BackEnd

  • Docker
  • Express
  • MySQL
  • Node.js

Docker IconExpress IconMySQL IconNodeJS Icon

How to run the application?

  • MySQL must be running on your machine
    ⚠️ If you are on Windows, stop Windows' MySQL, as it will use MySQL from your Linux distro via WSL2
  • Clone the repository:
    git clone git@github.com:prtpj1/project-blogs-api.git
  • Access the project folder:
    cd project-blogs-api
  • Install dependencies:
    npm install
  • Create the ".env" file at the root of the project with the following data:
    HOSTNAME='localhost'
    MYSQL_PORT='3306'
    MYSQL_DB_NAME='blogs-api'
    MYSQL_USER='root'
    MYSQL_PASSWORD='your-mysql-password'
    JWT_SECRET='secret'
  • Create the database and migrations:
    npm run prestart
  • Populate the database:
    npm run seed
  • In the terminal, start the application:
    npm start

With the project running, you can test the routes by clicking on the Swagger link. Swagger image ⚠️ In this app, the route requires token authentication, which is done in the Swagger link above, but you need to follow the steps below:

  1. Open the route POST/user/ and click the Try it out button

Example image to show how to authorize user using swagger

  1. Fill in the fields with the new user data (you don't need to use your real data)

  2. Click the Execute button

Example image to show how to authorize user using swagger

  1. If everything went well, your user was created, you will see the 201 code and the authorization token was generated.

Example image to show how to authorize user using swagger

The token should be something like this:
    {
    "token":     "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo4LCJkaXNwbGF5TmFtZSI6IkZ1bGFubyBkYSBTaWx2YSIsImVtYWlsIjoiZnVsYW5vQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJGh2elo2elE3eEtDLk1PZ2VKUjQ2RXV2Z3I2YmMyT2pLWnZkUnlNc2FrSUpISWMvN2VvUTEyIiwiaW1hZ2UiOiJ3d3cucGljdHVyZS5wbmcifSwiaWF0IjoxNzE4MzA5NDIwLCJleHAiOjE3MTgzOTU4MjB9.gRMp60cD1GSAZwGVaSsfSj2kVxt8_3ZydNvoCY3J22A"
    }

If something goes wrong, the error code and message will be displayed below there (name too short, password too short or not numeric, etc...)
Example image to show how to authorize user using swagger

  1. Copy only the text between the quotes after "token":
  2. At the top of the screen, click the Authorize button, in Value paste the token text you copied and click the Authorize button, then close the modal by clicking Close
  3. Now you can access all other routes as they are authorized.

Note: If you have any difficulty with the instructions and want to provide feedback, send me a message


Descrição do Projeto

Fiz este projeto BackEnd durante meu período de aprendizagem na Trybe onde desenvolvi uma API e um banco de dados para a produção de conteúdo para um blog.

Esta é uma aplicação em NodeJS que usa o pacote sequelize para fazer um CRUD de posts.

Nesse projeto, aprendi e coloquei em prática

  • Desenvolvimento de endpoints que foram conectados ao banco de dados seguindo os princípios do REST

De acordo com os requisitos do projeto designados pela Trybe consegui desenvolver

  • ✅ Criar migrations para as entidades User, Categories, BlogPosts e PostCategories
  • ✅ Criar o modelo User em src/database/models/user.js com as propriedades definidas
  • ✅ Criar o endpoint POST /login
  • ✅ Criar o endpoint POST /user
  • ✅ Criar o endpoint GET /user
  • ✅ Criar o endpoint GET /user/:id
  • ✅ Criar o modelo Category em src/database/models/category.js com as propriedades definidas
  • ✅ Criar o endpoint POST /categories
  • ✅ Criar o endpoint GET /categories
  • ✅ Criar o modelo BlogPost em src/database/models/blogPost.js com as propriedades e associações definidas
  • ✅ Criar o modelo PostCategory em src/database/models/postCategory.js com as propriedades e associações definidas
  • ✅ Criar o endpoint GET /post
  • ❌ Criar o endpoint POST /post
  • ❌ Criar o endpoint GET post/:id
  • ❌ Criar o endpoint PUT /post/:id
  • ❌ Criar o endpoint DELETE post/:id
  • ❌ Criar o endpoint DELETE /user/me
  • ❌ Criar o endpoint GET post/search?q=:searchTerm

OBS: Em alguns projetos alguns requisitos não foram feitos devido a dinamica acelerada do curso e não por eu não saber como fazê-los. Na época eu apenas precisaria de um pouco mais de tempo.

Ainda não decidi se é melhor deixar desta forma para demonstrar o meu progresso durante meu aprendizado ou se seria melhor completar os requisitos que ficaram faltando nos projetos do curso.

Feedbacks são bem vindos.


Tecnologias Utilizadas

BackEnd

  • Docker
  • Express
  • MySQL
  • Node.js

Docker IconExpress IconMySQL IconNodeJS Icon

Como rodar a aplicação?

  • O MySQL precisa estar rodando na sua máquina
    ⚠️ Se estiver no Windows, pare o MySQL do Windows, pois usará o MySQL da sua distro linux através do WSL2
  • Clone o repositório:
    git clone git@github.com:prtpj1/project-blogs-api.git
  • Acesse a pasta do projeto:
    cd project-blogs-api
  • Instale as dependências:
    npm install
  • Crie o arquivo ".env" na raiz do projeto com os seguintes dados:
    HOSTNAME='localhost'
    MYSQL_PORT='3306'
    MYSQL_DB_NAME='blogs-api'
    MYSQL_USER='root'
    MYSQL_PASSWORD='sua-senha-mysql'
    JWT_SECRET='secret'
  • Crie o banco de dados e as migrations:
    npm run prestart
  • Popule o banco de dados:
    npm run seed
  • No terminal inicie a aplicação:
    npm start

Com o projeto rodando você poderá testar as rotas clicando no link do Swagger Tela do Swagger ⚠️ Neste app a rota precisa de autenticação via token que é feita na própria rota do Swagger linkado acima, mas é preciso seguir alguns passos:

  1. Abra a rota POST/user/ e clique no botão Try it out

Imagem para demonstrar como criar um novo usuário autorizado e poder utilizar o swagger

  1. Preencha os campos com os dados do novo usuário (não precisam ser dados reais)

  2. Clique no botão Execute

Imagem para demonstrar como criar um novo usuário autorizado e poder utilizar o swagger

  1. Se tudo correu bem, o seu usuário foi criado, você verá o código 201 e foi gerado o token de autorização.

Imagem para demonstrar como criar um novo usuário autorizado e poder utilizar o swagger

O token deve ser algo parecido com este:
    {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo4LCJkaXNwbGF5TmFtZSI6IkZ1bGFubyBkYSBTaWx2YSIsImVtYWlsIjoiZnVsYW5vQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJGh2elo2elE3eEtDLk1PZ2VKUjQ2RXV2Z3I2YmMyT2pLWnZkUnlNc2FrSUpISWMvN2VvUTEyIiwiaW1hZ2UiOiJ3d3cucGljdHVyZS5wbmcifSwiaWF0IjoxNzE4MzA5NDIwLCJleHAiOjE3MTgzOTU4MjB9.gRMp60cD1GSAZwGVaSsfSj2kVxt8_3ZydNvoCY3J22A"
    }

Caso algo de errado, será exibido o código do erro e a mensagem de erro (nome muito curto, senha muito curta ou não numérica, etc...) Imagem para demonstrar como criar um novo usuário autorizado e poder utilizar o swagger

  1. Copie somente o texto que está entre aspas após "token":
  2. No topo da tela clique no botão Authorize, em Value cole o texto do token que você copiou e clique no botão Authorize e feche o modal clicando em Close
  3. Agora você pode acessar todas as outras rotas, pois já estão autorizadas.

OBS: Se tiver alguma dificuldade com as instruções e quiser dar um feedback me mande uma mensagem

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors