Skip to content

Commit

Permalink
Labook Project
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleAntunes committed May 11, 2023
1 parent 5509789 commit ad4e94c
Show file tree
Hide file tree
Showing 29 changed files with 659 additions and 530 deletions.
5 changes: 2 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
PORT=3003

DB_FILE_PATH=./src/database/Labook.db
DB_FILE_PATH=./src/database/nome-do-arquivo.db

JWT_KEY=minha-senha-segura-bananinha
JWT_KEY=senha-de-exemplo-jwt-key
JWT_EXPIRES_IN=7d


BCRYPT_COST=12
8 changes: 0 additions & 8 deletions .env.example

This file was deleted.

6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ O Labook é uma rede social com o objetivo de promover a conexão e interação

## Documentação da API

Link Demonstração:
[Link Demonstração](https://documenter.getpostman.com/view/25825355/2s93ebUBDa)

## Estruturação do banco de dados

img aqui
![projeto-labook (2)](https://user-images.githubusercontent.com/29845719/216036534-2b3dfb48-7782-411a-bffd-36245b78594e.png)

## Requisitos:
Expand All @@ -36,7 +35,7 @@ img aqui
- [x] create post
- [x] edit post
- [x] delete post
- [x] like / dislike post
- [] like / dislike post

- Autenticação e autorização

Expand Down Expand Up @@ -83,7 +82,6 @@ $ npm run dev
8. [Geração de UUID](https://pt.wikipedia.org/wiki/Identificador_%C3%BAnico_universal)
9. [Geração de hashes](https://pt.wikipedia.org/wiki/Fun%C3%A7%C3%A3o_hash_criptogr%C3%A1fica)
10. [Autenticação e autorização](https://pt.wikipedia.org/wiki/Autoriza%C3%A7%C3%A3o)
(https://pt.wikipedia.org/wiki/Autentica%C3%A7%C3%A3o)
11. [Roteamento](https://acervolima.com/roteamento-em-node-js/)
12. [Postman](https://www.postman.com/)

Expand Down
203 changes: 146 additions & 57 deletions src/business/PostBusiness.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,169 @@
import { PostDatabase } from "../database/PostDatabase";
import { BadRequestError } from "../errors/BadRequestError";
import { NotFoundError } from "../errors/NotFoundError";
import { PostDB } from "../types";
import { Post } from "../models/Post";
import { IdGenerator } from "../sevices/IdGenerator";
import { TokenManager } from "../sevices/TokenManager";
import { GetPostInputDTO, GetPostOutputDTO } from "../dtos/Post/getPosts.dto";
import {
CreatePostInputDTO,
CreatePostOutputDTO,
} from "../dtos/Post/createPost.dto";
import {
DeletePostInputDTO,
DeletePostOutputDTO,
} from "../dtos/Post/delete.dto";
import { EditPostInputDTO, EditPostOutputDTO } from "../dtos/Post/editPost.dto";
import { GetPostsInputDTO, GetPostsOutputDTO } from "../dtos/Post/getPosts.dto";

import {
LikeOrDislikePostInputDTO,
LikeOrDislikePostOutputDTO,
} from "../dtos/Post/likeOrdeslikePost.dto";
import { NotFoundError } from "../errors/NotFoundError";

import { LikeDislikeDB, Post, PostDB, POST_LIKE } from "../models/Posts";
import { USER_ROLES } from "../models/User";
import { TokenManager } from "../services/TokenManager";

import { IdGenerator } from "../services/IdGenerator";
import { BadRequestError } from "../errors/BadRequestError";

export class PostBusiness {
constructor(
private postDatabase: PostDatabase,
private idGenerator: IdGenerator,
private tokenManage: TokenManager
private tokenManeger: TokenManager
) {}

public getPosts = async (
input: GetPostInputDTO
): Promise<GetPostOutputDTO> => {
const { q, token } = input;
public getPost = async (
input: GetPostsInputDTO
): Promise<GetPostsOutputDTO> => {
const { token } = input;

const payload = this.tokenManage.getPayload(token);
const payload = this.tokenManeger.getPayload(token);

if (payload === null) {
throw new Error("Você não esta logado");
if (!payload) {
throw new BadRequestError("Token inválido.");
}

const postsDB = await this.postDatabase.findePosts(q);

const post = postsDB.map((postDB) => {
const product = new Post(
postDB.id,
postDB.content,
postDB.likes,
postDB.deslikes,
postDB.created_at,
postDB.updated_at,
postDB.creator_id
);
const postsWithCreatorName =
await this.postDatabase.findPostsWithCreatorName();

return product.toBusinessModel();
const posts = postsWithCreatorName.map((postWithCreatorName) => {
const post = new Post(
postWithCreatorName.id,
postWithCreatorName.content,
postWithCreatorName.likes,
postWithCreatorName.dislikes,
postWithCreatorName.created_at,
postWithCreatorName.updated_at,
postWithCreatorName.creator_id,
postWithCreatorName.creator_name
);
return post.toBusinessModel();
});

const output: GetPostOutputDTO = post;
const output: GetPostsOutputDTO = posts;

return output;
};

public postPost = async (
input: CreatePostInputDTO
): Promise<CreatePostOutputDTO> => {
const { token, content } = input;

const payload = this.tokenManeger.getPayload(token);

if (!payload) {
throw new BadRequestError("Token inválido");
}

const id = this.idGenerator.generate();

const newPost = new Post(
id,
content,
0,
0,
new Date().toString(),
new Date().toString(),
payload.id,
payload.name
);

const newPostDB = newPost.toDBModel();
await this.postDatabase.createPost(newPostDB);

const output: CreatePostOutputDTO = undefined;

return output;
};

public putPost = async (
input: EditPostInputDTO
): Promise<EditPostOutputDTO> => {
const { token, idToEdit, content } = input;

const payload = this.tokenManeger.getPayload(token);

if (!payload) {
throw new BadRequestError("Token inválido");
}

const postDBExists = await this.postDatabase.findPostById(idToEdit);

if (!postDBExists) {
throw new NotFoundError("Post id not found");
}

if (postDBExists.creator_id !== payload.id) {
throw new BadRequestError("Only the creator of the post can edit it");
}

const post = new Post(
postDBExists.id,
postDBExists.content,
postDBExists.likes,
postDBExists.dislikes,
postDBExists.created_at,
postDBExists.updated_at,
postDBExists.creator_id,
payload.name
);

post.setContent(content);

const updatedPostDB = post.toDBModel();
await this.postDatabase.editPost(updatedPostDB);

const output: EditPostOutputDTO = undefined;

return output;
};

public deletePost = async (
input: DeletePostInputDTO
): Promise<DeletePostOutputDTO> => {
const { token, idToDelete } = input;

const payload = this.tokenManeger.getPayload(token);

if (!payload) {
throw new BadRequestError("Token inválido");
}

const postDBExists = await this.postDatabase.findPostById(idToDelete);

if (!postDBExists) {
throw new NotFoundError("Post-Is não existe");
}

if (payload.role !== USER_ROLES.ADMIN) {
if (payload.id !== postDBExists.creator_id) {
throw new BadRequestError("Somente quem criou o post pode deletá-lo");
}
}

await this.postDatabase.removePost(idToDelete);

const output: DeletePostOutputDTO = undefined;

return output;
};
// public createProduct = async (
// input: CreatePostInputDTO
// ): Promise<CreatePostOutputDTO> => {
// const { name, content, like, deslike } = input;

// const id = this.idGenerator.generate();

// const newProduct = new Post(
// id,
// name,
// content,
// like,
// deslike,
// new Date().toISOString(),
// new Date().toISOString(),
// creatorId
// );

// const newProductDB = newProduct.toDBModel();
// await this.PostDatabase.insertProduct(newProductDB);

// const output: CreateProductOutputDTO = {
// message: "Producto cadastrado com sucesso",
// product: newProduct.toBusinessModel(),
// };

// return output;
// };
}
Loading

0 comments on commit ad4e94c

Please sign in to comment.