forked from labenuexercicios/projeto-labook-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5509789
commit ad4e94c
Showing
29 changed files
with
659 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
// }; | ||
} |
Oops, something went wrong.