Labeddit

Table of Contents
  1. About The Project
  2. Diagram
  3. Documentation
  4. RoadMap
  5. Estructure REST
  6. Getting Started
  7. Usage
  8. FrontEnd Repository
  9. Exemples requests
  10. Contact
  11. Acknowledgments

About the project

Labeddit is a social network with the goal of connecting and interacting between posts from its users. Anyone who signs up for the app can create, like, and comment on posts.

Built With

  • NodeJS
  • Typescript
  • Express
  • SQL e SQLite
  • Knex
  • POO
  • Layered architecture
  • UUID
  • Password hashed
  • Autentication and autozrization
  • Router
  • Postman

Diagram

Diagram

Documentation

Ducumentation of the API with instrutions on how to use the endpoints avalible for the Labeddit aplication

Labook API

RoadMap

  • Endpoints

    • Users
      • Signup
      • Login
      • Delete user
    • Posts
      • Get Posts
      • Edit Post
      • Reaction Post
      • Create new Post
      • Delete Post
    • Comments
      • Get Comments of a Post
      • Edit Comment
      • Reaction Comment
      • Create new comment
      • Delete Comment
  • Authentication and authorization

    • UUID
    • Hashed passwords with Bcrypt.
    • tokens JWT
  • Code

    • POO
    • Layered architecture
    • Router in Express
    • SOLID
    • Clean code

Methods

Requests for the API must follow HTTP RESTful patterns.

Methods Description
GET Returns information for one or more records.
POST Used to create a new record or login access.
PUT Updates data for a record or changes its status.
PATCH Partially updates data for a record.
DELETE Removes a record from the system.

Response

Código Descrição
200 Request successfully executed (success).
201 Data created successfully (success).
400 Validation errors or fields provided do not exist in the system.
404 Searched record not found (Not found).
409 The user already exists in the system. (Conflict).
500 Unexpected error.

Getting Started

Here is an example of instructions on how to set up the project locally. To have a local copy, follow the steps below:

Instalation

  1. Clone repository
git clone https://github.com/Afmjuniors/labeddit-back.git
  1. Install NPM TypeScript packages
npm init -y (create package.json)
npm i -g typescript (just once)
npm i typescript -D (install typescript in the project)
npx tsc -init (create tsconfig.json)
  1. Install NPM Express packages
npm install express
npm install @types/express -D
  1. Install NPM Cors packages
npm install cors
npm install @types/cors -D
  1. Install NPM Node packages
npm install ts-node-dev -D
  1. Run NPM Universally Unique Identifier (UUID)
npm install uuid
npm install -D @types/uuid
  1. Run NPM Environment variables (ENV, see .env.exemple)
npm install dotenv
  1. Run NPM JWT(Token)
npm install jsonwebtoken
  npm install -D @types/jsonwebtoken
  1. Run NPM Bcrypt
npm i bcryptjs
 npm i -D @types/bcryptjs
  1. Run NPM Jest
npm i -D jest @types/jest ts-jest
  1. Run NPM developer
npm run dev

Usage

This application is a Fullstack development of a working social media plataform. Using a blog format the user can view posts by others users, Like or Dislike them and even comment in a specific post. To see the application online deployed visti the vercel('https://labeddit-front.vercel.app/')

FrontEnd-Repository

To see repository of this application Front-end

Exemples requests

It isnt necessary to use the email, name, and password in the exemples. However remember to respect th basic struture.

Signup

Public endpoint for create new user.

// request POST /users/signup
// body JSON
{
    "name":"junior",
    "email":"junior@email.com",
    "password":"123456@Aa"
}

// response
// status 201 CREATED
{
    "message": "Usuario adicionado com sucesso",
    "user": {
        "id": "UUID",
        "name": "junior",
        "email": "junior@email.com",
        "role": "NORMAL",
        "createdAt": Date.now(),
        "updatedAt": Date.now()"
    },
    "token": "token jwt"
}

Login

Public endpoint for login user.

// request POST /users/login
// body JSON
{
  "email": "junior@email.com",
  "password": "123456@Aa"
}

// response
// status 200 OK
    "message": "Login feito com sucesso",
    "user": {
        "id": "UUID",
        "name": "junior"
    },
    "token": "token jwt"
}

Delete User

Protected endpoint for deleted logged user.

// request DELETE /users
// headers.authorization = "token jwt"

// response
// status 200 OK
{
    "message": "Usuario deletado com sucesso"
}

Get posts

Protected endpoint get all posts. required a jwt token.

// request GET /posts
// headers.authorization = "token jwt"

// response
// status 200 OK
[
    {
        "id": "UUID",
        "content": "Conten of post made in the postman",
        "likes": 0,
        "dislikes": 0,
        "comments": 1,
        "creator": {
            "id": "UUID",
            "name": "Alexandre Machado"
        },
        "userReaction": [
            null
        ],
        "createdAt": "2023-03-11T20:58:49.263Z",
        "updatedAt": "2023-03-11T20:58:49.263Z"
    },
    {
        "id": "UUID",
        "content": "Content as a test made in the postman",
        "likes": 0,
        "dislikes": 0,
        "comments": 0,
        "creator": {
            "id": "UUID",
            "name": "junior"
        },
        "userReaction": [
            null
        ],
        "createdAt": "2023-03-11T20:58:49.263Z",
        "updatedAt": "2023-03-11T20:58:49.263Z"
    }
]

Create new post

Protected endpoint create a new post. required a jwt token.

// request POST /posts
// headers.authorization = "token jwt"
// body JSON
{
    "content": "New Post!"
}

// response
// status 201 CREATED
{
    "message": "Post adicionado com sucesso",
    "post": {
        "id": "UUID",
        "content": "New Post!",
        "likes": 0,
        "dislikes": 0,
        "comments": 0,
        "creator": {
            "id": "UUID",
            "name": "junior"
        },
        "userReaction": [
            null
        ],
        "createdAt": Date.now(),
        "updatedAt": Date.now()
    }
}

Edit post

Protected endpoint edit a post. required a jwt token.
Only the creator can edit its content

// request PATCH /posts/:id
// headers.authorization = "token jwt"
// body JSON
{
    "content": "New content, of the post created by junior"
}

// response
// status 200 OK
{
    "message": "Post adicionado com sucesso",
    "post": {
        "id": "UUID",
        "content": "New content, of the post created by junior",
        "likes": 0,
        "dislikes": 0,
        "comments": 0,
        "creator": {
            "id": "UUID",
            "name": "junior"
        },
        "userReaction": [
            null
        ],
        "createdAt": "2023-03-11T21:05:56.708Z",
        "updatedAt": Date.now()
    }
}

Delete post

Protected endpoint delete a post. required a jwt token.
Only the creator can delete it. ADMIN can delete all posts.

// request DELETE /posts/:id
// headers.authorization = "token jwt"

// response
// status 200 OK
{
    "message": "Post deletado com sucesso"
}

Reaction Post

Protected endpoint create a new post. required a jwt token.
Case the user had liked the post and do it again, the reaction will be neutral.
Case the user had liked the post and disliked it now, the reaction will be inverse.

Like (function 1)

// request PUT /posts/:id/reaction
// headers.authorization = "token jwt"
// body JSON
{
    "like": true
}

// response
// status 200 OK
{
    "message": "O usuario deu like no post"
}

Dislike (function 2)

// request PUT /posts/:id/reaction
// headers.authorization = "token jwt"
// body JSON
{
    "like": false
}

// response
// status 200 OK
{
    "message": "O usuario trocou para dislike"
}

Dislike (function 3)

// request PUT /posts/:id/reaction
// headers.authorization = "token jwt"
// body JSON
{
    "like": false
}

// response
// status 200 OK
{
    "message": "O usuario desfez o dislike"
}

Get Comments

Protected endpoint get all comments of an post. required a jwt token.

// request GET /comments/:id
// headers.authorization = "token jwt"

// response
// status 200 OK
[
    {
        "id": "UUID",
        "postId": "UUID",
        "content": "New content",
        "likes": 0,
        "dislikes": 1,
        "createdAt": Date.now(),
        "updatedAt": Date.now(),
        "creator": {
            "id": "UUID",
            "name": "Alexandre Machado"
        },
        "userReaction": [
            null
        ]
    }
]

Create new comment

Protected endpoint create a new comment of a post. required a jwt token.

// request POST /comments/:id
// headers.authorization = "token jwt"
// body JSON
{
    "content": "New content!"
}

// response
// status 201 CREATED
{
    "message": "Post adicionado com sucesso",
    "post": {
        "id": "UUID",
        "content": "New content!",
        "likes": 0,
        "dislikes": 0,
        "comments": 0,
        "creator": {
            "id": "UUID",
            "name": "Alexandre Machado"
        },
        "userReaction": [
            null
        ],
        "createdAt": Date.now(),
        "updatedAt": Date.now()
    }
}

Edit Comment

Protected endpoint create a post. required a jwt token.
Only the creator can edit its content

// request PATCH /comments/:id
// headers.authorization = "token jwt"
// body JSON
{
    "content": "New content, of the post created by junior"
}

// response
// status 200 OK
{
    "message": "Post adicionado com sucesso",
    "post": {
        "id": "UUID",
        "content": "New content, of the post created by junior",
        "likes": 0,
        "dislikes": 0,
        "comments": 0,
        "creator": {
            "id": "UUID",
            "name": "junior"
        },
        "userReaction": [
            null
        ],
        "createdAt": "2023-03-11T21:05:56.708Z",
        "updatedAt": Date.now()
    }
}

Delete Comment

Protected endpoint delete a comment. required a jwt token.
Only the creator can delete it. ADMIN can delete all posts.

// request DELETE /comments/:id
// headers.authorization = "token jwt"

// response
// status 200 OK
{
    "message": "Post deletado com sucesso"
}

Reaction Comment

Protected endpoint create a new comment. required a jwt token.
Case the user had liked the comment and do it again, the reaction will be neutral.
Case the user had liked the comment and disliked it now, the reaction will be inverse.

Like (function 1)

// request PUT /comments/:id/reaction
// headers.authorization = "token jwt"
// body JSON
{
    "like": true
}

// response
// status 200 OK
{
    "message": "O usuario deu like no post"
}

Dislike (function 2)

// request PUT /comments/:id/reaction
// headers.authorization = "token jwt"
// body JSON
{
    "like": false
}

// response
// status 200 OK
{
    "message": "O usuario trocou para dislike"
}

Dislike (function 3)

// request PUT /comments/:id/reaction
// headers.authorization = "token jwt"
// body JSON
{
    "like": false
}

// response
// status 200 OK
{
    "message": "O usuario desfez o dislike"
}

Contact

Alexandre Machado - afmjuniors@gmail.com

Linkedin

Acknowledgments

  • As my last project made by the bootcamp Labenu I would like to thanks them, every single teacher was critical to my learning experience, and the support staff was wornderful.
  • Also as the last project by the bootcamp I would like to thanks my colegues form Ammal-A, they were a very unite commuty and I hope that we continue to do so.